MySQL互為主從數(shù)據(jù)同步部署操作方式
在MySQL數(shù)據(jù)庫(kù)管理中,實(shí)現(xiàn)互為主從的配置是一種常見的高可用性和數(shù)據(jù)冗余策略。
這種配置允許兩個(gè)MySQL服務(wù)器相互復(fù)制數(shù)據(jù),每個(gè)服務(wù)器既是對(duì)方的主服務(wù)器也是從服務(wù)器。
這樣,無(wú)論哪個(gè)服務(wù)器發(fā)生故障,另一個(gè)都可以接管,保證數(shù)據(jù)的持續(xù)可用性和一致性。
一、環(huán)境準(zhǔn)備
- 準(zhǔn)備兩臺(tái)虛擬機(jī)(192.168.4.51 192.168.4.52)
- 關(guān)閉防火墻和selinux
- 確保兩臺(tái)虛擬機(jī)網(wǎng)絡(luò)互通
- 兩臺(tái)虛擬機(jī)都安裝了MySQL(5.7及以上的版本)
二、配置互為主從
1.編輯/etc/my.cnf文件
vim /etc/my.cnf
在[mysqld]模塊下添加以下代碼段
[mysqld] server-id=51 #標(biāo)識(shí) log_bin=/var/lib/mysql/mysql_log_bin #開啟binlog并指定路徑 binlog_format=mixed #binlog 格式(mixed 兼容 row 和 statement) log_slave_updates #開啟主從 auto_increment_increment = 2 #主鍵自增量 auto_increment_offset = 1 #主鍵起始偏量
兩臺(tái)虛擬機(jī)操作一樣 只是主鍵起始偏量不同為了避免主鍵相同而讓其錯(cuò)開
讓51主機(jī)的主鍵成 1 , 3 ,5 ,7…
而52主機(jī)的主鍵成 2 , 4, 6, 8…
vim /etc/my.cnf [mysqld] server-id=52 #標(biāo)識(shí) log_bin=/var/lib/mysql/mysql_log_bin #開啟binlog并指定路徑 binlog_format=mixed #binlog 格式(mixed 兼容 row 和 statement) log_slave_updates #開啟主從 auto_increment_increment = 2 #主鍵自增量 auto_increment_offset = 2 #主鍵起始偏量
重啟MySQL服務(wù)
[root@host51 ~]# systemctl restart mysqld
2.創(chuàng)建授權(quán)用戶
兩臺(tái)虛擬機(jī)操作一樣
mysql -uroot -p你的密碼
創(chuàng)建連接用戶 mysql> create user 'repl'@'%' identified by '123qqq...A'; Query OK, 0 rows affected (0.00 sec) 授權(quán)連接用戶 mysql> grant replication slave on *.* to 'repl'@'%'; Query OK, 0 rows affected (0.00 sec) 刷新權(quán)限 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
3.查看binlog日志信息
查看192.168.4.51
mysql> show master status; +----------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------------+----------+--------------+------------------+-------------------+ | mysql_log_bin.000004 | 1007 | | | | +----------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.01 sec)
查看192.168.4.52
mysql> show master status
-> ;
+----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| mysql_log_bin.000004 | 1007 | | | |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
4.配置主從
首先在51主機(jī)上配置(52為主機(jī))
mysql> change master to master_host='192.168.4.52', #設(shè)置主的IP地址 master_user='repl', #設(shè)置連接用戶名 master_password='123qqq...A', #設(shè)置連接用戶密碼 master_log_file='mysql_log_bin.000004', #設(shè)置binlog文件名 master_log_pos=1007; #設(shè)置binlog 同步位置 Query OK, 0 rows affected, 2 warnings (0.00 sec)
其次再在52主機(jī)上配置(51為主機(jī))
mysql> change master to master_host='192.168.4.51', master_user='repl', master_password='123qqq...A', master_log_file='mysql_log_bin.000004', master_log_pos=1007; Query OK, 0 rows affected, 2 warnings (0.01 sec)
啟動(dòng)并查看
mysql> start slave ;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G ;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.52
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_log_bin.000004
Read_Master_Log_Pos: 1007
Relay_Log_File: host51-relay-bin.000003
Relay_Log_Pos: 324
Relay_Master_Log_File: mysql_log_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: 1007
Relay_Log_Space: 702
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: 52
Master_UUID: 888f4a92-acd3-11f0-8b2a-000c29a32fbd
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都為yes即為同步成功
- Slave_IO_Running: Yes – IO 線程正常(負(fù)責(zé)拉取主庫(kù)binlog)
- Slave_SQL_Running: Yes– SQL 線程正常(負(fù)責(zé)執(zhí)行同步的 SQL)
三、同步測(cè)試
在51主機(jī)上創(chuàng)建庫(kù)表并插入數(shù)據(jù)
mysql> create database db1; Query OK, 1 row affected (0.00 sec) mysql> create table db1.user(id int primary key auto_increments); Query OK, 0 rows affected (0.01 sec) mysql> insert into db1.user values(1); Query OK, 1 row affected (0.00 sec)
在52主機(jī)上查詢是否數(shù)據(jù)同步
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> select * from db1.user; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec)
然后再在52主機(jī)上創(chuàng)建庫(kù)表并插入數(shù)據(jù)
mysql> create database db2; Query OK, 1 row affected (0.00 sec) mysql> create table db2.user(id int primary key auto_increments); Query OK, 0 rows affected (0.01 sec) mysql> insert into db2.user values(2); Query OK, 1 row affected (0.00 sec)
在51主機(jī)上查詢是否數(shù)據(jù)同步
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | db2 | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec) mysql> select * from db2.user; +------+ | id | +------+ | 2 | +------+ 1 row in set (0.00 sec)
可看到兩次數(shù)據(jù)的同步都成功了
至此互為主從配置成功數(shù)據(jù)同步成功!
總結(jié)
通過以上步驟我們完成了MySQL的互為主從配置并實(shí)現(xiàn)數(shù)據(jù)同步。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL數(shù)據(jù)庫(kù)事務(wù)隔離級(jí)別詳解
這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)事務(wù)隔離級(jí)別詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
mysql5.7 設(shè)置遠(yuǎn)程訪問的實(shí)現(xiàn)
這篇文章主要介紹了mysql5.7 設(shè)置遠(yuǎn)程訪問的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
教你如何在?MySQL?數(shù)據(jù)庫(kù)中支持完整的Unicode
UTF-8?是一種可變寬度編碼,它使用一到四個(gè)?8?位字節(jié)對(duì)每個(gè)符號(hào)進(jìn)行編碼,永遠(yuǎn)不要在MySQL中使用?utf8——總是使用?utf8mb4,對(duì)mysql支持?Unicode相關(guān)知識(shí)感興趣的朋友一起看看吧2023-01-01
Ubuntu 18.04配置mysql以及配置遠(yuǎn)程連接的步驟
這篇文章主要給大家介紹了關(guān)于Ubuntu 18.04配置mysql以及配置遠(yuǎn)程連接的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
mysql實(shí)用技巧之比較兩個(gè)表是否有不同數(shù)據(jù)的方法分析
這篇文章主要介紹了mysql實(shí)用技巧之比較兩個(gè)表是否有不同數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了mysql數(shù)據(jù)表比較的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-12-12
CentOS 7下MySQL服務(wù)啟動(dòng)失敗的快速解決方法
CentOS 7下MySQL服務(wù)啟動(dòng)失敗怎么辦?下面小編就為大家?guī)?lái)一篇CentOS 7下MySQL服務(wù)啟動(dòng)失敗的快速解決方法?,F(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-03-03

