- 使用docker安装mysql
docker pull mysql:8.0
- 先run一个master的mysql容器
##master节点
docker run --name master \
> -e MYSQL_ROOT_PASSWORD=root \
> -v /你自己到路径/master.cnf:/etc/mysql/my.cnf \
> -d -i \
> -p 3306:3306 --restart=always mysql:8.0
##slave节点
docker run --name slave \
> -e MYSQL_ROOT_PASSWORD=root \
> -v /你自己到路径/slave.cnf:/etc/mysql/my.cnf \
> -d -i \
> -p 3307:3306 --restart=always mysql:8.0
以上参数的含义:
–name mysql
将容器命名为mysql,后面可以用这个name进行容器的启动暂停等操作
-e MYSQL_ROOT_PASSWORD=root 设置MySQL密码为root
-d 此容器在后台运行,并且返回容器的ID
-i 以交互模式运行容器
-p 进行端口映射,格式为主机(宿主)端口:容器端口
-v /你自己到路径/master.cnf 这里是把你宿主机到mysql 配置 映射到 容器里
–restart=always 当docker重启时,该容器自动重启
3. 然后更改从节点的server-id
更改 slave.cnf文件
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id = 2 //这里是新加的
# Custom config should go here
!includedir /etc/mysql/conf.d/
- 先在主mysql容器里创建一个用户用来读取信息
docker exec -it master bash #进入容器
mysql -uroot -p
Enter password:
//输入密码
CREATE USER `reader`@`%` IDENTIFIED WITH mysql_native_password BY 'reader';
GRANT Replication Slave ON *.* TO `reader`@`%`;
//查看mysql的binlog
show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 | 654 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
- 进到slave到mysql设置同步
change master to master_host='192.168.50.130',master_user='root',master_password='root',master_log_file='binlog.000002',master_log_pos=0;
//master_log_file 这里是你主库到binlog名
//master_log_pos 从多少行同步
//master_host//这里的ip是你本地的ip
- 查看是否同步成功
show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.50.179
Master_User: reader
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000002
Read_Master_Log_Pos: 654
Relay_Log_File: 40f1b26b074b-relay-bin.000003
Relay_Log_Pos: 319
Relay_Master_Log_File: binlog.000002
Slave_IO_Running: Yes //这里是yes和下面也是yes就正确
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 654
Relay_Log_Space: 1241
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 26a899a5-2796-11ea-9184-0242ac110002
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
Slave_IO_Running ,Slave_SQL_Runnin这两个参数都是yes则正确 反之则根据错误信息检查