Mysql主从复制搭建

找到cnf文件

root@7103a3767cad:/# which mysqld
/usr/sbin/mysqld
ns’t@7103a3767cad:/# /usr/sbin/mysqld --verbose --help |grep -A 1 'Default option
2019-05-29 05:55:59 0 [Note] /usr/sbin/mysqld (mysqld 5.6.43) starting as process 98 …
2019-05-29 05:55:59 98 [Note] Plugin ‘FEDERATED’ is disabled.
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
2019-05-29 05:55:59 98 [Note] Binlog end
2019-05-29 05:55:59 98 [Note] Shutting down plugin ‘MyISAM’
2019-05-29 05:55:59 98 [Note] Shutting down plugin ‘CSV’

修改主库配置文件

my.cnf

[mysqld]
server-id = 301
log_bin = master-bin

重启mysql

systemctl restart mysqld

建立帐户并授权slave

GRANT FILE ON . TO ‘backup’@’%’ IDENTIFIED BY ‘123456’;
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON . to ‘backup’@’%’ identified by ‘123456’;
FLUSH PRIVILEGES;

查看用户

mysql> select user,host from mysql.user;
±-------±-------------+
| user | host |
±-------±-------------+
| backup | % |
| root | % |
| zmall | % |
| root | 127.0.0.1 |
| root | ::1 |
| | e887569c716d |
| root | e887569c716d |
| | localhost |
| root | localhost |
±-------±-------------+
9 rows in set (0.00 sec)

查看mater状态

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

修改从库配置文件

my.cnf

[mysqld]
server-id = 302

重启mysql

systemctl restart mysql

配置

change master to master_host=‘172.16.22.119’,master_port=3301,master_user=‘backup’,master_password=‘123456’,master_log_file=‘master-bin.000001’,master_log_pos=606;

start slave;

检查

在从服务起上运行sql
show slave status;
在这里插入图片描述
表示成功

如果出现:
在这里插入图片描述

查看日志:tail /var/log/mysqld.log
[ERROR] Slave I/O: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs;

因为是docker容器导致auto.cnf中的uuid重复:
解决:
[root@82b7123af546 /]# find . -name ‘auto.cnf’
./var/lib/mysql/auto.cnf
[root@82b7123af546 /]# cd /var/lib/mysql/
[root@82b7123af546 mysql]# cat auto.cnf
[auto]
server-uuid=e94fdf0e-665d-11e8-b90a-0242ac110002
[root@82b7123af546 mysql]# mv auto.cnf auto.cnf.bak
[root@82b7123af546 mysql]# systemctl restart mysql
[root@82b7123af546 mysql]# cat auto.cnf
[auto]
server-uuid=7f0d8b6f-5450-11e9-88a8-0242ac110003

建议设为双向互为主备复制

恢复数据库后 slave状态异常处理

在这里插入图片描述
无法从主库复制

注意如果是dockers的mysql,一定要再mysql容器里配置my.cnf 而不能用映射的方式,否则会导致无法找到bin文件!

你可能感兴趣的:(中间件)