上一篇博客文章:Linux安装glibc版mysql-5.7.15已经讲到glibc版MySQL安装,那么这一篇就讲一下MySQL源码编译安装。
准备工作
1、下载好mysql-5.7.16.tar.gz源码包,下载地址可以是MySQL官网,也可以是国内的镜像地址,如:http://mirrors.sohu.com/mysql/,下载最新版即可。
2、下载好boost包,MySQL 5.7.5开始Boost库是必需的
3、将mysql的源码包,boost包上传到服务器,路径:/usr/local/
安装MySQL依赖库
1 | yum -y install gcc gcc-c++ ncurses ncurses-devel cmake |
安装MySQL
##进入安装目录1
cd /usr/local/
##解压安装包1
2tar -xvf mysql-5.7.16.tar.gz
tar -xvf boost_1_59_0.tar.gz
##创建MySQL用户组1
groupadd mysql
##创建MySQL用户1
useradd -g mysql mysql -s /bin/false
##创建MySQL的data目录(这个可以自定义)1
mkdir -p /opt/mysql/data
##改变目录的所有者1
chown -R mysql:mysql /opt/mysql/
##进入MySQL目录cmake1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16cd mysql-5.7.16
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/opt/mysql/data \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=../boost_1_59_0 \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DTRACE=0 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EMBEDDED_SERVER=1
##编译安装,这个过程时间会有点长,同时要保证硬盘空间大于5G,编译后的文件是4.6G左右1
make && make install
##如果要重新cmake,需要删除CMakeCache.txt文件1
rm -f CMakeCache.txt
##进入安装目录,初始化MySQL1
2cd /usr/local/mysql
./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/opt/mysql/data --initialize
##会出现下面提示,记住root账号的初始密码:i<VzkKS)P9dH,修改密码是要用到
2016-10-2T02:18:23.437852Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-10-2T02:18:23.718104Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-10-2T02:18:23.866501Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this
server has been started. Generating a new UUID: 9731b834-3cd6-11e6-8654-fcaa14e34b30.
2016-10-2T02:18:23.896540Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2016-10-2T02:18:23.898416Z 1 [Note] A temporary password is generated for root@localhost: i<VzkKS)P9dH
##可能系统原因,有的可能没有上面提示,这是正常情况,只要没报错就没问题
##到了这里已经成功了一半
##为了避免启动出错,需要修改一下启动文件1
vi support-files/mysql.server
##找到basedir和datadir,修改为安装目录和指定的data目录1
2basedir=/usr/local/mysql
datadir=/opt/mysql/data
##保存退出
##拷贝配置文件到/etc/下1
cp support-files/my-default.cnf /etc/my.cnf
##编辑my.cnf1
vi /etc/my.cnf
##添加下面内容1
2
3
4
5[mysqld]
basedir=/usr/local/mysql
datadir=/opt/mysql/data
user=mysql
port=3306
##把sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES这一句去掉(避免启动出错)
##保存退出
##拷贝启动文件到/etc/init.d/1
cp support-files/mysql.server /etc/init.d/mysql
##添加执行权限1
chmod +x /etc/init.d/mysql
##添加开启启动MySQL1
2chkconfig --add mysql
chkconfig mysql on
##启动MySQL测试一下1
service mysql start
##看到这一句,代表MySQL已经成功启动:Starting MySQL. SUCCESS!
##如果启动出错,先看一下mysql的日志,一般的错误可能是写入权限,指定文件目录不存在等原因
修改MySQL初始密码
##创建一个软连接1
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
##初始密码在初始化MySQL的时候有提示,如果没保存,或者初始化的时候没有提示,
##可以直接在MySQL的日志文件里查找。日志的默认路径:/var/log/mysqld.log1
2cat /var/log/mysqld.log|grep 'temporary password'
2016-10-04T07:51:58.185607Z 1 [Note] A temporary password is generated for root@localhost: WG5QS)/gXARs
##用初始密码登录MySQL1
/usr/local/mysql/bin/mysql_secure_installation -p
##出现以下提示
mysql_secure_installation: [Warning] Using a password on the command line interface can be insecure.
Securing the MySQL server deployment.
Enter password for user root: ##输入初始密码
The existing password for the user account root has expired. Please set a new password.
New password: ##输入新密码
Re-enter new password: ##输入新密码
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: y ###是否校验密码插件
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
###密码设定有三种,LOW:密码长度大于等于8位;
###MEDIUM:密码长度大于等于8位,并且要有数字,大小写字母和特殊符号;
###STRONG:是MEDIUM基础上加上dictionary file(指定密码验证的文件路径)
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1 ###设定密码策略等级,这里选择1
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : NO ##暂时不修改root密码
… skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y ###是否移除匿名用户
Success.
Normally, root should only be allowed to connect from
‘localhost’. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y ###是否关闭root远程登陆功能
Success.
By default, MySQL comes with a database named ‘test’ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y ###是否移除测试数据库
Dropping test database…
Success.
Removing privileges on test database…
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y ###是否立即生效权限表
Success.
All done!
###修改完成,测试一下1
mysql -uroot -p
添加MySQL其他用户
##登录MySQL1
2
3
4
5
6
7
8
9
10
11
12
13[root@Tenken mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.16-log Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
###添加一个test用户并且不指定地址1
2mysql> create user 'test'@'%' identified by 'Aa123456&*';
Query OK, 0 rows affected (0.01 sec)
##授权test用户1
2grant all privileges on *.* to 'test'@'%' identified by 'Aa123456&*';
Query OK, 0 rows affected (0.01 sec)
##刷下一下权限表1
2mysql>flush privileges;
Query OK, 0 rows affected (0.01 sec)
###现在可以用数据库工具来测试test账号了
末尾
可能会出现的问题:
1、–datadir指定目录不能有其他文件,否则会报错,同时要修改目录的权限,否则写入文件失败,同样会报错。
2、启动文件:support-files/mysql.server的basedir和datadir要修改为安装时的配置参数,否则同样会报错
到此MySQL5.7.16的源码包编译安装结束,其实源码包安装跟glibc二进制包安装有大部分的操作是相同的,源码编译安装多了一步cmake,make && make install,后面的跟glibc版的一样。
附上修改后的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[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /opt/mysql/data
pid-file = /opt/mysql/data/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
#skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30
log_error = /opt/mysql/log/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /opt/mysql/log/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M