溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Mysql數(shù)據(jù)庫(kù)主從復(fù)制的實(shí)現(xiàn)原理

發(fā)布時(shí)間:2021-08-26 17:01:42 來(lái)源:億速云 閱讀:215 作者:chen 欄目:MySQL數(shù)據(jù)庫(kù)

這篇文章主要介紹“Mysql數(shù)據(jù)庫(kù)主從復(fù)制的實(shí)現(xiàn)原理”,在日常操作中,相信很多人在Mysql數(shù)據(jù)庫(kù)主從復(fù)制的實(shí)現(xiàn)原理問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Mysql數(shù)據(jù)庫(kù)主從復(fù)制的實(shí)現(xiàn)原理”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

Mysql主從復(fù)制的實(shí)現(xiàn)原理圖大致如下:

 Mysql數(shù)據(jù)庫(kù)主從復(fù)制的實(shí)現(xiàn)原理

MySQL之間數(shù)據(jù)復(fù)制的基礎(chǔ)是以二進(jìn)制日志文件(binary log file)來(lái)實(shí)現(xiàn)的,一臺(tái)MySQL數(shù)據(jù)庫(kù)一旦啟用二進(jìn)制日志后,其作為master,它數(shù)據(jù)庫(kù)中所有操作都會(huì)以“事件”的方式記錄在二進(jìn)制日志中,其他數(shù)據(jù)庫(kù)作為slave通過(guò)一個(gè)I/O線(xiàn)程與主服務(wù)器保持通信,并監(jiān)控master的二進(jìn)制日志文件的變化,如果發(fā)現(xiàn)master二進(jìn)制日志文件發(fā)生變化,則會(huì)把變化復(fù)制到自己的中繼日志中,然后slave的一個(gè)SQL線(xiàn)程會(huì)把相關(guān)的“事件”執(zhí)行到自己的數(shù)據(jù)庫(kù)中,以此實(shí)現(xiàn)從數(shù)據(jù)庫(kù)和主數(shù)據(jù)庫(kù)的一致性,也就實(shí)現(xiàn)了主從復(fù)制。

實(shí)現(xiàn)MySQL主從復(fù)制配置要求

主服務(wù)器:1、開(kāi)啟數(shù)據(jù)庫(kù)二進(jìn)制日志功能;2、配置數(shù)據(jù)庫(kù)認(rèn)證唯一服務(wù)id;3、獲得主庫(kù)的二進(jìn)制日志文件名及位置;4、在主庫(kù)上面創(chuàng)建一個(gè)用于主庫(kù)和從庫(kù)通信的用戶(hù)賬號(hào) ,安全管理。

從服務(wù)器: 1、在從庫(kù)中配置唯一 服務(wù)id;2、使用主庫(kù)創(chuàng)建分配的用戶(hù)賬號(hào)讀取主庫(kù)的二進(jìn)制日志 ;3、啟用slave功能,用于主從通信。

一、準(zhǔn)備工作:

1.主從數(shù)據(jù)庫(kù)版本最好一致;

2.主從數(shù)據(jù)庫(kù)內(nèi)數(shù)據(jù)保持一致;

主數(shù)據(jù)庫(kù)(master):192.168.3.91    /CentOS Linux release 7.5.1804 (Core)

從數(shù)據(jù)庫(kù)( slave ) :192.168.3.218   /CentOS Linux release 7.5.1804 (Core)

注意:這里的主從都是通過(guò)yum源安裝的mariadb 5.5.56;

# yum install mariadb-server.x86_64 mariadb.x86_64  -y

//設(shè)置mariadb服務(wù)

# systemctl start mariadb.service &&  systemctl enable mariadb.service

//設(shè)置mariadb數(shù)據(jù)庫(kù)root賬號(hào)的密碼,默認(rèn)root用戶(hù)是沒(méi)有密碼;

# mysql_secure_installation

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.

Set root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

 ... Success!

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

production environment.

Remove anonymous users? [Y/n] 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? [Y/n] n

 ... skipping.

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

 ... skipping.

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

Reload privilege tables now? [Y/n] y

 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

Thanks for using MariaDB!

二、主數(shù)據(jù)庫(kù)master修改:

1.修改mysql配置

找到主數(shù)據(jù)庫(kù)的配置文件my.cnf(或者my.ini),我的在/etc/my.cnf,在[mysqld]部分插入如下兩行:

# find / -name my.cnf

默認(rèn)配置

[mysqld]log-bin=mysql-bin #開(kāi)啟二進(jìn)制日志 server-id=1 #設(shè)置server-id

 log-bin="/var/lib/mysql/" #設(shè)定生成的log文件名; 

修改后:

# systemctl restart mariadb.service

2.重啟mysql,創(chuàng)建用于同步的用戶(hù)賬號(hào)

# mysql -hlocalhost -uroot -ppassword

創(chuàng)建用戶(hù)并授權(quán):用戶(hù):wxp,密碼:password

MariaDB [(none)]> CREATE USER 'wxp'@'192.168.3.218' IDENTIFIED BY 'password';#創(chuàng)建用戶(hù)

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'wxp'@'192.168.3.218';#分配權(quán)限

 MariaDB [(none)]>flush privileges;    #刷新權(quán)限

3.查看master狀態(tài),記錄二進(jìn)制文件名(mysql-bin.000001)和位置(492):

MariaDB [(none)]> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000001 |      492 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

二、從服務(wù)器slave修改:

1.修改mysql配置

同樣找到my.cnf配置文件,添加server-id

# find / -name my.cnf

my.cnf默認(rèn)配置

[mysqld]server-id=2            #設(shè)置server-id,必須唯一

 log-bin="/var/lib/mysql/" #設(shè)定生成的log文件名; 

修改后:

# systemctl restart mariadb.service

2.重啟mysql,打開(kāi)mysql會(huì)話(huà),執(zhí)行同步SQL語(yǔ)句(需要主服務(wù)器主機(jī)名,登陸憑據(jù),二進(jìn)制文件的名稱(chēng)和位置):

# 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;

這里是直接把信息寫(xiě)入到數(shù)據(jù)庫(kù)里面,

mysql> select * from mysql.slave_master_info \G

3.啟動(dòng)slave同步進(jìn)程:

MariaDB [(none)]>start slave;

4.查看slave狀態(tài):

MariaDB [(none)]> show slave status\G;

MariaDB [(none)]> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.3.91

                  Master_User: wxp

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 492

               Relay_Log_File: mariadb-relay-bin.000002

                Relay_Log_Pos: 529

        Relay_Master_Log_File: mysql-bin.000001

             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: 492

              Relay_Log_Space: 825

              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

1 row in set (0.00 sec)

ERROR: No query specified

當(dāng)Slave_IO_Running和Slave_SQL_Running都為YES的時(shí)候就表示主從同步設(shè)置成功了。接下來(lái)就可以進(jìn)行一些驗(yàn)證了,比如在主master數(shù)據(jù)庫(kù)的test數(shù)據(jù)庫(kù)的一張表中插入一條數(shù)據(jù),在slave的test庫(kù)的相同數(shù)據(jù)表中查看是否有新增的數(shù)據(jù)即可驗(yàn)證主從復(fù)制功能是否有效,還可以關(guān)閉slave(MariaDB [(none)]>stop slave;),然后再修改master,看slave是否也相應(yīng)修改(停止slave后,master的修改不會(huì)同步到slave),就可以完成主從復(fù)制功能的驗(yàn)證了。

5、測(cè)試,操作Master數(shù)據(jù)庫(kù)

MariaDB [(none)]> use test;

Database changed

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;

+------+

| Name |

+------+

| wxp  |

+------+

1 row in set (0.00 sec)

在slave上面查看test庫(kù)是否有數(shù)據(jù)同步過(guò)來(lái);

[root@backup-3-218 ~]# mysql -hlocalhost -uroot -ppassword

MariaDB [(none)]> use test;

MariaDB [test]> show tables;

+----------------+

| Tables_in_test |

+----------------+

| t1             |

+----------------+

1 row in set (0.00 sec)

MariaDB [test]> select * from t1;

+------+

| Name |

+------+

| wxp  |

+------+

1 row in set (0.00 sec)

6、還可以用到的其他相關(guān)參數(shù):

master開(kāi)啟二進(jìn)制日志后默認(rèn)記錄所有庫(kù)所有表的操作,可以通過(guò)配置來(lái)指定只記錄指定的數(shù)據(jù)庫(kù)甚至指定的表的操作,具體在mysql配置文件的[mysqld]可添加修改如下選項(xiàng):

# 不同步哪些數(shù)據(jù)庫(kù) 

# vim /etc/my.cnf binlog-ignore-db = mysql   binlog-ignore-db = test   binlog-ignore-db = information_schema  

# systemctl restart mariadb.service

   # 只同步哪些數(shù)據(jù)庫(kù),除此之外,其他不同步  binlog-do-db = wxp

# 日志保留時(shí)間

expire_logs_days = 10

# 控制binlog的寫(xiě)入頻率。每執(zhí)行多少次事務(wù)寫(xiě)入一次

# 這個(gè)參數(shù)性能消耗很大,但可減小MySQL崩潰造成的損失

sync_binlog = 5

# 日志格式,建議mixed

# statement 保存SQL語(yǔ)句

# row 保存影響記錄數(shù)據(jù)

# mixed 前面兩種的結(jié)合

binlog_format = mixed

在slave數(shù)據(jù)庫(kù)上面操作,設(shè)置重新連接超時(shí)時(shí)間

# 停止主從同步

mysql> stop slave;

# 連接斷開(kāi)時(shí),重新連接超時(shí)時(shí)間

mysql> change master to master_connect_retry=50;

# 開(kāi)啟主從同步

mysql> start slave;

到此,關(guān)于“Mysql數(shù)據(jù)庫(kù)主從復(fù)制的實(shí)現(xiàn)原理”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI