docker mysql 主從複製

操做過程

  • 主服務(Master)配置服務id、開啓二進制日誌功能(bin-log)
  • 主服務建立用戶(用來同步數據)
  • 從服務(Slave)配置服務id
  • 從服務配置master的用戶信息、日誌信息,開啓同步(start slave)

安裝docker

菜鳥教程html

拉取mysql鏡像

$ docker pull mysql:5.7

建立主從服務容器

mysql主服務 端口3307,密碼rootmysql

$ docker run -p 3307:3306 --name mysql_master -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7

mysql從服務 端口3308,密碼rootsql

$ docker run -p 3308:3306 --name mysql_slave -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7

配置主服務

進入主服務容器docker

$ docker exec -it a00792eaeeb4 /bin/bash

找到配置文件目錄數據庫

$ cd /etc/mysql/mysql.conf.d

安裝vimmacos

docker容器安裝vimvim

修改配置bash

$ vim mysqld.cnf
[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
#log-error	= /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address	= 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#新添加的配置
server-id	= 1 
log-bin		= mysql-bin
binlog-format	= mixed

保存後重啓mysql服務、重啓容器socket

$ service mysql restart
$ docker restart a00792eaeeb4

建立一個同步複製的用戶slave,密碼爲123456,授予用戶slave REPLICATION SLAVE權限和REPLICATION CLIENT權限,該用戶用於在主從庫之間同步數據。測試

mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';

查看配置狀態

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      435 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

記下這裏的位置:435,在下面數據同步時要用到

配置從服務

一樣進入容器,編輯/etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
#log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#新加的內容
server-id       = 2

保存後重啓容器

$ docker restart 2061a7aaa728

先查詢Master服務的ip地址

$ docker inspect --format='{{.NetworkSettings.IPAddress}}' a00792eaeeb4
172.17.0.2

而後進入Slave服務,開始配置數據同步

$ docker exec -it 2061a7aaa728 /bin/bash
$ mysql -uroot -proot

先中止從服務的slave,

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

拿到從上面Master的位置435,執行:

mysql> change master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos= 435;

而後再啓動slave,開啓同步

mysql> start slave;

解釋

CHANGE MASTER TO 
	MASTER_HOST='172.17.0.2',//主服務的IP地址
	master_port=3306,//主服務的服務端口
	MASTER_USER='slave',//主服務用戶
	MASTER_PASSWORD='123456',//主服務用戶密碼
	MASTER_LOG_FILE='mysql-bin.000001',//日誌文件
	MASTER_LOG_POS=435;//日誌位置

這個時候,查看SLave服務的slave狀態:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.0.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 435
               Relay_Log_File: dbc647dfde50-relay-bin.000002
                Relay_Log_Pos: 515
        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: 435
              Relay_Log_Space: 729
              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: 1af39b3e-13d2-11e9-ae89-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)

若是顯示 Slave_IO_Running: Yes,Slave_SQL_Running: Yes,那麼說明配置成功。

若是失敗,可能緣由是:

  • ip地址不對,若是用docker的話,Master的host不是localhost或127.0.0.0,而須要經過docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器id來查詢ip地址
  • 端口不對,這裏端口是mysql服務的端口3306,而不是docker對外訪問的端口3307
  • 密碼錯誤,這裏的用戶名密碼是Master服務上新建立的,專門作數據同步的用戶,而不是Slave的用戶
  • 日誌文件名或位置不對,這裏須要在Master服務執行show master status;來獲取日誌名和位置

測試主從同步

在Master服務新建數據庫test

CREATE DATABASE `test` DEFAULT CHARACTER SET = `utf8mb4`;

而後查看Slave服務

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

在Master服務建個表user

CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

查看從服務Slave:

mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user           |
+----------------+
1 row in set (0.00 sec)

在Master新加數據,查看Slave

注意

若是在Master服務增刪改數據,都會同步到Slave。可是若是在Slave修改數據後,沒法同步到Master,而且日誌文件會錯亂(Slave除非從新設置日誌文件位置),致使數據沒法完成同步。因此通常Master服務給讀寫權限,而Slave只須要讀權限,禁止寫入數據。

相關文章
相關標籤/搜索