My mysql master-slave replication configuration method

server-id I used the last number of the IP. The maximum log size is 512MB, and records older than 30 days are deleted, which can be adjusted freely according to the actual environment.

first step:

Main mysql my.cnf configuration:

wrote
server-id = 228
log-bin=mysql-bin
expire_logs_days = 30

 

 

Connect to the main database:

mysql -uroot -pxxxxxxxx

GRANT REPLICATION SLAVE ON *.* to 'sync_xxx'@'%' identified by 'xxxxxxxx';

flush privileges;

View permissions: select * from user where user = 'sync_xxx';

You can see that the value of the Repl_slave_priv key pair is Y.

 

 

 Step 2:

Configure my.cnf from database and restart:

wrote
server-id = 220
log-bin=mysql-bin
relay_log = mysql-relay-bin
log_slave_updates = 1
read_only = 1
replicate-wild-ignore-table =mysql.%
replicate-wild-ignore-table =test.%
replicate-wild-ignore-table =log.%
replicate-wild-ignore-table =information_schema.%
replicate-wild-ignore-table =performance_schema.%

 

 

The third step is also the most important and most important step. To ensure that the data of the two databases are completely consistent, you must stop the web service of the main database, then dump the data, then start the main database, and check the log status, because this backup data is imported From the library, the log status is configured to the slave library, which can ensure that the slave library data is completely consistent with the master library.

 

At this point, it must be ensured that there are no read and write operations in the two databases (disconnecting services such as the web that write to the database).

Dump the main database data (if the data is too large to be dumped, you can stop mysql and copy the entire data directory.), and then check the log status:

1. Connect to the database:

Check the log status: show master status; see the following information.

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 |     1230 |              |                  |

+------------------+----------+--------------+------------------+

 

1 row in set (0.00 sec)

With the backup file and the master status of the primary database that has not performed any write operations since the backup file, the web service of the server where the primary database is located can be restored, because the same data is imported from the library, and this log starting point can be configured. 

 

The data exported from the master database is imported into the slave database.

Connect to the slave database and execute: Note: mysql-bin.000002 comes from the file name field File obtained by viewing the status of the master database, and MASTER_LOG_POS is the Position field.

CHANGE MASTER TO
MASTER_HOST= '192.168.1.228' ,
MASTER_USER= 'xxxxx' ,
MASTER_PASSWORD= 'xxxxxx.' ,
MASTER_PORT= 3306 ,
MASTER_LOG_FILE= 'mysql-bin.000003' ,
MASTER_LOG_POS= 1230 ,
MASTER_CONNECT_RETRY= 10 ;
implement:
start slave;
 
View the slave database status: show slave status\G;
 
 
The configuration has set the slave database to be read-only, preventing any data from being written from the database, check:
mysql> show global variables like 'read_only'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | read_only | ON | +---------------+-------+ 1 row in set (0.01 sec)
 
It should be noted here that although read-only is set, the root user still has permission to write data, so all services that write to the database cannot be connected by the root user. In addition, if you create a user, the following method will also cause Created a root class user with supper privileges:
 

grant all privileges on *.* to [email protected] identified by 'password';

flush privileges;

Equivalent to creating another super user! ! !

Therefore, creating users can only be done in the following ways:

grant all privileges on db_name.* to [email protected] identified by 'password';

flush privileges;

The method to verify that the read-only setting is successful:

connect to mysql database

Update a piece of data to see if it works.

mysql> update account set nick='xxx';

ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement

 

After the master-slave configuration is complete, go to the master database to change a piece of data, and then execute the sql query from the slave database to see if there is any change. If it changes, just start the web service on the master-slave server.

 

 

 The role of the master-slave configuration is mainly to achieve dual-system hot backup, but only the master database provides external services. Of course, the slave database can be used for data reading, but there must be no write operation, otherwise the slave database data will not be synchronized to In the master database, and for the case of using an auto-incrementing id, it will cause the slave database to be unable to come from the master database due to the addition of data (duplicate id error).

If the master-slave database needs to provide services to the outside world, you need to configure the master-slave database with each other. Multiple hosts can be masters and slaves of each other, but you need to set the increment method of id, see the article:

http://www.cnblogs.com/top5/archive/2009/04/15/1436399.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326751305&siteId=291194637
Recommended