MySQL database master can be made from a master copy of the primary database, to copy from one or more servers from a MySQL database, default, asynchronous replication; depending on the configuration, can be copied in databases of all, or even a selected database is selected from set the table.
Mysql master-slave replication advantages:
Scale Solutions
A plurality of expansion between the load from the library to improve performance. In this environment, all written and updated in the main library . However, the reading may occur on one or more from the library. The model can improve the write performance (due to the primary database is dedicated to the update), and read from the plurality of libraries, can greatly improve the reading speed.
Data security
Since the data is copied to the main library from the library, from the library can pause the copy process, you can run the backup service from the library without disrupting data corresponding to the main library.
analysis
You can create real-time data on the primary database, and the information can be analyzed from the library without affecting the performance of the primary server.
Long-distance data distribution
You can use remote site replication of data used to create a local copy without permanent access to the main library.
1. Preparations
Mysql version: MySQL 5.7.19
Master-Server: 192.168.252.123
Slave-Server: 192.168.252.124
Turn off the firewall
iptables -F
2. Master-Server Configuration
Configure Master to copy-based binary log file location, you must enable binary logging and create a unique server ID, otherwise you can not be master-slave replication.
Stop the MySQL service.
systemctl stop mysqld
Open binlog, each different set of server-id
cat /etc/my.cnf [mysqld] log-bin=mysql-bin server-id=1
Start MySQL service
systemctl start mysqld
Log in MySQL
mysql -uroot -p
Create a user
Each connection to the main library from the library to use MySQL user name and password, so you must have a user account on the main library, you can connect from the library. Any account can be used for this operation, as long as it has been granted REPLICATION SLAVE
permission. You can create different accounts for each selected from the library, or each use the same account to connect to the main library from library
Although not necessary to create an account specifically for the copy, it should be noted, used to copy the username and password will be stored in plain text format in the main information repository file or table. Therefore, we need to create a separate account, the account has permission to copy only the process, to reduce risks to other accounts as possible.
mysql> CREATE USER 'replication'@'192.168.252.124' IDENTIFIED BY 'mima'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.252.124';
Note: If problems arise password error when creating a user:
error:Your password does not satisfy the current policy requirements
View password policy:
show variables like '%password%';
+---------------------------------------+--------+
| Variable_name | Value |
+---------------------------------------+--------+
| default_password_lifetime | 0 |
| disconnect_on_expired_password | ON |
| log_builtin_as_identified_by_password | OFF |
| mysql_native_password_proxy_users | OFF |
| old_passwords | 0 |
| report_password | |
| sha256_password_proxy_users | OFF |
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 0 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+---------------------------------------+--------+
MySQL modify password policy
set global validate_password_mixed_case_count=0;
3.Slave-Server Configuration
Modify my.cnf
cat /etc/my.cnf [mysqld] server-id=2
To set from a plurality of libraries, each with a main server-id and the other unique values library from the library from the library of different settings.
Start MySQL service, log on MySQL
systemctl start mysqld
mysql -uroot -p
Placement main Hyogo communication
View Master-Server, binlog File file name and location and write down the value of Position
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 629 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
To set up to communicate with the master library from the library, copy, with the necessary connection information from the library arranged in the following statements from the library
will be replaced with the actual value associated with the value of the option for the system
CHANGE MASTER TO MASTER_HOST='192.168.252.123', MASTER_USER='replication', MASTER_PASSWORD='mima', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=629;
Start copied from the server thread
mysql> START SLAVE; Query OK, 0 rows affected (0.00 sec)
查看复制状态 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.252.123 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 629 Relay_Log_File: master2-relay-bin.000003 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes ......
Check the replication master from the communication state
Slave_IO_State
# Station from the current state of Slave_IO_Running: Yes
whether the main program # reads the binary log of I / O threads are running Slave_SQL_Running: Yes
# execute if the primary server to read binary log events in SQL threads are running. And I / O thread as Seconds_Behind_Master
# if that has been synchronized to 0,0
Must all be Yes
If not because of the following four aspects:
1, No Network
2, password wrong
3, MASTER_LOG_POS not PS
4, MySQL of auto.cnf
the same server-uuid (you may be copied mysql)
CAT /var/lib/mysql/auto.cnf $ [Auto] Server-UUID = # accordance with this a178-000c29cb5cbc 6b831bf3-8ae7-11e7-hex format, modify server-uuid, to restart mysql
4. Test master copy from
Start MySQL service
Log in MySQL
Create a test library Master-Server
View the Slave-Server are synchronized over
Some commands
Check the status of a master server
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 1190 | | | | +------------------+----------+--------------+------------------+-------------------+
View from the server host list
mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+ | 2 | | 3306 | 1 | 6b831bf2-8ae7-11e7-a178-000c29cb5cbc | +-----------+------+------+-----------+--------------------------------------+
Get binlog file list
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+ | mysql-bin.000001 | 1190 | +------------------+-----------+
See only the first binlog file
mysql> mysql> show binlog events;
+------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.19-log, Binlog ver: 4 | | mysql-bin.000001 | 123 | Previous_gtids | 1 | 154 | | | mysql-bin.000001 | 420 | Anonymous_Gtid | 1 | 485 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000001 | 485 | Query | 1 | 629 | GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.252.124' | | mysql-bin.000001 | 629 | Anonymous_Gtid | 1 | 694 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000001 | 694 | Query | 1 | 847 | CREATE DATABASE `replication_wwww.ymq.io` | | mysql-bin.000001 | 847 | Anonymous_Gtid | 1 | 912 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000001 | 912 | Query | 1 | 1190 | use `replication_wwww.ymq.io`; CREATE TABLE `sync_test` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 | +------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
View the contents of the specified binlog file
mysql> mysql> show binlog events in 'mysql-bin.000001'; +------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.19-log, Binlog ver: 4 | | mysql-bin.000001 | 123 | Previous_gtids | 1 | 154 | | | mysql-bin.000001 | 420 | Anonymous_Gtid | 1 | 485 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000001 | 485 | Query | 1 | 629 | GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.252.124' | | mysql-bin.000001 | 629 | Anonymous_Gtid | 1 | 694 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000001 | 694 | Query | 1 | 847 | CREATE DATABASE `replication_wwww.ymq.io` | | mysql-bin.000001 | 847 | Anonymous_Gtid | 1 | 912 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000001 | 912 | Query | 1 | 1190 | use `replication_wwww.ymq.io`; CREATE TABLE `sync_test` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 | +------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Thread from start to copy library
mysql> START SLAVE;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Stop copy from the library thread
mysql> STOP SLAVE;
Query OK, 0 rows affected (0.00 sec)