基于Docker MySQL的主从复制

 

Docker MySQL 实现主从复制(1 Master + 2 Slave)


  1.要求:
      3个mysql版本一致
      初始化表,并在后台启动mysql
      修改root密码
     
修改配置文件:
启动一个docker mysql 容器,并进入,复制其配置文件 /etc/mysql/my.cnf到本地,做为基础配配置   html

docker run --name db1 -p 30000:3306 -d -e MYSQL_ROOT_PASSWORD='root' mysql:5.6
docker exec -it 757b /bin/bash
cat /etc/mysql/my.cnf

拷贝内容到 /home/jayhe/jay/config/mysql/master_2slave/my-m.cnf 做为基础配置,下面的修改都是基于这个基础配置
            
  2.修改主服务器master:
      my.cnfmysql

[mysqld]
#[必须]启动二进制日志
log-bin=mysql-bin
#[必须]设置服务器惟一ID,默认时1,通常取IP最后一段
server-id=3001 


  3.修改从服务器slave:
      my.cnf redis

[mysqld]
#[非必须]启动二进制日志
log-bin=mysql-bin
#[必须]设置服务器惟一ID,默认时1,通常取IP最后一段
server-id=3002
      

my.cnfsql

[mysqld]
#[非必须]启动二进制日志
log-bin=mysql-bin
#[必须]设置服务器惟一ID,默认时1,通常取IP最后一段
server-id=3003

启动3个mysql:
主服务器:挂载   /xx/xx/my.cnf(主服务器配置)到mysql容器docker

docker run --name mysql1 -p 3001:3306 -d -v /home/jayhe/jay/config/mysql/master_2slave/my-m.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD='root' mysql:5.6

从服务器:挂载   /xx/xx/my.cnf(从服务器配置)到mysql容器数据库

docker run --name mysql2 -p 3002:3306 -d -v /home/jayhe/jay/config/mysql/master_2slave/my-s1.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD='root' mysql:5.6
docker run --name mysql3 -p 3003:3306 -d -v /home/jayhe/jay/config/mysql/master_2slave/my-s2.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD='root' mysql:5.6

链接容器中的mysql   ---  即便本地mysql每启动,仍然能够链接容器的mysql服务
语法:安全

mysql -h IP -P port -uroot -proot

 

##链接主服务器
mysql -h 127.0.0.1 -P 3001 -uroot -proot     
##登陆主服务器,建立帐户并受权
grant replication slave on *.* to 'mysync'@'%' identified by 'root';
##查看主服务器状态
show master status;


+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      312 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
  登陆从服务器:bash

 mysql -h 127.0.0.1 -P 3002 -uroot -proot

配置从服务器:  
---  这里的 master_log_file='mysql-bin.000004', master_log_pos与主服务器中的master status一致服务器

change master to master_host='172.17.64.106',master_port=3001,master_user='mysync',master_password='root',master_log_file='mysql-bin.000004', master_log_pos=312;

启动从服务器复制功能less

start slave;

查看从服务器复制功能状态:
     

   show slave status;

 

        +----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+

| Slave_IO_State                   | Master_Host   | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File          | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                                     | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position |
+----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
| Waiting for master to send event | 172.17.64.106 | mysync      |        3001 |            60 | mysql-bin.000004 |                 409 | mysqld-relay-bin.000002 |           380 | mysql-bin.000004      | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 409 |             554 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |             3001 | 735f9147-d3ec-11e6-b8c6-0242ac120002 | /var/lib/mysql/master.info |         0 |                NULL | Slave has read all relay log; waiting for the slave I/O thread to update it |              86400 |             |                         |                          |                |                    |                    |                   |             0 |
+----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+

注意:
   Slave_IO_State  =  Waiting for master to send event
   Slave_IO和Slave_SQL 都是 YES
   则表示主从复制配置成功
  测试主从复制:
        进入主服务器,建立一个数据库
       mysql -h 127.0.0.1 -P 3001 -uroot -proot
       create database test1;
        进入从服务器,查看数据库,发现已经存在了test1   --- 与主服务器同步
            mysql -h 127.0.0.1 -P 3001 -uroot -proot
            mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test1              |
+--------------------+
            
特别注意:
1.主服务器中,建立帐户并受权
grant replication slave on *.* to 'mysync'@'%' identified by 'root';
通常不用root帐户,
 *.* 表示全部客户端均可连,只要帐户和密码正确,这里可用具体IP代替,增强安全 eg: 192.168.145.222
          
2.从服务器中,配置从服务器时:
master_host 为docker的宿主机地址,    不能写  127.0.0.1
master_port 为主服务器mysql映射到宿主机的端口  3001
master_user 为主服务器中建立的用户            mysync
master_log_file 为主服务器show master status查询出来的 File
master_log_pos 为主服务器show master status查询出来的 Position      
               
3.配置文件其余内容配置项:
#若是须要增长Slave库则,此id日后顺延;  
server-id = 2    
log-bin=mysql-bin  
#主库host  
master-host = 192.168.168.253  
#在主数据库服务器中创建的用于该从服务器备份使用的用户  
master-user = forslave  
master-password = ******  
master-port = 3306  
#若是发现主服务器断线,从新链接的时间差;  
master-connect-retry=60  
#不须要备份的数据库;   
replicate-ignore-db=mysql  
#须要备份的数据库  
replicate-do-db=minishop  
log-slave-update 
         
            

 4.从MySQL镜像容器中获取的 my.cnf 基础配置 

# Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.  
#  
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; version 2 of the License.  
#  
# This program is distributed in the hope that it will be useful,  
# but WITHOUT ANY WARRANTY; without even the implied warranty of  
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
# GNU General Public License for more details.  
#  
# You should have received a copy of the GNU General Public License  
# along with this program; if not, write to the Free Software  
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA  
  
#  
# The MySQL Community Server configuration file.  
#  
# For explanations see  
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html  
  
[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=3001  
# 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/  
相关文章
相关标签/搜索