docker實現mysql主從複製

docker實現mysql主從複製

1. 下載mysql docker image

docker pull mysql

參考: 鏡像地址html

2. 建立一個mysql容器

docker run -t --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD='123456' -d  mysql

3. 實現主從複製

  • 建立主mysql服務器
docker run -t --name mysql-master -p 3307:3306 -e MYSQL_ROOT_PASSWORD='123456' -d  mysql
  • 建立從mysql服務器
docker run -t --name mysql-slave -p 3308:3306 -e MYSQL_ROOT_PASSWORD='123456' -d  mysql

4. mysql實現主從複製原理

經過binary logging來實現複製mysql

4.1 在哪配置?

/etc/mysql/conf.d/git

4.2 配置什麼?

/etc/mysql/conf.d/目錄下添加*.cnf文件(名字隨便取)github

  • 主服務器配置(master.cnf)
[mysqld]
log-bin=mysql-bin 
server-id=1
  • 從服務器(slave.cnf)
[mysqld]
#log-bin=mysql-bin #不必啓用 binary logging除非 從服務器 要做爲其餘服務器的 主服務器
server-id=2

4.3 docker如何將上面的配置加入到以前建立的mysql-mastermysql-slave

直接將文件複製到容器的相應木目錄下,而後重啓容器sql

docker cp master.cnf mysql-master:/etc/mysql/my.cnf 
docker cp slave.cnf mysql-master:/etc/mysql/my.cnf

4.4 主服務器建立一個具備複製權相的用戶

  • docker進入mysql命令行
docker exec -it mysql-master mysql -uroot -p123456
  • 建立複製權限用戶並賦予相關權限
CREATE USER 'repl'@'%' IDENTIFIED BY '123456';       -- '%'意味着全部的終端均可以用這個用戶登陸
GRANT SELECT,REPLICATION SLAVE ON *.* TO 'repl'@'%'; -- SELECT權限是爲了讓repl能夠讀取到數據,生產環境建議建立另外一個用戶
  • 從服務器鏈接到主服務器
docker exec -it mysql-slave mysql -uroot -p123456
CHANGE MASTER TO MASTER_HOST='192.168.99.100',MASTER_PORT=3308, MASTER_USER='repl', MASTER_PASSWORD='123456';
START SLAVE;
SHOW SLAVE STATUS\G       -- \G用來代替";",能把查詢結果按鍵值的方式顯示

因爲剛開始沒有配置主服務器的端口致使以下錯誤運行結果docker

mysql> mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.99.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File:
          Read_Master_Log_Pos: 4
               Relay_Log_File: d0f565d3c84f-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File:
             Slave_IO_Running: Connecting
            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: 0
              Relay_Log_Space: 154
              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: 2003
                Last_IO_Error: error connecting to master 'repl@192.168.99.100:3306' - retry-time: 60  retries: 2
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
                  Master_UUID:
             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: 170622 08:50:54
     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)

正確運行結果數據庫

mysql> change master to
    -> master_host='192.168.99.100'
    -> master_port=3308
    -> master_user='repl';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'master_port=3308
master_user='repl'' at line 3
mysql> change master to master_host='192.168.99.100',master_port=3308,master_user='repl',master_passwrod='123456';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'master_passwrod='123456'' at line 1
mysql> change master to master_host='192.168.99.100',master_port=3308,master_user='repl',master_password='123456';
Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql> start slave
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.99.100
                  Master_User: repl
                  Master_Port: 3308
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 313
               Relay_Log_File: d0f565d3c84f-relay-bin.000002
                Relay_Log_Pos: 526
        Relay_Master_Log_File: mysql-bin.000001
             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: 313
              Relay_Log_Space: 740
              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: 2b3c5dee-5722-11e7-8ddb-0242ac110003
             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)

5. 測試

mysql-master執行以下操做服務器

CREATE DATABASE test;

查看mysql-slave中也會建立相關的數據庫,至此,主從備份搞定。測試

參考官網命令行

相關文章
相關標籤/搜索