您好,登錄后才能下訂單哦!
MySQL的主從復(fù)制是什么?怎么實(shí)現(xiàn)MySQL服務(wù)器的主從同步?這些問(wèn)題可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到的。通過(guò)這些問(wèn)題,希望你能收獲更多。下面是揭開(kāi)這些問(wèn)題的詳細(xì)內(nèi)容。
MySQL主從復(fù)制
案例概述
在企業(yè)網(wǎng)站中,后端MySQL數(shù)據(jù)庫(kù)只有一臺(tái)時(shí),會(huì)有以下問(wèn)題:
[root@master ~]# yum install ntp -y
[root@slave1 ~]# yum install ntp ntpdate -y
[root@slave2 ~]# yum install ntp ntpdate -y
修改ntp配置文件
在主服務(wù)器下設(shè)置ntp配置文件,然后開(kāi)啟ntpd,關(guān)閉防火墻
[root@master ~]# vim /etc/ntp.conf
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
‘server 127.127.247.0 //設(shè)置本地是時(shí)鐘源,這里的127.127指自己的192.168
’fudge 127.127.247.0 stratum 8 //設(shè)置時(shí)間環(huán)的時(shí)間層級(jí)(時(shí)間環(huán))為8
[root@master ~]# systemctl start ntpd
[root@master ~]# systemctl stop firewalld
[root@master ~]# setenforce 0
[root@master ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
配置從服務(wù)器slave1,直接開(kāi)啟ntpd服務(wù),進(jìn)行時(shí)間同步,去匹配主服務(wù)器(ip地址)時(shí)間
[root@slave1 ~]# systemctl start ntpd
[root@slave1 ~]# systemctl stop firewalld
[root@slave1 ~]# setenforce 0
[root@slave1 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@slave1 ~]# /usr/sbin/ntpdate 192.168.247.160
8 Jan 18:39:26 ntpdate[114393]: the NTP socket is in use, exiting
slave1同步完成,接下來(lái)同步slave2
[root@slave2 ~]# systemctl start ntpd
[root@slave2 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@slave2 ~]# systemctl stop firewalld
[root@slave2 ~]# setenforce 0
[root@slave2 ~]# /usr/sbin/ntpdate 192.168.247.160
8 Jan 18:40:38 ntpdate[82655]: the NTP socket is in use, exiting
接下來(lái)就是安裝MySQL和優(yōu)化的過(guò)程
由于過(guò)程冗雜,詳細(xì)過(guò)程請(qǐng)查看本人之前博客:
https://blog.51cto.com/14557905/2458283
安裝mysql完畢,并且做完一系列優(yōu)化
開(kāi)始做主從同步
1.修改主服務(wù)器配置文件
[root@master mysql-5.6.26]# vim /etc/my.cnf
//寫在mysqld下
log-bin=master-bin
//上面是開(kāi)啟二進(jìn)制文件
log-slave-update=true
//開(kāi)啟從服務(wù)器更新
server-id = 11
//服務(wù)器id為11(id不可以重復(fù))
重啟服務(wù)
[root@master mysql-5.6.26]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
主服務(wù)器登陸mysql,給從服務(wù)器創(chuàng)建用戶并且允許復(fù)制所有數(shù)據(jù)(ON . )
[root@master mysql-5.6.26]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.247.%' IDENTIFIED BY 'abc123';
//允許 復(fù)制,從服務(wù)器以myslave用戶的身份,從192.168.247.0的網(wǎng)段,使用abc123的密碼,去復(fù)制所有的數(shù)據(jù)庫(kù)以及下面的表(*.*)
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
//刷新數(shù)據(jù)庫(kù)
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
//查看主服務(wù)器的位置點(diǎn),從服務(wù)器的同步位置點(diǎn)就是下面的412
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 412 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置從服務(wù)器slave1,服務(wù)器id不能一致,開(kāi)啟中繼日志,索引中繼日志
[root@slave1 mysql-5.6.26]# vim /etc/my.cnf
[mysqld]
//寫在mysqld下
log-bin=mysql-bin
server-id = 22
//另一臺(tái)slave2 id 為23
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
[root@slave1 mysql-5.6.26]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
從服務(wù)器登陸mysql,添加主服務(wù)器(ip地址,使用主服務(wù)器的賬戶mysalve,輸入主服務(wù)器賬戶的密碼,確定同步的二進(jìn)制文件,同步的位置點(diǎn))
[root@slave1 mysql-5.6.26]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> change master to master_host='192.168.247.160',master_user='myslave',master_password='abc123',master_log_file='master-bin.000001',master_log_pos=412;
//添加主服務(wù)器
Query OK, 0 rows affected, 2 warnings (0.01 sec)
開(kāi)啟從服務(wù)器功能
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
查看從服務(wù)器狀態(tài)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.247.160
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 412
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 284
Relay_Master_Log_File: master-bin.000001
‘ Slave_IO_Running: Yes //顯示slave功能已開(kāi)啟
‘ Slave_SQL_Running: Yes //顯示slave功能已開(kāi)啟
Exec_Master_Log_Pos: 412
Relay_Log_Space: 455
Master_Server_Id: 11
Master_UUID: e9a82741-3223-11ea-af25-000c29524d89
Master_Info_File: /home/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
1 row in set (0.00 sec)
同步配置完成,接下來(lái)測(cè)試
主服務(wù)器,創(chuàng)建一個(gè)school數(shù)據(jù)庫(kù)
mysql> create database school;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
5 rows in set (0.00 sec)
從服務(wù)器直接查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
5 rows in set (0.00 sec)
以上就是MySQL的主從復(fù)制介紹以及MySQL服務(wù)器實(shí)現(xiàn)主從同步的方法,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎ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)容。