基於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/  
相關文章
相關標籤/搜索