Mysql5.7.22 主从配置
Mysql5.7.22 主从配置
使用lnmp脚本搭建环境,默认mysql位于 /usr/local/mysql 目录下,默认配置文件为 /etc/my.cnf
Master配置
开启binlog,即my.cnf配置的[mysqld]部分下面有如下配置:
log-bin=mysql-bin binlog_format=mixed server-id = 1 expire_logs_days = 10
此处为lnmp脚本默认执行时开启的binlog,具体binlog开启还有更多配置,包括类型,缓存,过滤等。因不想重启master mysql服务器,此处不做修改
为slave服务器新建用户并授权(可指定slave服务器ip,增强安全性):
``` grant replication slave on *.* to 'slave'@'1.1.1.1' identified by '*******'; flush privileges; ```
查看master服务器的状态,查看结果后面配置slave服务器需用到
show master status;
查询结果参考如下:
mysql> show master status; +------------------+-----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+-----------+--------------+------------------+-------------------+ | mysql-bin.000005 | 455142607 | | | | +------------------+-----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
全量备份需要主从同步的数据库
mysqldump -u root -p --databases db_test_ms > /tmp/db_test_ms.sql
Slave配置
binlog部分配置特别注意server-id与master需不同
log-bin=mysql-bin binlog_format=mixed server-id = 101 expire_logs_days = 10
登录slave服务器,并导入需同步的表数据(先复制master服务器的 */tmp/*db_test_ms.sql 到当前服务器的相同地址)
# comezc @ localhost in ~ $ mysql -h127.0.0.1 -uroot -P3308 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database db_test_ms; Query OK, 1 row affected (0.00 sec) mysql> use db_test_ms; Database changed mysql> source /tmp/db_test_ms.sql ...some output...
连接主服务器,并设置仅同步 db_test_ms数据库
master_log_file和master_log_pos读取自master服务器上执行*show master status*之后的结果里的 File和Position,务必在执行这行命令之前去master服务器查询这两个参数
mysql> change master to -> master_host='1.1.1.1', -> master_port=3306, -> master_user='slave', -> master_password='*****', -> master_log_file='mysql-bin.000001', -> master_log_pos=123456; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> change replication filter replicate_do_db=(db_test_ms); Query OK, 0 rows affected (0.00 sec)
查看slave是否运行正常,如下两个显示Yes即为正常
mysql> show slave status\G; ...... ...... Slave_IO_Running: Yes Slave_SQL_Running: Yes ...... ......