Mysql主從複製的實現原理圖大致如下:
MySQL之間數據複製的基礎是以二進制日誌文件(binary log file)來實現的,一臺MySQL數據庫一旦啓用二進制日誌後,其作爲master,它數據庫中所有操作都會以“事件”的方式記錄在二進制日誌中,其他數據庫作爲slave通過一個I/O線程與主服務器保持通信,並監控master的二進制日誌文件的變化,如果發現master二進制日誌文件發生變化,則會把變化複製到自己的中繼日誌中,然後slave的一個SQL線程會把相關的“事件”執行到自己的數據庫中,以此實現從數據庫和主數據庫的一致性,也就實現了主從複製。MySQL(MariaDB)具體詳細的安裝可以參考《Linux就該這麼學》教程的第十八章節,裏面內容寫的非常詳細,適合初學者,本文也比較適合企業應用。
主服務器:1、開啓數據庫二進制日誌功能;2、配置數據庫認證唯一服務id;3、獲得主庫的二進制日誌文件名及位置;4、在主庫上面創建一個用於主庫和從庫通信的用戶賬號,安全管理。
從服務器:1、在從庫中配置唯一服務id;2、使用主庫創建分配的用戶賬號讀取主庫的二進制日誌;3、啓用slave功能,用於主從通信。
2.主從數據庫內數據保持一致;
主數據庫(master):192.168.3.91 /CentOS Linux release 7.5.1804 (Core)
從數據庫( slave ) :192.168.3.218 /CentOS Linux release 7.5.1804 (Core)
注意:這裏的主從都是通過yum源安裝的mariadb 5.5.56;
# yum install mariadb-server.x86_64 mariadb.x86_64 -y
# systemctl start mariadb.service && systemctl enable mariadb.service
//設置mariadb數據庫root賬號的密碼,默認root用戶是沒有密碼;
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Password updated successfully!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
Remove anonymous users? [Y/n] y
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] n
Reloading the privilege tables will ensure that all changes made so far
Reload privilege tables now? [Y/n] y
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
找到主數據庫的配置文件my.cnf(或者my.ini),我的在/etc/my.cnf,在[mysqld]部分插入如下兩行:
[mysqld]log-bin=mysql-bin #開啓二進制日誌 server-id=1 #設置server-id
log-bin="/var/lib/mysql/" #設定生成的log文件名;
# systemctl restart mariadb.service
# mysql -hlocalhost -uroot -ppassword
MariaDB [(none)]> CREATE USER 'wxp'@'192.168.3.218' IDENTIFIED BY 'password';#創建用戶
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'wxp'@'192.168.3.218';#分配權限
MariaDB [(none)]>flush privileges; #刷新權限
3.查看master狀態,記錄二進制文件名(mysql-bin.000001)和位置(492):
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 492 | | |
+------------------+----------+--------------+------------------+
[mysqld]server-id=2 #設置server-id,必須唯一
log-bin="/var/lib/mysql/" #設定生成的log文件名;
# systemctl restart mariadb.service
2.重啓mysql,打開mysql會話,執行同步SQL語句(需要主服務器主機名,登陸憑據,二進制文件的名稱和位置):
# mysql -hlocalhost -uroot -ppassword
MariaDB [(none)]> CHANGE MASTER TO -> MASTER_HOST='192.168.3.91', -> MASTER_USER='wxp', -> MASTER_PASSWORD='password', -> MASTER_LOG_FILE='mysql-bin.000001', -> MASTER_LOG_POS=492;
mysql> select * from mysql.slave_master_info \G
MariaDB [(none)]> show slave status\G;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Log_File: mysql-bin.000001
Relay_Log_File: mariadb-relay-bin.000002
Relay_Master_Log_File: mysql-bin.000001
Master_SSL_Verify_Server_Cert: No
當Slave_IO_Running和Slave_SQL_Running都爲YES的時候就表示主從同步設置成功了。接下來就可以進行一些驗證了,比如在主master數據庫的test數據庫的一張表中插入一條數據,在slave的test庫的相同數據表中查看是否有新增的數據即可驗證主從複製功能是否有效,還可以關閉slave(MariaDB [(none)]>stop slave;),然後再修改master,看slave是否也相應修改(停止slave後,master的修改不會同步到slave),就可以完成主從複製功能的驗證了。
MariaDB [test]> create table t1(Name varchar(18));
Query OK, 0 rows affected (0.03 sec)
MariaDB [test]> insert into t1(Name) values('wxp');
Query OK, 1 row affected (0.01 sec)
MariaDB [test]> select * from t1;
[root@backup-3-218 ~]# mysql -hlocalhost -uroot -ppassword
MariaDB [test]> select * from t1;
master開啓二進制日誌後默認記錄所有庫所有表的操作,可以通過配置來指定只記錄指定的數據庫甚至指定的表的操作,具體在mysql配置文件的[mysqld]可添加修改如下選項:
# vim /etc/my.cnf binlog-ignore-db = mysql binlog-ignore-db = test binlog-ignore-db = information_schema
# systemctl restart mariadb.service
# 只同步哪些數據庫,除此之外,其他不同步 binlog-do-db = wxp
mysql> change master to master_connect_retry=50;