mysql Master-slave replication --centos

 mysql master-slave replication is believed to have used a lot, but because of his work has been how not used. Taking advantage of this period of time is relatively idle, also achieve their own again. Although there are a lot of similar articles on the Internet, but their implementation is still worth recording.

surroundings:

Master: centos 6.0 mysql 5.1.67-log IP : 192.168.0.107
from the server: centos 6.0 mysql 5.1.67-log IP : 192.168.0.109
primary database server test

Copy the code

CREATE TABLE `menber` (
`name` varchar(255) DEFAULT NULL default '',
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
insert into `menber` (`name`, `id`) values('zhangsan','1');
insert into `menber` (`name`, `id`) values('lisi','2');
insert into `menber` (`name`, `id`) values('王五','3');

Copy the code

mysql default configuration file, if not specified defaults to special /etc/my.cnf

mysql configuration file search order: /etc/my.cnf /etc/mysql/my.cnf ~ / .my.cnf

Configuration:

First, the primary server

1.1, create a replication user with permission replication slave.

mysql>grant replication slave on *.* to 'repl'@'192.168.0.109' identified by 'repl';
mysql>flush privileges;

1.2, edit my.cnf file

we /etc/my.cnf

Add to

  server-id=107

Log-bin and open binary log file ( Mysql need / var / write permissions lib / mysql / directory by [chown -R mysql: change mysql / var / lib / mysql Syntax )

  log-bin=/var/lib/mysql/mysql-bin

# Specify an absolute path, whether the person will appear mysql run the show master status; you can not view the log case when 
mysql> show master status; 
Empty the SET (0.00 sec) 
mysql> Show binary logs; 
ERROR 1381 (HY000): by You are not a using binary logging

Other extensions configuration items: 

binlog-do-db = mysql1 # database name to be backed up, if the backup multiple databases, set this option to repeat
binlog-ignore-db = mysql2 # do not need to back up the database name, if the backup multiple databases, set this repeat option to
log-slave-updates = 1 # this parameter must be added, otherwise it will not give an updated binary files to record some
slave-skip-errors = 1 # to skip the error, continue with the copy operation (optional )

1.3, restart the mysql database

service mysqld restart

1.4, setting a read lock

mysql>flush tables with read lock;

1.5 to give binlog log file name and offset ( here remember the File Name and Position values need to use when behind the slave server configuration )

Copy the code

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      713 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

Copy the code

    

1.6, a backup of the database to be synchronized

mysqldump test > test.sql

1.7, Unlock

mysql>unlock tables;

    

Second, from the server (192.168.0.109)

Restore database data master (192.168.0.107) from the backup to the slave server (192.168.0.109)

2.1, edit my.cnf file

we /etc/my.cnf

Add to

server-id=109

2.2, restart from the database

service mysqld restart

2.3, corresponding to the set from the database

  Note here that the value of the name and position of the logfile, the rest of the host, user and password-based account and password database settings

Copy the code

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to 
   -> master_host='192.168.0.107',
   -> master_user='repl',
   -> master_password='repl',
   -> master_log_file='mysql-bin.000001',
   -> master_log_pos=713;

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.0.107
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1079
Relay_Log_File: mysqld-relay-bin.000004
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: 
Replicate_Ignore_DB: 
Replicate_Do_Table: 
Replicate_Ignore_Table: 
Replicate_Wild_Do_Table: 
Replicate_Wild_Ignore_Table: 
Last_Errno: 0
Last_Error: 
Skip_Counter: 0
Exec_Master_Log_Pos: 1079
Relay_Log_Space: 407
Until_Condition: None
Until_Log_File: 
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File: 
Master_SSL_CA_Path: 
Master_SSL_Cert: 
Master_SSL_Cipher: 
Master_SSL_Key: 
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error: 
Last_SQL_Errno: 0
Last_SQL_Error: 
1 row in set (0.00 sec)

ERROR: 
No query specified

Copy the code

Here mainly to see:

  Slave_IO_Running=Yes
  Slave_SQL_Running=Yes

If the Slave_IO_Running: No or Slave_SQL_Running: NO, needs to be redone 2.3, corresponding to the set from the database

Third, the test:
  After you configure the above items can be viewed on the master and slave thread state. On the master, you can see the connection of slave I / O thread is created: Enter the show processlist \ G on the master;

Copy the code

mysql> show processlist\G;
*************************** 1. row ***************************
     Id: 4
   User: root
   Host: localhost
     db: NULL
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
*************************** 2. row ***************************
     Id: 19
   User: repl
   Host: 192.168.0.109:42337
     db: NULL
Command: Binlog Dump
   Time: 183
  State: Has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL
2 rows in set (0.00 sec)

ERROR: 
No query specified

Copy the code

  3.1, in the primary database: 192.168.0.107 on adding new data

insert into `menber` (` name`) values ​​( 'Lee eight'); insert into `menber` (` name`) values ​​( 'Aoi');

3.2 from the database: View database on 192.168.0.109

Copy the code

MySQL> the SELECT * from menber; 
+ ----------- + ---- + 
| name | the above mentioned id | 
+ ----------- + ---- + 
| zhangsan | 1 | 
| lisi | 2 | 
| Wang Wu | 3 | 
| Lee eight | 4 | 
| Aoi | 5 | 
+ ----------- + ---- + 
5 rows in the SET (0.02 sec )

Copy the code

  At this point the data has been successfully copied to the slave from the database 192.168.0.109.

Published 168 original articles · won praise 16 · views 90000 +

Guess you like

Origin blog.csdn.net/ajax_yan/article/details/104993867