日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

mysql主从复制、基于GTID的主从、半同步

發布時間:2025/3/19 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql主从复制、基于GTID的主从、半同步 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用的mysql版本5.7.17

一、主從復制

原理:

  • 主從復制一共有三個進程,從庫生成兩個線程,一個I/O線程,一個SQL線程;
  • i/o線程去請求主庫的binlog,并將得到的binlog日志寫到relay log(中繼日志) 文件中;
  • 主庫會生成一個 log dump 線程,用來給從庫 i/o線程傳binlog;
  • SQL 線程,會讀取relay log文件中的日志,并解析成具體操作,來實現主從的操作一致,而最終數據一致

實驗環境:

server1:master

server2:slave

  • 使用pos號實現主從復制

實驗操作:

  • server1
    安裝所需要的安裝包
[root@server1 ~]# ls mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm mysql-community-common-5.7.17-1.el6.x86_64.rpm mysql-community-server-5.7.17-1.el6.x86_64.rpm mysql-community-libs-5.7.17-1.el6.x86_64.rpm [root@server1 ~]# yum install -y *
  • server2
    安裝所需要的安裝包
[root@server2 ~]# ls mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm mysql-community-common-5.7.17-1.el6.x86_64.rpm mysql-community-server-5.7.17-1.el6.x86_64.rpm mysql-community-libs-5.7.17-1.el6.x86_64.rpm [root@server2 ~]# yum install -y *
  • 配置master
[root@server1 ~]# vim /etc/my.cnf [root@server1 ~]# cat /etc/my.cnf | tail -n 2 server-id=1 //這個server-id可以任意,但是必須是唯一的 //這里server1的設為1, sevrre2的設為2 log-bin=mysql-bin //日志名稱 [root@server1 ~]# /etc/init.d/mysqld start //mysql的開啟 Initializing MySQL database: [ OK ] Installing validate password plugin: [ OK ] Starting mysqld: [ OK ] [root@server1 ~]# grep password /var/log/mysqld.log //過濾查看mysql的初始密碼(其他過濾出來的內容就不顯示了) 2018-08-08T08:51:19.321424Z 1 [Note] A temporary password is generated for root@localhost: u/Klif/g3sFi [root@server1 ~]# mysql_secure_installation //進行初始化Securing the MySQL server deployment.Enter password for user root: The existing password for the user account root has expired. Please set a new password.New password: //輸入用戶密碼8位以上,并且是字母大小寫+特殊字符+數字Re-enter new password: The 'validate_password' plugin is installed on the server. The subsequent steps will run with the existing configuration of the plugin. Using existing password for root.Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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 production environment.Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success.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? (Press y|Y for Yes, any other key for No) : y Success.By default, MySQL 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? (Press y|Y for Yes, any other key for No) : ... skipping. Reloading the privilege tables will ensure that all changes made so far will take effect immediately.Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success.All done! [root@server1 ~]# mysql -p Enter password: //新密碼登陸即可 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.7.17-log MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)mysql> grant replication slave on *.* to repl@'172.25.54.%' identified by '5820hhXM!@#'; //創建同步帳戶,并給予權限 Query OK, 0 rows affected, 1 warning (0.04 sec)mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000003 | 843 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)mysql>
server2登陸測試
[root@server2 ~]# mysql -u repl -p -h 172.25.54.1 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 5.7.17-log MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> quit Bye
  • 配置slave
[root@server2 ~]# vim /etc/my.cnf [root@server2 ~]# cat /etc/my.cnf | tail -n 1 server-id=2 //只需服務id即可 [root@server2 ~]# /etc/init.d/mysqld start Starting mysqld: [ OK ] [root@server2 ~]# grep password /var/log/mysqld.log 2018-08-08T08:52:10.646512Z 1 [Note] A temporary password is generated for root@localhost: Ddp=y*gOr0uq [root@server2 ~]# mysql_secure_installation //初始化過程同server1 [root@server2 ~]# mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.7.17 MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)mysql> change master to master_host='172.25.54.1',master_user='repl',master_password='5820hhXM!@#',master_log_file='mysql-bin.000003',master_log_pos=843; //與master建立認證, 所有信息需要與master的一樣 //master主機ip,被授權的用戶,密碼,日志文件,pos號 Query OK, 0 rows affected, 2 warnings (0.99 sec)mysql> start slave; Query OK, 0 rows affected (0.07 sec)mysql> show slave status\G *************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.25.54.1Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000003Read_Master_Log_Pos: 843Relay_Log_File: server2-relay-bin.000002Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000003Slave_IO_Running: Yes //yes代表I/O線程沒問題Slave_SQL_Running: Yes //yes代表SOL線程沒問題Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 843Relay_Log_Space: 529Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: 37ab8283-9ae8-11e8-ba69-525400d23507Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)mysql>
測試數據同步
在master上進行數據庫操作
mysql> create database test; Query OK, 1 row affected (0.14 sec)mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec)mysql> use test; Database changed mysql> create table userlist (-> username varchar(15) not null,-> password varchar(25) not null); Query OK, 0 rows affected (0.66 sec)mysql> desc userlist; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | username | varchar(15) | NO | | NULL | | | password | varchar(25) | NO | | NULL | | +----------+-------------+------+-----+---------+-------+ 2 rows in set (0.02 sec)mysql> insert into userlist values ('user1','147'); Query OK, 1 row affected (0.07 sec)mysql> insert into userlist values ('user2','258'); Query OK, 1 row affected (0.10 sec)mysql> insert into userlist values ('user3','369'); Query OK, 1 row affected (0.06 sec)mysql> quit Bye [root@server1 ~]#
在slave上邊驗證
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec)mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed mysql> select * from userlist; +----------+----------+ | username | password | +----------+----------+ | user1 | 147 | | user2 | 258 | | user3 | 369 | +----------+----------+ 3 rows in set (0.00 sec)mysql> quit Bye [root@server2 ~]#
  • 基于gtid實現主從復制

  • 配置master
[root@server1 ~]# vim /etc/my.cnf [root@server1 ~]# cat /etc/my.cnf | tail -n 4 server-id=1 log-bin=mysql-bin gtid_mode=ON //開啟GTID enforce-gtid-consistency=true [root@server1 ~]# /etc/init.d/mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ] [root@server1 ~]#
  • 配置slave
[root@server2 ~]# vim /etc/my.cnf [root@server2 ~]# cat /etc/my.cnf | tail -n 3 server-id=2 gtid_mode=ON enforce-gtid-consistency=true [root@server2 ~]# /etc/init.d/mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ] [root@server2 ~]# mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.17 MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> stop slave; //停止slave工作 Query OK, 0 rows affected (0.03 sec)mysql> change master to master_host='172.25.54.1',master_user='repl',master_password='5820hhXM!@#',MASTER_AUTO_POSITION = 1; //使用gtid重新與master建立認證 Query OK, 0 rows affected, 2 warnings (0.52 sec)mysql> start slave; Query OK, 0 rows affected (0.03 sec)mysql> show slave status\G *************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.25.54.1Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000004Read_Master_Log_Pos: 154Relay_Log_File: server2-relay-bin.000002Relay_Log_Pos: 367Relay_Master_Log_File: mysql-bin.000004Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 576Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: 37ab8283-9ae8-11e8-ba69-525400d23507Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 1Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)mysql>
測試
在master上進行數據庫操作
mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | userlist | +----------------+ 1 row in set (0.00 sec)mysql> delete from userlist where username='user3'; Query OK, 1 row affected (0.12 sec)mysql> Bye [root@server1 ~]#
在slave上邊驗證
mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed mysql> select * from userlist; +----------+----------+ | username | password | +----------+----------+ | user1 | 147 | | user2 | 258 | +----------+----------+ 2 rows in set (0.00 sec)mysql> Bye [root@server2 ~]#

二、半同步復制——基于GTID

簡介

  • 默認情況下,MySQL的復制功能是異步的,異步復制可以提供最佳的性能, 主庫把binlog日志發送給從庫,這一動作就結束了,并不會驗證從庫是否接收完畢,這一過程,也就意味著有可能出現當主服務器或從服務器端發生故障的時候,有可能從服務器沒有接收到主服務器發送過來的binlog日志,這就會造成主服務器和從服務器的數據不一致,甚至在恢復時造成數據的丟失
  • 為了解決上述可能發生的錯誤,MySQL 5.5 引入了一種半同步復制模式。該模式可以確保從服務器接收完主服務器發送的binlog日志文件并寫入到自己的中繼日志relay log里,然后會給主服務器一個反饋,告訴主服務器已經接收完畢,這時主服務線程才返回給當前session告知操作完成。
  • 當出現超時情況是,主服務器會暫時切換到異步復制模式,直到至少有一個從服務器從及時收到信息為止
  • 中繼日志的自我修復:
    從MySQL 5.5.X 版本開始,增加了relay_log_recovery參數,這個參數的作用是:當slave從庫宕機后,假如relay.log損壞了,導致一部分中繼日志沒有處理,則自動放棄所有未執行的relay-log,并且重新從master上獲取日志,這樣就保證了relay-log的完整性。默認情況下該功能是關閉的,將relay_log_recovery的值設置為1時,可在slave從庫上開啟該功能,建議開啟
  • 半同步復制與異步復制的切換:
    半同步復制的工作原理就是當slave從庫IO_Thread線程將binlog日志接收完畢之后,要給master主庫一個確認,如果rpl_semi_sync_master_timeout=10000 (10秒)超過10秒未收到slave從庫的接受確認信號,那么就會自動切換為傳統的異步復制模式

注意:
半同步復制模式必須在主服務器和從服務器端同時開啟,否則主服務器默認使用異步復制模式

實驗環境:

server1:master

server2:slave

實驗在主從復制實現了的基礎上進行

實驗操作:

  • master、slave
    mysql> show variables like ‘have_dynamic_loading’;
    確保value為YES
  • master 安裝插件
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so'; Query OK, 0 rows affected (0.20 sec)mysql> set global rpl_semi_sync_master_enabled=ON; Query OK, 0 rows affected (0.00 sec)

  • slave 安裝插件
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so'; Query OK, 0 rows affected (0.05 sec)mysql> set global rpl_semi_sync_slave_enabled=ON; Query OK, 0 rows affected (0.00 sec)

測試

首先在master上查看以下參數:

然后在slave上關閉io_thread
mysql> stop slave io_thread; Query OK, 0 rows affected (0.01 sec)
接著在master上執行數據庫操作,這里是對test庫的表進行插入操作

此時插入數據會有一個10s的timeout,會有卡頓,查看參數,發現有些數值發生了變化

此時在slave上開啟io_thread,并查看數據庫,發現數據同步了

再進行一次測試
slave

master


slave

總結

以上是生活随笔為你收集整理的mysql主从复制、基于GTID的主从、半同步的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。