1.下拉mysql鏡像:mysql
docker pull mysqlsql
此處選擇在主機上保存配置文件,從而不會出現docker服務重啓,配置文件須要從新配置的問題。docker
2.準備master的配置文件:數據庫
mkdir -p /usr/local/mysqlData/master/cnf
mkdir -p /usr/local/mysqlData/master/data
vim /usr/local/mysqlData/master/cnf/mysql.cnfvim
[mysqld]
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/run/mysqld/mysqld.sock
datadir=/var/lib/mysql
secure-file-priv= NULL
server-id=1
symbolic-links=0
log-bin=mysql-bin
lower_case_table_names=1
!includedir /etc/mysql/conf.d/bash
3.啓動master容器:session
docker run -itd -p 3306:3306 --name mysqlMaster -v /usr/local/mysqlData/master/cnf/mysql.cnf:/etc/mysql/my.cnf -v /usr/local/mysqlData/master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --privileged=true mysql:latest --character-set-server=utf8socket
將容器端口3306映射到外部端口3306,啓動惟一容器名爲mysqlMaster,本地目錄/usr/local/mysqlData/master/cnf和/usr/local/mysqlData/master/data均掛載到容器對應目錄上,鏈接密碼爲123456spa
4.開啓遠程訪問: rest
docker exec -it mysqlMaster bash
mysql -u root -p
123456
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | caching_sha2_password | $A$005$;Yb9H*Y ?I_B&ba}CHlwIJ731GtvTcZXlmnUxWRKicnQJLL4le1KwHW1iv2 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | caching_sha2_password | $A$005$eeFC b;CHcIdH>eZkK21qbAw8DGu7SYUxWTLkkM8GprvKkEQLyAZcH/mDA |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)plugin的值爲:caching_sha2_password 的帳戶,均須要修改密碼。
ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;
5.準備slave的配置文件:
mkdir -p /usr/local/mysqlData/slave/cnf
mkdir -p /usr/local/mysqlData/slave/data
vim /usr/local/mysqlData/slave/cnf/mysql.cnf
[mysqld]
pid-file=/var/run/mysqld/mysqld.pid
socket= /var/run/mysqld/mysqld.sock
datadir=/var/lib/mysql
secure-file-priv= NULL
server-id=2
relay_log_recovery=0
symbolic-links=0
log-bin=mysql-bin
lower_case_table_names=1
!includedir /etc/mysql/conf.d/
6.啓動slave容器:
docker run -d -p 3307:3306 --name mysqlSlave -v /usr/local/mysqlData/slave/cnf/mysql.cnf:/etc/mysql/my.cnf -v /usr/local/mysqlData/slave/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --privileged=true mysql:latest --character-set-server=utf8
7.開啓遠程訪問:
docker exec -it mysqlSlave bash
mysql -u root -p
123456
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | caching_sha2_password | $A$005$;Yb9H*Y ?I_B&ba}CHlwIJ731GtvTcZXlmnUxWRKicnQJLL4le1KwHW1iv2 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | caching_sha2_password | $A$005$eeFC b;CHcIdH>eZkK21qbAw8DGu7SYUxWTLkkM8GprvKkEQLyAZcH/mDA |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)plugin的值爲:caching_sha2_password 的帳戶,均須要修改密碼。
ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;
8.查看Master的信息:
鏈接上Master數據庫,查詢:
show master status;
9.進入slave數據庫,設置對應的master:
change master to master_host='192.168.106.128',
master_user='root',
master_password='123456',
master_port=3306;
10.開啓主從模式:
start slave;
查看slave狀態:
show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.106.128
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000003
Read_Master_Log_Pos: 155
Relay_Log_File: 00642958d650-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: binlog.000003
Slave_IO_Running: 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: 155
Relay_Log_Space: 534
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: 409f26fe-d9bd-11e8-9dc8-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
1 row in set (0.00 sec)
11.完成。
如今,在master的CRUD操做,都會同步到slave上面。
須要注意:每次從新啓動docker,都須要:先分別啓動master和slave容器
重啓docker容器:
docker restart mysqlMaster;
docker restart mysqlSlave;
注意:
show slave status\G
\G後面不能再加分號,由於\G在功能上等同於分號;
若是加了分號,那麼就是2個分號,就會出現SQL語法錯誤。