M-S Architecture: Achieving Dual Hot Standby (AB Replication)
1. Reducing master Reading Pressure
2. The database can be "hot-standby". Hot-standby can only solve the hardware and software faults of hardware master, but it can not solve the logic faults caused by human misoperation (such as deleting important records by incorrect input of SQL statements), so regular backup is necessary.
Environmental preparation and requirements:
1. Close firewalls and selinux
2. Host name and ip address of two servers in hosts file correspond one by one
3. System Time Need Synchronization
4. The database versions of master and slave are consistent (system versions are consistent)
Train of thought:
1. master must open binary logs
2. Slve must open the relay log
3. The server-id of master and slave must be different (2 ^ 23-1)
4. The initial data of master and slave are consistent
M-S Architecture Operating Steps
Environmental Science:
master: 192.168.1.166
slave: 192.168.1.114
1. Environmental preparation
# master:
[root@master ~]# vim /etc/hosts
192.168.1.114 slave
# slave:
[root@slave ~]# vim /etc/hosts
192.168.1.166 master
2. Modifying configuration files
# master:
root@master ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id = 1
# slave:
[root@slave ~]# vim /etc/my.cnf
server-id = 2
relay-log=/data/DB/relay.log
3. Data Initialization Consistency
Scenario 1: There is no business data on either side, and the master and slave databases are initialized directly.
Case 2: The master has business data on it
masteer:
1>Stop the database
[root@master ~]# /etc/init.d/mysqld stop
2>Physical level backup
[root@master ~]# cd /data/DB/ && ll
[root@master DB]# tar -cvzf /tmp/mysql.tar.gz *
[root@master DB]# scp /tmp/mysql.tar.gz 192.168.1.114:/tmp
slave:
3>slave Data recovery at the end
[root@slave ~]# cd /data/DB/ && ll
[root@slave DB]# tar -xf /tmp/mysql.tar.gz -C .
[root@slave DB]# /etc/init.d/mysqld start
master and slave:
4>See slave and master Is the data above the same?
[root@master DB]# echo "show databases;" | mysql -p123
[root@slave DB]# echo "show databases;" | mysql -p123
The sample master has business data on it
4. Create authorized users on master and view binlog information
5. Configure synchronization information on slave
6. Test Verification
Test one:
//First look at the user table information in ceshi libraries of master and slave
master:
mysql> select * from ceshi.users;
+----+-------+----------+-----------------+
| id | name | password | address |
+----+-------+----------+-----------------+
| 1 | zhang | 1234 | NULL |
| 2 | wang | 4321 | Wuhan |
| 3 | li | 5678 | Haidian, Beijing |
| 4 | liu | 1122 | NULL |
| 5 | zou | 4311 | Changsha, Hunan |
| 6 | zhou | 6789 | Babaoshan, Beijing |
| 7 | ding | 7891 | Xili, Shenzhen |
+----+-------+----------+-----------------+
7 rows in set (0.00 sec)
slave:
mysql> select * from ceshi.users;
+----+-------+----------+-----------------+
| id | name | password | address |
+----+-------+----------+-----------------+
| 1 | zhang | 1234 | NULL |
| 2 | wang | 4321 | Wuhan |
| 3 | li | 5678 | Haidian, Beijing |
| 4 | liu | 1122 | NULL |
| 5 | zou | 4311 | Changsha, Hunan |
| 6 | zhou | 6789 | Babaoshan, Beijing |
| 7 | ding | 7891 | Xili, Shenzhen |
+----+-------+----------+-----------------+
7 rows in set (0.11 sec)
//Delete data on master and go back to slave to see
master:
mysql> delete from ceshi.users where id>=3;
mysql> select * from ceshi.users;
+----+-------+----------+--------------+
| id | name | password | address |
+----+-------+----------+--------------+
| 1 | zhang | 1234 | NULL |
| 2 | wang | 4321 | Wuhan |
+----+-------+----------+--------------+
2 rows in set (0.00 sec)
slave:
mysql> select * from ceshi.users;
+----+-------+----------+--------------+
| id | name | password | address |
+----+-------+----------+--------------+
| 1 | zhang | 1234 | NULL |
| 2 | wang | 4321 | Wuhan |
+----+-------+----------+--------------+
2 rows in set (0.00 sec)
//Test two:
//Stop services on slave
[root@slave ~]# /etc/init.d/mysqld stop
//Insert data on master and view it
mysql> insert into ceshi.users(name, password, address) values("ceshi","666666","Shenzhen, Guangdong");
Query OK, 1 row affected (0.04 sec)
mysql> select * from ceshi.users;
+----+-------+----------+--------------+
| id | name | password | address |
+----+-------+----------+--------------+
| 1 | zhang | 1234 | NULL |
| 2 | wang | 4321 | Wuhan |
| 8 | ceshi | 666666 | Shenzhen, Guangdong |
+----+-------+----------+--------------+
3 rows in set (0.00 sec)
//Start slave again and see if there are updates
[root@slave ~]# /etc/init.d/mysqld start
mysql> select * from ceshi.users;
+----+-------+----------+--------------+
| id | name | password | address |
+----+-------+----------+--------------+
| 1 | zhang | 1234 | NULL |
| 2 | wang | 4321 | Wuhan |
| 8 | ceshi | 666666 | Shenzhen, Guangdong |
+----+-------+----------+--------------+
3 rows in set (0.00 sec)