mysql5.5.15 configure master-slave database

1. Ensure that the main library and the slave library network are unblocked and can access each other

Second, the main library configuration

1. Edit my.cnf of the main library

   Add the following configuration under [mysqld]

   server-i=1 #Generally the default is 1, and no modification is required (generally the last two digits of the ip are used as server-id to ensure global consistency)

   read-only=0#The main library can read and write

   binlog-do-db=test#The name of the database to be synchronized, multiple lines can be written to synchronize multiple

   binlog-ignore-db=mysql#The name of the database that does not need to be synchronized, you can write multiple lines

   binlog-ignore-db=performance_schema

   log-bin=mysql-bin #Binary log name, make sure this file is writable

2. Set the account used to synchronize the database

   After editing the configuration file, restart mysql,  service mysqld restart , log in, execute the following command, which means to configure the login user, password, and permissions for slave node 195,

User: backup, password: 123, slave node: 10.6.222.195, permission: replication slave

  例:  grant replication slave on *.* to [email protected] identified by '123'

3. View the host status

mysql>show master status;

 

Note down the file and position, the binlog_do_db and binlog_ignore_db shown here are the first steps set in the configuration file. file is the synchronized log file, the position should start from the line of the file

3. Slave library configuration

1. Edit my.cnf from the library

   Change the server-id of the slave library to be inconsistent with the main library. You can also add replicate-do-db and replicate-ignore-db to specify the synchronized database and the unsynchronized database.

   server-id=2

   read-only=1 #Slave library read-only

   replicate-do-db=test

   replicate-ignore-db=mysql

   replicate-ignore-db=information_schema

   replicate-ignore-db=performance_schema

2. Set the master library information on the slave library

   After editing the configuration file, restart the slave library mysql , execute stop slave in mysql; then use the change master  command to set the master library information.

   mysql>change master to master_host='10.6.208.183',master_user='backup',master_password='123',master_log_file='mysql-bin.000019',master_log_pos=746;

master_host为主库ipmaster_usermaster_password是第二步分配的同步用的用户名和密码;master_log_filemaster_log_position是第三步中的fileposition值。

3、查看是否配置成功

start slave #启动从数据库

show slave status \G;#查看从库状态

 

Slave_IO_StateSlave_IO_RunningSlave_SQL_Running状态如上图则表示配置成功。

四、其他

1、主从库都配置好后,将主库的进行一个完全备份,然后导入从库,保证当前主从一致,那么以后主库的任何修改都会同步到从库上面,保证主从数据的一致性。

2、配置过程常见错误

若出现Slave_IO_RunningSlave_SQL_Running状态为no,则从stop slave 从新执行一遍change master to master_host='10.6.208.183',master_user='backup',master_password='123',master_log_file='mysql-bin.000019',master_log_pos=746;

然后再start slave就可以了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325959693&siteId=291194637