如題,今天來模擬下簡單的MySQL主從同步模型的搭建。正式開始之前,先確保已經安裝了docker。
準備素材
拉鏡像
docker pull mysql
起一個容器,待會要從裏面拿到原始的配置文件
docker run -d --name mysql_origin mysql
拿到配置文件
docker ps -a # 拿到對應的container-id
docker inspect container-id | grep IP # 拿到此容器的IP地址,一般第一個都是172.17.0.2
docker cp container-id:/etc/mysql ./ # 把容器中的mysql配置文件 取出來 待會改改就可以用了
主從配置
經過上一步, 已經把測試需要的內容拿到了,接下來就分別對master和slave做下配置。
我這裏的目錄結構如下:
➜ mysql tree .
.
├── master # master 配置,從mysql_origin內容修改而來
│ ├── conf.d
│ │ ├── docker.cnf
│ │ └── mysql.cnf
│ └── my.cnf
├── mysql # 剛纔從mysql_origin容器中拷到的
│ ├── conf.d
│ │ ├── docker.cnf
│ │ └── mysql.cnf
│ ├── my.cnf
│ └── my.cnf.fallback
└── slave # slave 配置,從mysql_origin內容修改而來
├── conf.d
│ ├── docker.cnf
│ └── mysql.cnf
└── my.cnf
6 directories, 10 files
master配置
修改配置文件
➜ mysql cat master/my.cnf
# Copyright (c) 2017, 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 Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/
server-id = 100
log-bin = mysql-bin
然後把容器跑起來:
docker run -d -p 6666:3306 -e MYSQL_ROOT_PASSWORD=123456 -v $PWD/master:/etc/mysql --name mysql_master mysql
創建一個用於同步的賬號,否則slave沒辦法從master這裏通過權限校驗,也就拿不到數據了。這裏創建一個slave賬號。
CREATE USER 'slave'@'172.17.0.%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave'@'172.17.0.%';FLUSH PRIVILEGES;
slave配置
修改下slave的配置,對比master的內容,變化就再最後面幾行。
➜ mysql cat slave/my.cnf
# Copyright (c) 2017, 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 Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/
server-id = 101
log-bin = mysql-slave-bin
relay_log = edu-mysql-relay-bin # 可選項,這個想加就加,不想加就算了。
然後把slave也跑起來:
docker run -d -p 7777:3306 -e MYSQL_ROOT_PASSWORD=123456 -v $PWD/slave:/etc/mysql --name mysql_slave mysql
設置slave選項,大致的結構如下:
change master to
master_host='172.17.0.2',
master_user='slave',
master_password='123456',
master_port=3306,
master_log_file='mysql-bin.000003',
master_log_pos=1753,
master_connect_retry=30;
需要注意的是這個配置中的兩個部分:
master_log_file
master_log_pos
這倆是主從同步的關鍵,具體的內容來自於在master中使用
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 1753 | | | |
+------------------+----------+--------------+------------------+-------------------+
同步操作
接下來正式操作了,在slave命令行中輸入:
start slave;
然後觀察下slave的status,如果是下面的:
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
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.000003
Read_Master_Log_Pos: 1753
Relay_Log_File: edu-mysql-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql-bin.000003
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: 1753
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: 100
Master_UUID: 2022420e-7ac6-11e9-a12e-0242ac110002
Master_Info_File: mysql.slave_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:
Master_public_key_path:
Get_master_public_key: 0
1 row in set (0.00 sec)
如果Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes 說明基本上成功了,可以在master上創建庫,創建表,然後再slave上去查看是否同步過來了。
如果不是這樣,可能就會存在問題。具體的調試可以從Last_IO_Error 入手,可以從具體的文案進行處理。
問題彙總
在模擬的過程中,遇到了一些問題,最多的就是
Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 30 retries: 10
參考鏈接中的文章也都描述過此類問題,基本上就是:
- 網絡不通:可以ping或者在slave上用mysql-client 去連master等來測試
- pos或者log_file寫錯了: 先在master上show master status; 再去slave上填寫對應的change master …
- slave賬號的密碼不對: 一般是在master上創建slave賬戶的時候密碼出問題了,強制使用native格式的密碼可以解決這個問題,如我開頭寫的。最後記得flush privileges;刷新下grant了的權限信息。
總結
模擬的過程本身就是一個學習的過程,從中可以瞭解到一種模式下的思想。上面列舉的知識點也只是我個人遇到的一些小問題的總結,談不上什麼高深的內容,網上也都搜的到,我只是個知識的搬運工,能讓後來人少踩點坑就夠了。
anyway,enjoy it.