CentOS6.4 system MySQL master-slave replication basic configuration tutorial
For MySQL databases for general purposes, master-slave replication can be used to back up data (if you want to enable automatic slave nodes to take over after the master node fails, you need more complex configurations, if a hardware fault occurs on the master node, the database server can be manually switched to the backup node (slave node) to continue providing services. The basic master-slave replication configuration is very easy. Here we will make a simple record summary.
We select two servers for MySQL master-slave replication. One m1 serves as the master node and the other nn serves as the slave node.
The MySQL database must be installed on both machines. To uninstall the default installation, run the following command:
1
sudo
rpm -e --nodeps mysql
2
yum list |
grep
mysql
Now you can directly execute the following command on CentOS 6.4 for installation:
1
sudo
yum
install
-y mysql-server mysql mysql-deve
Set the password for the root user:
1
mysqladmin -u root password
'shiyanjun'
Then you can log on directly through the MySQL client:
1
mysql -u root -p
Master node configuration
First, considering the database security and ease of management, we need to add a dedicated replication user on the master node m1 so that any user who wants to copy from the master node must use this account:
1
CREATE
USER
repli_user;
2
GRANT
REPLICATION SLAVE
ON
*.*
TO
'repli_user'
@
'%'
IDENTIFIED
BY
'shiyanjun'
;
Operation authorization is also performed here. Use this account to perform cluster replication. If you want to restrict IP segments, you can also configure authorization here.
Then, on the Master node m1, modify the MySQL configuration file/etc/my. cnf to support Master replication. The modified content is as follows:
01
[mysqld]
02
datadir=/var/lib/mysql
03
socket=/var/lib/mysql/mysql.sock
04
user=mysql
05
# Disabling symbolic-links is recommended to prevent assorted security risks
06
symbolic-links=0
07
server-id=1
08
log-bin=m-bin
09
log-bin-index=m-bin.index
10
11
[mysqld_safe]
12
log-error=/var/log/mysqld.log
13
pid-file=/var/run/mysqld/mysqld.pid
Server-id indicates the identity of the Master node. The slave node uses this server-id to identify the node as a Master node (the source database server node in the replication architecture ).
If MySQL is currently started, restart the server after modifying the cluster copy Configuration:
1
sudo
service mysqld restart
Slave node configuration
Then, configure the Slave node nn Similarly, and modify the MySQL configuration file/etc/my. cnf to support the Server Load balancer copy function. The modified content is as follows:
01
[mysqld]
02
datadir=/var/lib/mysql
03
socket=/var/lib/mysql/mysql.sock
04
user=mysql
05
# Disabling symbolic-links is recommended to prevent assorted security risks
06
symbolic-links=0
07
server-id=2
08
relay-log=slave-relay-bin
09
relay-log-index=slave-relay-bin.index
10
11
[mysqld_safe]
12
log-error=/var/log/mysqld.log
13
pid-file=/var/run/mysqld/mysqld.pid
Similarly, if MySQL is currently started, restart the server after modifying the cluster replication Configuration:
1
sudo
service mysqld restart
Next, you need to direct the Slave node nn to the master node and start Slave replication. Execute the following command:
1
CHANGE MASTER
TO
MASTER_HOST=
'm1'
, MASTER_PORT=3306, MASTER_USER=
'repli_user'
, MASTER_PASSWORD=
'shiyanjun'
;
2
START SLAVE;
Verify cluster Replication
At this time, you can perform related operations on the master node m1 to verify that the contents of the database on the master node are synchronized from the node nn.
If we have configured master-slave replication, any changes to the MysQL database on the master node m1 will be replicated to the slave node nn, including creating a database, creating tables, inserting updates, and other operations, we will start from the database creation:
Create a table on the master node m1:
01
CREATE
DATABASE
workflow;
02
CREATE
TABLE
`workflow`.`project` (
03
`id`
int
(11)
NOT
NULL
AUTO_INCREMENT,
04
`
name
`
varchar
(100)
NOT
NULL
,
05
`type` tinyint(4)
NOT
NULL
DEFAULT
'0'
,
06
`description`
varchar
(500)
DEFAULT
NULL
,
07
`create_at`
date
DEFAULT
NULL
,
08
`update_at`
timestamp
NOT
NULL
DEFAULT
CURRENT_TIMESTAMP
ON
UPDATE
CURRENT_TIMESTAMP
,
09
`status` tinyint(4)
NOT
NULL
DEFAULT
'0'
,
10
PRIMARY
KEY
(`id`)
11
) ENGINE=InnoDB
DEFAULT
CHARSET=utf8;
View the binlog content on m1 and run the following command:
1
SHOW BINLOG EVENTS\G
The binlog content is as follows:
01
*************************** 1. row ***************************
02
Log_name: m-bin.000001
03
Pos: 4
04
Event_type: Format_desc
05
Server_id: 1
06
End_log_pos: 106
07
Info: Server ver: 5.1.73-log, Binlog ver: 4
08
*************************** 2. row ***************************
09
Log_name: m-bin.000001
10
Pos: 106
11
Event_type: Query
12
Server_id: 1
13
End_log_pos: 197
14
Info: CREATE DATABASE workflow
15
*************************** 3. row ***************************
16
Log_name: m-bin.000001
17
Pos: 197
18
Event_type: Query
19
Server_id: 1
20
End_log_pos: 671
21
Info: CREATE TABLE `workflow`.`project` (
22
`id` int(11) NOT NULL AUTO_INCREMENT,
23
`name` varchar(100) NOT NULL,
24
`type` tinyint(4) NOT NULL DEFAULT '0',
25
`description` varchar(500) DEFAULT NULL,
26
`create_at` date DEFAULT NULL,
27
`update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
28
`status` tinyint(4) NOT NULL DEFAULT '0',
29
PRIMARY KEY (`id`)
30
) ENGINE=InnoDB DEFAULT CHARSET=utf8
31
3 rows in set (0.00 sec)
Through the above binlog content, we can see that MySQL's binlog records that information, an event corresponds to a row of records. The organizational structure of these records is as follows:
- Log_name: log name, specify the binlog log name for the record operation, here is the m-bin.000001, which corresponds to what we configured in/etc/my. cnf
- Pos: records the start position of an event.
- Event_type: Event Type
- End_log_pos: records the end position of an event.
- Server_id: Server ID
- Info: Event Description
Then, we can view the replication information on the slave node nn. Run the following command to view the database and table information on the slave node nn:
1
SHOW DATABASES;
2
USE workflow;
3
SHOW TABLES;
4
DESC
project;
Let's take a look at the execution of the insert statement. Run the following SQL statement on the master node m1:
1
INSERT
INTO
`workflow`.`project`
VALUES
(1,
'Avatar-II'
, 1,
'Avatar-II project'
,
'2014-02-16'
,
'2014-02-16 11:09:54'
, 0);
You can execute a query on the node. You can see records of the INSERT statements executed on the m1 node copied from the node nn:
1
SELECT
*
FROM
workflow.project;
Verify that the replication is successful.
Copy Common commands
Below, we have summarized several commands that are commonly used in MySQL master-slave replication scenarios:
1
STOP MASTER;
- Terminate master node Replication
1
RESET MASTER;
- Clear master node copy files
1
STOP SLAVE;
- Terminate slave node Replication
1
RESET SLAVE;
- Clear slave node copy files
1
SHOW MASTER STATUS\G;
Result example:
1
*************************** 1. row ***************************
2
File: m-bin.000001
3
Position: 956
4
Binlog_Do_DB:
5
Binlog_Ignore_DB:
6
1 row in set (0.00 sec)
- View master node replication status
1
SHOW SLAVE STATUS\G;
Result example:
01
*************************** 1. row ***************************
02
Slave_IO_State: Waiting for master to send event
03
Master_Host: m1
04
Master_User: repli_user
05
Master_Port: 3306
06
Connect_Retry: 60
07
Master_Log_File: m-bin.000001
08
Read_Master_Log_Pos: 956
09
Relay_Log_File: slave-relay-bin.000002
10
Relay_Log_Pos: 1097
11
Relay_Master_Log_File: m-bin.000001
12
Slave_IO_Running: Yes
13
Slave_SQL_Running: Yes
14
Replicate_Do_DB:
15
Replicate_Ignore_DB:
16
Replicate_Do_Table:
17
Replicate_Ignore_Table:
18
Replicate_Wild_Do_Table:
19
Replicate_Wild_Ignore_Table:
20
Last_Errno: 0
21
Last_Error:
22
Skip_Counter: 0
23
Exec_Master_Log_Pos: 956
24
Relay_Log_Space: 1252
25
Until_Condition: None
26
Until_Log_File:
27
Until_Log_Pos: 0
28
Master_SSL_Allowed: No
29
Master_SSL_CA_File:
30
Master_SSL_CA_Path:
31
Master_SSL_Cert:
32
Master_SSL_Cipher:
33
Master_SSL_Key:
34
Seconds_Behind_Master: 0
35
Master_SSL_Verify_Server_Cert: No
36
Last_IO_Errno: 0
37
Last_IO_Error:
38
Last_SQL_Errno: 0
39
Last_SQL_Error:
40
1 row in set (0.00 sec)
- View slave node replication status
1
SHOW
BINARY
LOGS\G