Mysql5.7二进制包安装以及主从复制环境搭建

时间:2020-12-15 20:58:40

环境说明

Mysql:mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz

操作系统:CentOS Linux release 7.5.1804 (Core)

Mysql通用二进制包安装

1、删除mysql旧版本或者相关文件

#查找删除旧版本的mysql rpm -qa|grep -i mysql

删除/etc/my.cnf文件和/etc/mysql目录

2、安装重要依赖包

MySQL依赖于libaio库。如果这个库没有在本地安装,那么数据目录初始化和后续服务器启动步骤将会失败。

yum search libaio  # search for info
yum install libaio # install library

对于MySQL 5.0.19和更高版本:对非均匀内存访问(NUMA)的支持已经添加到通用Linux构建中,后者现在在libnuma库中有依赖关系

3、创建Mysql用户和组

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

因为用户只需要用于所有权目的,而不是登录目的,所以useradd命令使用-r和-s/bin/false选项来创建一个没有登录权限给服务器主机的用户。如果OS的useradd不支持它们,可以忽略这些选项。

4、解压tar.gz包

解压tar.gz包到安装位置,一般是/usr/local/mysql

cd /usr/local tar -zxvf /root/zhengl/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz

通用unix/linux二进制包的MySQL安装布局

Directory Contents of Directory
bin mysqld server, client and utility programs
docs MySQL manual in Info format
man Unix manual pages
include Unix manual pages
lib Libraries
share Error messages, dictionary, and SQL for database installation
support-files Miscellaneous support files

对安装目录设置符号链接,能够更容易地将其引用为/usr/local/mysql

ln -s /usr/local/mysql-5.7.23-linux-glibc2.12-x86_64/ mysql

为了避免在使用MySQL时必须输入客户端程序的路径名,将/usr/local/mysql/bin目录添加到PATH变量中(/etc/profile):

export PATH=$PATH:/usr/local/mysql/bin

5、初始化数据目录

在初始化之前先配置my.cnf文件:

 

 

创建目录/data,并修改属组为mysql

mkdir /data chown mysql:mysql /data

 

 

对于其他平台和安装类型,包括来自通用二进制和源发行版的安装,必须自己初始化数据目录。切换到MySQL安装目录,/usr/local/mysql:

cd /usr/local/mysql

 

创建一个目录,它的位置可以被提供给securefilepriv系统变量,该变量将导入/导出操作限制在特定目录下:

mkdir mysql-files

将目录的所有权授予mysql用户和组所有权给mysql组,并为目录设置正确的权限:

chown mysql:mysql mysql-files chmod 750 mysql-files

初始化数据目录,包括包含初始MySQL授权表的MySQL数据库,这些表决定了用户如何被允许连接到服务器。

通常,只有在第一次安装MySQL之后,才需要进行数据目录初始化。如果正在升级现有的安装,应该运行mysqlupgrade(参见第4.7节“mysqlupgrade——检查和升级MySQL表”)。然而,初始化数据目录的命令不会覆盖任何现存的特权表,因此在任何情况下都应该是安全的。使用服务器初始化数据目录;例如:

bin/mysqld --initialize --user=mysql
2018-08-18T05:45:58.625830Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-18T05:46:00.232167Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-18T05:46:00.495607Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-08-18T05:46:00.558159Z 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: fc94c68e-a2a9-11e8-8d6c-000c29f9898a. 2018-08-18T05:46:00.562688Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2018-08-18T05:46:00.564312Z 1 [Note] A temporary password is generated for root@localhost: aqgCrZ!yv7?a

 

注意,初始化的日志最后会输出root用户登录的初始化密码!

执行:mysqld --verbose --help可以查看帮助,如果安装目录设置的不是/usr/local/mysql(mysql的默认安装目录),需要指定--basedir参数。

参见http://blog.51cto.com/11819159/1933718

可以指定--basedir 参数设置数据目录

 

------------------------------------------------------------------------------------------------------------------

数据目录的初始化可能会失败,因为您的系统中缺少一些必需的软件库。例如:

shell> bin/mysqld --initialize --user=mysql
bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

当这种情况发生时,必须手动或在重新尝试数据目录初始化之前,手动安装缺失的库或系统的软件包管理器。

 

如果插件目录plugin directory(由plugin_dir系统变量命名的目录)是可写的,那么用户就可以使用SELECT ... INTO DUMPFILE 写入到该目录。

这可以通过将插件目录设置为只读或在服务器启动时将 secure_file_priv 系统变量设置为一个目录来避免,这样可以安全地执行SELECT写入(例如,将它设置为前面创建的mysql-files目录)

6、启动Mysql服务

bin/mysqld_safe --user=mysql &

 

[root@OS7-MASTER bin]# ps -ef|grep mysql root 99470  98595  0 14:41 pts/2    00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql     99630  99470  0 14:41 pts/2    00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql.log --pid-file=/data/mysql.pid --socket=/data/mysql.sock --port=3306
root     101160  97582  0 15:01 pts/1    00:00:00 grep --color=auto mysql

 

后台可以查看到进程mysqld_safe实际上是调用mysqld启动数据库的。

关闭mysql数据库,可以使用mysqladmin。需要在my.cnf指定[mysqladmin]节点sock文件,默认都是使用/tmp/mysql.sock。后者在加参数-S /data/mysql.sock

mysqladmin -uroot -ppassword shutdown

查看编码:

mysql> show variables like '%char%'; +--------------------------+----------------------------------------------------------------+
| Variable_name            | Value                                                          |
+--------------------------+----------------------------------------------------------------+
| character_set_client     | utf8                                                           |
| character_set_connection | utf8                                                           |
| character_set_database   | utf8                                                           |
| character_set_filesystem | binary                                                         |
| character_set_results    | utf8                                                           |
| character_set_server     | utf8                                                           |
| character_set_system     | utf8                                                           |
| character_sets_dir       | /usr/local/mysql-5.7.23-linux-glibc2.12-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
8 rows in set (0.01 sec)

 

 字符集全部设置为utf8。(原因是在my.cnf里设置了character_set_server=utf8,特别说明default-character-set参数在5.7中已过时,若在配置文件指定会导致数据库启动失败。)如果其他非utf8编码的参数,可以继续在my.cnf文件中进行设置。

7、登录重置密码

mysql -uroot -p'U0o%KqMWza63' mysql> ALTER USER 'root'@'localhost' identified by 'password'; Query OK, 0 rows affected (0.00 sec)

8、配置mysql服务

直接使用/usr/local/mysql/support-files目录下的mysql.server进行启动、停止和重启。mysql.server调用的实际上就是mysql_safe。centos7上chkconfig命令被阉割,使用systemctl进行替代。可能是为了向前兼容,依然可以通过将服务脚本添加到/etc/rc.d/init.d目录下,并通过chkconfig --add来添加服务。同时也可以使用chkconfig进行管理,以及使用service命令进行启动和停止。只是service启动和停止的mysql服务,在systemctl status查出来的状态可能不一样。systemctl status好像是不能获取到service启动的服务。建议可以按原来的方式添加服务,通过systemctl进行启停以及状态查看、设置自启动等。

将mysql.server服务脚本复制到/etc/rc.d/init.d目录下改名为mysql:

cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql

 

chkconfig添加服务

chkconfig --add mysql

 

查看是否自启动

[root@OS7-SLAVE support-files]# systemctl is-enabled mysql mysql.service is not a native service, redirecting to /sbin/chkconfig. Executing /sbin/chkconfig mysql --level=5 enabled [root@OS7-SLAVE support-files]# chkconfig --list mysql 注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。 要列出 systemd 服务,请执行 'systemctl list-unit-files'。 查看在具体 target 启用的服务请执行 'systemctl list-dependencies [target]'。 mysql 0:关    1:关    2:开    3:开    4:开    5:开    6:关

 

可以看到不论是chkconfig还是systemctl输出信息一致,默认运行级别为5,并自启动。

使用systemctl启动mysql

[root@OS7-SLAVE mysql]# systemctl start mysql [root@OS7-SLAVE mysql]# systemctl status mysql ● mysql.service - LSB: start and stop MySQL Loaded: loaded (/etc/rc.d/init.d/mysql; bad; vendor preset: disabled) Active: active (running) since 日 2018-08-19 12:25:23 CST; 7s ago Docs: man:systemd-sysv-generator(8) Process: 5132 ExecStart=/etc/rc.d/init.d/mysql start (code=exited, status=0/SUCCESS) Tasks: 28 CGroup: /system.slice/mysql.service ├─5144 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data --pid-file=/data/mysql.pid └─5345 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql.log --pid-file=/data/... 8月 19 12:25:22 OS7-SLAVE systemd[1]: Starting LSB: start and stop MySQL... 8月 19 12:25:22 OS7-SLAVE mysql[5132]: Starting MySQL.Logging to '/data/mysql.log'. 8月 19 12:25:23 OS7-SLAVE mysql[5132]: SUCCESS! 8月 19 12:25:23 OS7-SLAVE systemd[1]: Started LSB: start and stop MySQL.

 

配置主从复制

//记得关闭服务器防火墙 systemctl stop firewalld,systemctl disable firewalld或者打开3306端口的访问

1、配置master和slave端的my.cnf文件

 master端的my.cnf

[mysql]
socket=/data/mysql.sock

[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir
=/data socket=/data/mysql.sock lower_case_table_names=1 port=3306 character_set_server=utf8 pid-file=/data/mysql.pid server-id=151 log-bin=/data/binlog/master-bin log-bin-index = /data/binlog/master-bin.index max_connections=1024 innodb_buffer_pool_size=8G max_allowed_packet = 2G
skip-name-resolve

slow_query_log=1
long_query_time=3
#slow_query_log_file=/data/cloudvm01-slow.log
log_output=TABLE # Disabling symbolic
-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/data/mysql.log pid-file=/data/mysql.pid [mysqladmin] socket=/data/mysql.sock [mysqldump] quick max_allowed_packet = 2G # # include all files from the config directory # !includedir /etc/my.cnf.d

考虑到事务的绝对安全,sync-binlog参数是否需要开启,要权衡下。

注:需要手工新建二进制日志的目录binlog,并chown mysql:mysql binlog

slave端的my.cnf

[mysql]
socket=/data/mysql.sock

[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir
=/data socket=/data/mysql.sock lower_case_table_names=1 port=3306 character_set_server=utf8 pid-file=/data/mysql.pid #log-bin=mysql-bin max_connections=1024 innodb_buffer_pool_size=8G max_allowed_packet = 2G
skip-name-resolve server
-id=152 relay-log = relay-log relay-log-index = relay-log.index read_only=1 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/data/mysql.log pid-file=/data/mysql.pid [mysqladmin] socket=/data/mysql.sock [mysqldump] quick max_allowed_packet = 2G # # include all files from the config directory # !includedir /etc/my.cnf.d

 

slave端若是只读库,需要设置read_only=1,这项功能只对非管理员组以外的用户有效!

 

 

2、在master上创建账户,并授权slave

grant replication slave on *.* to 'master_rep'@'%' identified by 'password';

3、在主服务器上查看master状态

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      595 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

获取到当前二进制日志文件,以及当前位置595

3、配置152为slave从服务器,设置主服务器为151

change master to master_host='192.168.21.151',master_user='master_rep',master_password='password',master_log_file='master-bin.000001',master_log_pos=595

执行start slave,启动复制。(该步骤好像可以省略,自动启动)

 

 如果要复制部分用户或者部分表,请参见https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html,以及印象笔记的剪藏,这里面有很多坑!

 

重置从库:

stop slave;

reset slave;

change master to master_host='10.3.237.86',master_user='master_rep',master_password='password',master_log_file='master-bin.000002',master_log_pos=851197

start slave;

 

参考资料

https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

https://dev.mysql.com/doc/refman/5.7/en/postinstallation.html

https://dev.mysql.com/doc/refman/5.7/en/data-directory-initialization.html