mysql的主从配置
修改my.cnf文件
-
!includedir /etc/mysql/conf.d/ server-id=201 log-bin=mysql-bin binlog_format=row [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL default_authentication_plugin=mysql_native_password # Custom config should go here !includedir /etc/mysql/conf.d/ server-id=200 log-bin=mysql-bin binlog_format=row
master中创建slave账号操作从服务
进入容器后输入以下两个命令
cp /etc/skel/.bashrc /root/
cp /etc/skel/.bash_profile /root/
退出容器后再次进入
bash-4.4# cp /etc/skel/.bashrc /root/ bash-4.4# cp /etc/skel/.bash_profile /root/ bash-4.4# exit exit [root@localhost 3306]# docker exec -it mysql_3306 bash [root@ed5404afb218 /]#
[root@localhost conf]# docker exec -it mysql_master bash
root@6c7b6078f4ec:/# mysql -u root -p
Enter password:
mysql> create user 'slave'@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> use mysql
mysql> ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123';
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
create user 'slave'@'%' identified by '123';
mysql> grant replication slave, replication client on *.* to 'slave'@'%';
alter user 'slave'@'%' identified with mysql_native_password by '123';
flush privileges;
由于mysql8.x的密码加密方式改变,如果之后主从出现问题,需要修改密码加密方式,如果之前在mysql_3306中设置过的则可以跳过此处。
进入mysql的主服务器:
[root@localhost ~]# docker exec -it mysql_3306 bash
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123';
显示mysql_3306的主机状态
show master status;
-
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 157 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
docker inspect 容器名称 |grep IPA
[root@localhost conf]# docker inspect mysql_master |grep IPA
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAMConfig": null,
"IPAddress": "172.17.0.2",
序列 | 名称 | 值 |
---|---|---|
1 | 日志文件:file | mysql-bin.000001 |
2 | 日志文件位置:Position | 157 |
3 | 主服务器的ip | 172.17.0.2 |
mysql从服务器
change master to master_host='172.17.0.2',master_user='slave',master_password='123',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=157;
mysql> show slave status \G;
主从配置顺序
- 创建mysql master(3306) 容器
- 进入master(3306) docker exec -it mysql_3306 bash
- 显示master状态: show master status; —-记住 mysql-bin , position
- 退出容器
- 创建mysql_3310容器,注意挂载点的文件
- 进入slave(3310) docker exec -it mysql_3310 bash
- mysql -uroot -p123
- stop slave;
- reset slave;
- change master to master_host=’172.17.0.2’,master_user=’slave’,master_password=’123’,MASTER_LOG_FILE=’mysql-bin.000001’,MASTER_LOG_POS=157;
- start slave;