[MySQL] Linux下MySQL-5.6源码安装

时间:2022-09-14 16:17:56

本文主要介绍centos下源码安装MySQL 5.6的方法,centos的版本为5.8.

1)首先,你需要到MySQL官网下载源码tar包,点击MySQL Community Server,选择Source Code,源码包不大,只有34M左右。

注:以下操作没有特殊说明,都是以root账户执行。

2)先安装cmake(mysql5.5以后源码安装都得通过cmake编译)

# yum install cmake

并确保以下两个包已安装最新版:

  ncurses
  ncurses-devel

3)添加MySQL用户组和用户

# groupadd mysql
# useradd -g mysql mysql
4)创建MySQL软件安装目录和数据存放目录

mkdir -p /opt/mysql   #MySQL安装目录
chown -R mysql:mysql /opt/mysql
mkdir -p /data/mysql #MySQL数据存放目录
mkdir -p /data/mysql/data #存放数据
mkdir -p /data/mysql/log #存放日志
mkdir -p /data/mysql/tmp #存放临时文件
chown -R mysql:mysql /data/mysql
5)cmake编译安装MySQL

tar zxvf mysql-5.6.13.tar.gz
cd mysql-5.6.13
cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/opt/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1
make
make install

参数说明:

-DCMAKE_INSTALL_PREFIX=/opt/mysql        //指定安装目录
-DINSTALL_DATADIR=/data/mysql            //指定数据存放目录
-DSYSCONFDIR=/opt/mysql                  //指定配置文件目录(本例的配置文件为/opt/mysql/my.cnf)
-DDEFAULT_CHARSET=utf8                   //指定字符集
-DDEFAULT_COLLATION=utf8_general_ci      //指定校验字符
-DEXTRA_CHARSETS=all                     //安装所有扩展字符集
-DENABLED_LOCAL_INFILE=1                 //允许从本地导入数据

上述步骤执行成功后,MySQL软件已成功安装至/opt/mysql目录下。

7)编辑配置文件my.cnf

[client]
socket = /opt/mysql/run/mysql.sock

[innotop]
socket = /opt/mysql/run/mysql.sock

[mysql]
prompt = \\u@\\d \\r:\\m:\\s>
no-auto-rehash

[mysqld_safe]
pid-file = /opt/mysql/run/mysqld.pid

[mysqld]

#### Baes dir ####
basedir = /opt/mysql
datadir = /data/mysql/data
tmpdir = /data/mysql/tmp
socket = /opt/mysql/run/mysql.sock

#### Base configure info ####
port = 3306
skip-name-resolve
old_passwords = 0
lower_case_table_names = 1
open_files_limit = 65535
read_rnd_buffer_size = 5M
max_allowed_packet = 24M
max_connect_errors = 50000
max_connections = 1000
max_user_connections = 950
thread_cache_size=64
table_open_cache=1024
thread_stack=262144
wait_timeout=864000

#### Log info ####
log-error = /data/mysql/log/alert.log
slow_query_log=1
slow_query_log_file= /data/mysql/log/slow.log
log-slow-admin-statements
long_query_time = 0.1
slow_launch_time=1

#### Binary log && Relay log ####
binlog_format='MIXED'
log-bin = /data/mysql/log/mysql-bin
log-slave-updates = 1
relay-log = /data/mysql/log/mysqld-relay-bin
relay-log-index = /data/mysql/log/mysqld-relay-bin.index
master-info-file = /data/mysql/log/master.info
relay-log-info-file = /data/mysql/log/relay-log.info
max_binlog_size = 500M
max_binlog_cache_size = 2G


#### query cache ####
query_cache_size = 100M
query_cache_limit = 1K
query_cache_min_res_unit = 1K
query_cache_type=2

#myisam
concurrent_insert = 2
key_buffer_size = 100M
sort_buffer_size = 100K
join_buffer_size = 100K
read_buffer_size = 1M
myisam_sort_buffer_size = 100M

#innodb plugin

#innodb
default-storage-engine = INNODB
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
innodb_open_files=60000
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout = 100
innodb_additional_mem_pool_size = 20M
innodb_buffer_pool_size = 16G
innodb_log_buffer_size= 400M
innodb_log_file_size = 100M
innodb_log_files_in_group = 4
innodb_file_io_threads = 4
innodb_thread_concurrency = 16
innodb_max_dirty_pages_pct = 50
transaction-isolation = READ-COMMITTED
innodb_data_file_path = ibdata1:10G;ibdata2:5G:autoextend

innodb_buffer_pool_instances=4
innodb_thread_concurrency=32

### Server id ####
log_bin_trust_function_creators=1
read_only=0
server_id=5

8)创建数据库

/opt/mysql/scripts/mysql_install_db   --user=mysql 

上述建库语句将根据my.cnf里设置的数据文件目录和日志文件目录,生成相应的数据文件和日志文件,并创建系统数据库(如mysql,test,information_schema,performance_schema)

9)启动MySQL

/opt/mysql/support-files/mysql.server start

启动成功后,就可以以root用户登录(默认的root用户没有密码)

/opt/mysql/bin/mysql -u root

10)设置root密码,创建只读账号

export PATH=$PATH:/opt/mysql/bin 
mysqladmin -u root password '***'
mysql -uroot -p
root@(none) 06:08:34>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '***' WITH GRANT OPTION;
root@(none) 06:08:44>GRANT SELECT ON *.* TO 'rnd'@'%' IDENTIFIED BY '***';
11)最后,把MySQL加入系统启动项和开机启动

cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld #把Mysql加入系统启动
chmod 755 /etc/init.d/mysqld #增加执行权限
chkconfig mysqld on #加入开机启动
这样,执行service mysqld start即可启动MySQL了:)。