docker安装运行单实例的MySQL参考另一篇文档 http://www.cnblogs.com/manger/p/7611309.html
1、首先在/data/script下创建两个文件my-m.cnf(主库配置) 、my-s.cnf(从库配置)
my-m.cnf 内容如下
[client] port = 3306 socket = /var/run/mysqld/mysqld.sock [mysqld_safe] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock nice = 0 [mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp lc-messages-dir = /usr/share/mysql explicit_defaults_for_timestamp log-bin = mysql-bin server-id = 1 # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. #bind-address = 127.0.0.1 #log-error = /var/log/mysql/error.log # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # * IMPORTANT: Additional settings that can override those from this file! # The files must end with '.cnf', otherwise they'll be ignored. # !includedir /etc/mysql/conf.d/
主要是这两行,只需要在原来的配置里面加上就行
log-bin = mysql-bin
server-id = 1
my-s.cnf内容如下
[client] port = 3306 socket = /var/run/mysqld/mysqld.sock [mysqld_safe] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock nice = 0 [mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp lc-messages-dir = /usr/share/mysql explicit_defaults_for_timestamp log-bin = mysql-bin server-id = 2 # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. #bind-address = 127.0.0.1 #log-error = /var/log/mysql/error.log # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # * IMPORTANT: Additional settings that can override those from this file! # The files must end with '.cnf', otherwise they'll be ignored. # !includedir /etc/mysql/conf.d/
log-bin = mysql-bin
server-id = 2
2、OK,有了配置文件,就可以启动mysql了,先启动主库
docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-master -v /data/script/my-m.cnf:/etc/mysql/my.cnf -p 3307:3306 mysql
3、启动从库
docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-slave -v /data/script/my-s.cnf:/etc/mysql/my.cnf -p 3308:3306 mysql
4、连接主库,并运行以下命令,创建一个用户用来同步数据
想要访问docker的mysql数据库,我的机器上需要装一个mysql-client。
yum install mysql -y
mysql -h192.168.1.20 -P3307 -uroot -p123456
mysql> GRANT REPLICATION SLAVE ON *.* to 'repl'@'%' identified by 'repl';
5、查看主库状态
mysql> show master status;
mysql> show master status;
记住File、Position的值,如果没查到数据,请检查第一、第二步,配置问题。
我查出来的是mysql-bin.000003、940
6、连接到从库,运行以下命令,设置主库链接
mysql -h192.168.1.20 -P3308 -uroot -p123456
mysql> change master to master_host='192.168.1.20',master_user='repl',master_password='repl',
master_log_file='mysql-bin.000003',master_log_pos=940,master_port=3307;
7、启动同步
mysql> start slave;
8、查看同步状态
mysql> show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.20
Master_User: repl
Master_Port: 3307
Connect_Retry: 10
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 940
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.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: 940
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: 42aa1711-a46e-11e7-945d-0242ac110002
Master_Info_File: /var/lib/mysql/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:
1 row in set (0.00 sec)
如果看到2个yes,说明主从搭建成功