Master-Slave Replication configuration
The steps are as follows:
Primary server: From the server IP address, respectively
[Python]View Plain copy
- 192.168.145.222,192.168. 145.226
1. Modify Master server Master:
[Python]View Plain copy
- Vi/etc/my.cnf
- [Mysqld]
- Log-bin=mysql-bin #[must] enable binary logging
- server-id=222 #[must] server unique ID, default is 1, usually take IP last paragraph
2. Modify the slave from the server:
[Python]View Plain copy
- Vi/etc/my.cnf
- [Mysqld]
- Log-bin=mysql-bin #[not required] enable binary logging
- server-id=226 #[must] server unique ID, default is 1, usually take IP last paragraph
3. Restart MySQL for two servers
[Python]View Plain copy
- Systemctl Restart MARIADB
4. Establish an account on the primary server and authorize slave:
[Python]View Plain copy
- Mysql
- Mysql>grant REPLICATION SLAVE On * * to ' mysync ' @'% ' identified by ' q123456 ';//normally without root account, "%" Indicates that all clients may connect, as long as the account, the password is correct, here can be replaced by specific client IP, such as 192.168. 145.226, enhance security.
5, log on to the master server MySQL, query the status of master
[Python]View Plain copy
- Mysql>show Master status;
- +------------------+----------+--------------+------------------+
- | File | Position | binlog_do_db | binlog_ignore_db |
- +------------------+----------+--------------+------------------+
- | Mysql-bin. 000004 | 308 | | |
- +------------------+----------+--------------+------------------+
- 1 row in Set (0.00 sec)
Note: Do not operate the master server MySQL again after performing this step to prevent the change of the primary server state value
6. Configure the slave from the server:
Note that mysql-bin.000004 and 308 are file and position in the fifth step
[Python]View Plain copy
- Mysql>change Master to master_host=' 192.168.145.222 ', master_user=' Mysync ', master_password=' q123456 ', master_log_file=' mysql-bin.000004 ', master_log_pos=308;//Note mysql-bin. 000004 and 308 are the fifth steps in the file and
- Mysql>start slave; To start the Copy from Server feature
7. Check the status of the replication function from the server:
[Python]View Plain copy
- Mysql> Show Slave Status\g
- 1. Row ***************************
- Slave_io_state:waiting for Master to send event
- Master_host: 192.168. 2.222//Primary server address
- Master_user:mysync//Authorization account name, try to avoid using root
- Master_port: 3306//Database port, partial version not on this line
- Connect_retry:
- Master_log_file:mysql-bin. 000004
- Read_master_log_pos: + //#同步读取二进制日志的位置, greater than or equal to Exec_master_log_pos
- Relay_log_file:ddte-relay-bin. 000003
- Relay_log_pos: 251
- Relay_master_log_file:mysql-bin. 000004
- Slave_io_running:yes//This status must be Yes
- Slave_sql_running:yes//This status must be Yes
- ......
Note: The slave_io and slave_sql processes must function normally, that is, the Yes state, otherwise it is an error state (e.g., one of the No is an error).
The above operation process, the master and slave server configuration is complete.
9, the master-slave server test:
master server MySQL, build the database, and create a table in this library to insert a piece of data:
[Python]View Plain copy
- mysql> CREATE DATABASE hi_db;
- Query OK, 1 row affected (0.00 sec)
- mysql> use hi_db;
- Database changed
- Mysql> CREATE TABLE HI_TB (id int (3), name char (10));
- Query OK, 0 rows affected (0.00 sec)
- mysql> INSERT into HI_TB values (001,' Bobu ');
- Query OK, 1 row affected (0.00 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | Information_schema |
- | hi_db |
- | MySQL |
- | Test |
- +--------------------+
- 4 rows in Set (0.00 sec)
- MySQL Query from server:
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | Information_schema |
- | hi_db | I ' M here, you see?
- | MySQL |
- | Test |
- +--------------------+
- 4 rows in Set (0.00 sec)
- Mysql> Use hi_db
- Database changed
- Mysql> SELECT * from HI_TB; View new specific data on the primary server
- +------+------+
- | ID | name |
- +------+------+
- | 1 | bobu |
- +------+------+
- 1 row in Set (0.00 sec)
MySQL Master-slave configuration