docker配置mysql主從實現讀寫分離

1.獲取mysql鏡像

docker pull mysql:5.7

2.建立工做目錄

cd ~
mkdir docker-mysql-config

3.建立主從mysql配置文件

  • 建立主mysql配置文件
vim ~/docker-mysql-config/master.cnf

複製以下配置:html

# Copyright (c) 2014, 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 = 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/
  • 建立從mysql配置文件
vim ~/docker-mysql-config/slave01.cnf

複製主mysql配置文件,注意: 
將server-id = 1 修改成server-id = 2mysql

4.運行主myqsl容器

docker run -d -e MYSQL_ROOT_PASSWORD=123 --name mysql-master -v ~/docker-mysql-config/master.cnf:/etc/mysql/my.cnf -p 3306:3306 mysql:5.7

5.運行從mysql容器

docker run -d -e MYSQL_ROOT_PASSWORD=123 --name mysql-slave01 -v ~/docker-mysql-config/slave01.cnf:/etc/mysql/my.cnf -p 3307:3306 mysql:5.7

6.主從服務配置

  • 終端1:主mysql容器
docker exec -it mysql-master bash
mysql -uroot -p123

mysql>
  • 終端2:從mysql容器
docker exec -it mysql-slave01 bash
mysql -uroot -p123

mysql>
  • 主mysql容器配置
mysql> grant replication slave on *.* to 'slave01'@'%' identified by 'slave01';

mysql> flush privileges;

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      1160 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • 從mysql容器配置
mysql> change master to master_host='192.168.99.100',
    master_user='slave01',
    master_password='slave01',
    master_log_file='mysql-bin.000004',
    master_log_pos=0,         ## 從master什麼記錄位置開始讀取,從0開始讀取能夠複製master整個庫表
    master_port=3306;

mysql> start 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: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1160
               Relay_Log_File: 3dc37943fb7a-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000004
             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: 1160
              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: 39f6cd9a-f870-11e7-b14e-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)

只要一下兩個爲Yes,表明配置完成redis

Slave_IO_Running: Yessql

Slave_SQL_Running: Yesdocker

若Slave_SQL_Running爲No,表示執行SQL出錯,由於slave數據庫沒有mater數據庫的庫和表結構,須要把master和slave同步一下後,執行一下命令數據庫

mysql> stop slave ;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;    #客戶端運行,用來跳過幾個事件,只有當同步進程出現錯誤而中止的時候才能夠執行。
mysql> start slave ;
相關文章
相關標籤/搜索