MySQL编译和安装

##在CentOS7中编译安装MySQL 5.7.21. 依赖和源码包 安装相关的依赖:

1
2
yum install gcc gcc-c++ ncurses ncurses-devel cmake bison openssl-devel -y 
yum install make cmake gcc gcc-c++ bison bison-devel ncurses ncurses-devel autoconf automake

下载MySQL 5.7.32源码包和依赖boost, MySQL 5.7.32依赖boost 1.59.0:

1
2
3
4
curl -o boost_1_59_0.tar.gz https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz 
#curl -o mysql-5.7.32.tar.gz https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.32.tar.gz
如果拉取不到使用下方下载地址 :
https://downloads.mysql.com/archives/community/

解压下载的包:

1
2
3
4
5
# 进入下载的路径 
# 解压到/usr/local/目录
tar -xzvf boost_1_59_0.tar.gz -C /usr/local/
# 解压到当前目录
tar -xzvf mysql-5.7.32.tar.gz

创建用户和组

创建MySQL用户和组, 并且用户不能登陆:

1
groupadd -r mysql && useradd -r -g mysql -s /sbin/nologin -M mysql 

创建相关的目录

创建数据目录

1
2
3
4
5
6
7
mkdir -p /home/mysql/data

mkdir -p /home/mysql/logs

mkdir -p /usr/local/mysql

mkdir -p /home/mysql/temp
1
2
3
4
chown -Rf mysql:mysql /usr/local/mysql


chown -Rf mysql:mysql /home/mysql

预编译

使用各种参数, 预编译源代码. 进入解压的MySQL源码目录, 执行以下命令:

1
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/home/mysql/data -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysqld.sock -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DENABLED_PROFILING=1 -DMYSQL_TCP_PORT=3306 -DWITH_DEBUG=0 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost_1_59_0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
DCMAKE_INSTALL_PREFIX=/usr/local/mysql :安装路径
DMYSQL_DATADIR=/data/mysql :数据文件存放位置
DSYSCONFDIR=/etc :my.cnf路径
DMYSQL_UNIX_ADDR=/usr/local/mysql/mysqld.sock :连接数据库socket路径
DEXTRA_CHARSETS=all :安装所有的字符集
DDEFAULT_CHARSET=utf8mb4 :默认字符
DDEFAULT_COLLATION=utf8mb4_unicode_ci :排序集
DWITH_MYISAM_STORAGE_ENGINE=1 :支持MyIASM引擎
DWITH_INNOBASE_STORAGE_ENGINE=1 :支持InnoDB引擎
DWITH_PARTITION_STORAGE_ENGINE=1 :安装支持数据库分区
DENABLED_LOCAL_INFILE=1 :允许从本地导入数据
DENABLED_PROFILING=1 :
DMYSQL_TCP_PORT=3306 :端口
DWITH_DEBUG=0 :
DDOWNLOAD_BOOST=1 :允许下载
DWITH_BOOST=/usr/local/boost_1_59_0 :本地boost路径

编译安装

预编译完成后, 执行下面的命令编译, 安装:

1
2
# 指定CPU数量编译 
make -j `grep processor /proc/cpuinfo | wc -l` && make install

添加开机自启

对目录修改权限, 添加service/systemd服务:

1
2
3
4
5
6
chown -R mysql:mysql /usr/local/mysql 
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
# 开机自启
chkconfig --add mysql
chkconfig mysql on

环境变量

/usr/local/mysql/bin添加进入环境变量, 或者直接使用软链接的方式链到/usr/local/bin下:

1
2
3
4
5
6
7
8
# 添加到环境变量 
echo "" >> /etc/bashrc
echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/bashrc
echo "" >> /etc/bashrc
source ~/.bashrc

# 使用软链接
ln -s /usr/local/mysql/bin/* /usr/local/bin/

初始化数据库

以上都完成后, 还不能启动MySQL, 如果非要启动, 会报错. 需要初始化数据库:

1
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data
1
2
3
--user :指定用户 
--basedir :mysql所在目录
--datadir :mysql数据库和表所在的目录,以及PID文件

初始化后, 会有一行提示, 冒号后面的是初始密码root@localhost: password:

1
A temporary password is generated for root@localhost: xKefZvib13)5 

启动服务

以上都配置完成, 就可以启动服务了:

1
2
3
4
5
6
# 使用service 
service mysql start

# 使用systemd
systemctl daemon-reload
systemctl start mysql

修改密码

将初始密码修改成自己的密码, 直接在shell中输入命令: mysqladmin -uroot -p'old_pass' password 'new_pass'

配置文件

默认MySQL不需要配置文件, 编译时已经配置好了, 但是也可以使用配置文件, 指定log的位置, 编辑vim /etc/my.cnf, 将以下内容添加到文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
[client]

port = 3306

socket = /usr/local/mysql/mysql.sock

default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]

character-set-client-handshake=FALSE

character-set-server=utf8mb4

collation-server=utf8mb4_unicode_ci

init_connect='SET NAMES utf8mb4'

#character-set-server = utf8

#collation-server = utf8_general_ci

skip-external-locking

skip-name-resolve

user = mysql

port = 3306

basedir = /usr/local/mysql

datadir = /home/mysql/data

tmpdir = /home/mysql/temp

# server_id = .....

socket = /usr/local/mysql/mysql.sock

log-error = /home/mysql/logs/mysql_error.log

pid-file = /home/mysql/mysql.pid

open_files_limit = 10240

back_log = 600

max_connections=500

max_connect_errors = 6000

wait_timeout=605800

#open_tables = 600

#table_cache = 650

#opened_tables = 630

max_allowed_packet = 32M

sort_buffer_size = 4M

join_buffer_size = 4M

thread_cache_size = 300

query_cache_type = 1

query_cache_size = 256M

query_cache_limit = 2M

query_cache_min_res_unit = 16k

tmp_table_size = 256M

max_heap_table_size = 256M

key_buffer_size = 256M

read_buffer_size = 1M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 64M

lower_case_table_names=1

default-storage-engine = INNODB

innodb_buffer_pool_size =2G

innodb_log_buffer_size = 32M

innodb_log_file_size = 128M

innodb_flush_method =O_DIRECT



#####################

#thread_concurrency = 32 5.7不支持

long_query_time= 2

slow-query-log=on

slow-query-log-file =/home/mysql/logs/mysql-slow.log

[mysqldump]

quick

max_allowed_packet = 32M

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid