Centos7預設使用的是MariaDB,要想安裝Mysql,需要先解除安裝掉MariaDB,防止衝突。
1:安裝mysql資料庫
規劃:
主:server-1---10.64.5.167
從:server-2---10.64.5.170
從:server-3---10.64.5.172
資料目錄:/var/lib/mysql
新增yum源:
1
2
|
#wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm #rpm -ivh mysql-community-release-el7-5.noarch.rpm |
安裝包:
1
|
#yum install mysql |
mysql-community-common-5.6.27-2.el7.x86_64
mysql-community-libs-5.6.27-2.el7.x86_64
mysql-community-server-5.6.27-2.el7.x86_64
mysql-community-client-5.6.27-2.el7.x86_64
自啟動:
1
|
#systemctl enable mysqld |
2:配置mysql主庫
主:server-1 10.64.5.167
(1)配置建立需要同步的資料庫cattle。
1
2
3
4
|
#mysql mysql>CREATEDATABASE IF NOT EXISTS cattle COLLATE= 'utf8_general_ci' CHARACTER SET= 'utf8' ; mysql>GRANT ALL ON cattle.*TO 'cattle' @ '%' IDENTIFIED BY 'cattle' ; mysql>GRANT ALL ON cattle.*TO 'cattle' @ 'localhost' IDENTIFIED BY 'cattle' ; |
(2)建立使用者
1
2
3
|
mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO slave1@ '10.64.5.170' IDENTIFIED BY '123456' ; mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO slave2@ '10.64.5.172' IDENTIFIED BY '123456' ; mysql> flush privileges; |
(3)配置檔案
1
2
3
4
5
6
|
#vim /etc/my.cnf 新增 server- id = 1 log-bin=mysql-bin log-slave-updates binlog- do -db=cattle binlog-ignore-db=mysql |
重啟mysql
(4)鎖主庫表
1
|
mysql> FLUSH TABLES WITH READ LOCK; |
(4)顯示主庫資訊
1
|
mysql> SHOW MASTER STATUS; |
(5)另開一個終端,打包主庫
1
2
|
#cd /var/lib/mysql #tar czvf cattle.tar.gz cattle |
(6)解鎖主庫表
1
|
mysql> UNLOCK TABLES; |
3:配置mysql從庫
從:server-2 10.64.5.170
從:server-3 10.64.5.172
(1)將cattle.tar.gz 傳輸到slav機器
1
2
3
|
#mv cattle.tar.gz /var/lib/mysql/ #cd /var/lib/mysql/ #tar xf cattle.tar.gz |
(2)檢視修改cattle資料夾許可權
1
|
#chown -R mysql:mysql cattle |
(3)配置檔案
1
2
3
4
5
6
7
8
|
#vim /etc/my.cnf ------------server-2 新增 server- id =2 log_bin = mysql-bin relay_log = mysql-relay-bin read -only=1 replicate- do -db=cattle log-slave-updates=1 |
1
2
3
4
5
6
7
|
------------server-3 新增 server- id =3 log_bin = mysql-bin relay_log = mysql-relay-bin read -only=1 replicate- do -db=cattle log-slave-updates=1 |
重啟slave的mysql
(4)驗證連線
從庫server-2上測試連線主庫
1
2
|
#mysql -h10.64.5.167 -uslave1 -p123456 mysql> show grants for slave1@10.64.5.170; |
從庫server-3上測試連線主庫
1
2
|
# mysql -h10.64.5.167 -uslave2 -p123456 mysql> show grants for slave2@10.64.5.172; |
(5)設定slave複製
查詢master的position值
1
2
3
4
5
6
7
8
9
|
mysql> SHOW MASTER STATUS\G; *************************** 1. row *************************** File: mysql-bin.000001 Position: 120 Binlog_Do_DB: cattle Binlog_Ignore_DB: mysql 1 row in set (0.00 sec) ERROR: No query specified |
1
2
3
4
5
6
|
server-2配置 mysql>CHANGE MASTER TO MASTER_HOST= '10.64.5.167' , -> MASTER_USER= 'slave1' , -> MASTER_PASSWORD= '123456' , -> MASTER_LOG_FILE= 'mysql-bin.000001' , -> MASTER_LOG_POS=120; -------MASTER_LOG_POS為主庫的Position |
1
2
3
4
5
6
|
server-3配置 mysql>CHANGE MASTER TO MASTER_HOST= '10.64.5.167' , -> MASTER_USER= 'slave2' , -> MASTER_PASSWORD= '123456' , -> MASTER_LOG_FILE= 'mysql-bin.000001' , -> MASTER_LOG_POS=120; |
(6)slave啟動
1
2
|
mysql> START SLAVE; Query OK, 0 rows affected, 1 warning (0.00 sec) |
執行SHOW SLAVE STATUS檢視輸出結果:
主要檢視
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
mysql> SHOW SLAVE STATUS\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.64.5.167 Master_User: slave1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 120 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 178995708 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: cattle 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: 178995562 Relay_Log_Space: 178995864 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) |
驗證master
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
mysql> SHOW PROCESSLIST\G; *************************** 1. row *************************** Id: 14 User: system user Host: db: NULL Command: Connect Time: 63424 State: Connecting to master Info: NULL *************************** 2. row *************************** Id: 15 User: system user Host: db: NULL Command: Connect Time: 63424 State: Slave has read all relay log; waiting for the slave I /O thread to update it Info: NULL *************************** 3. row *************************** Id: 25 User: slave1 Host: 10.64.5.170:47139 db: NULL Command: Binlog Dump Time: 62967 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL *************************** 4. row *************************** Id: 244 User: slave2 Host: 10.64.5.172:45629 db: NULL Command: Binlog Dump Time: 53898 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL 16 rows in set (0.00 sec) ERROR: No query specified |
4:驗證主從同步
在主庫server-1中建立一個表
1
2
3
4
5
6
7
8
9
10
11
12
|
mysql> USE cattle; Database changed mysql> CREATE TABLE ` test ` (`name` varchar(10) NULL ,`old` char(10) NULL ); Query OK, 0 rows affected (0.00 sec) mysql> DESC test ; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | | NULL | | | old | char(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) |
從庫查詢是否有這個新表
1
2
3
4
5
6
7
8
9
10
|
mysql> USE cattle; Database changed mysql> DESC test ; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | | NULL | | | old | char(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec) |
至此,mysql的主從複製完成。
備註: 使用vmware映象 ,兩個mysql的 auto.cnf一樣,會報錯
進入/var/lib/mysql/ 刪除 auto.cnf
報錯檢視 日誌:
cat /var/log/mysql.log