linux mysql database replication

A main server configured

1, modify the configuration file my.cnf

[the root @ localhost MySQL] Vim /etc/my.cnf #

# in [mysqld] added:
Server-ID. 1 =
log_bin = Master-bin
log_bin_index = Master-bin.index
binlog_do_db = Test
# Note:
# Server-ID server Uniquely identifies.
#log_bin start the MySQL binary log that data synchronization statements will be executed one by one these statements from the database.
#binlog_do_db designated record binary log database, that database name you want to copy, if you copy multiple databases, set this option to repeat.
#binlog_ignore_db specified binary log database does not record that does not need to copy the database name, and if there are multiple databases, set this option to repeat.
# Which needs to be noted that, binlog_do_db and binlog_ignore_db as mutually exclusive options, usually only one can be.

2, create users and permissions from the server

# Into the mysql database
[root @ localhost mysql] # mysql -p-uroot-
the Enter password:

# masterbackup create users and permissions from the database
mysql> grant replication slave on * * to masterbackup@'192.168.17.% 'identified by'. 123456 ';
# Note
# 192.168.17% wildcard meaning 0-255 IP can access the main server, configure officially designated environment from the server IP.
# if 192.168.17% to%, it can be any ip. as its primary server to access the database from

# exit MySQL
MySQL> exit;

3, restart the mysql service

[root@localhost mysql]# service mysqld restart

4, view the status of the primary server

# Into the mysql database
[root @ localhost mysql] # mysql -p-uroot-
the Enter password:

# Check status of the primary server
mysql> Show Master Status;
+ ------------------- + -------------- + ------------------ + ---------- + ---- + ---------------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+ ------------------- + - ------------------ -------------- + -------- + ------- + + ------------
| Master-bin.000001 | 154 | the Test | | |
+ ------------------- + --- ------------------ + ------- + -------------- + -------- + -----------
1 Row in the SET (0.00 sec)

ps: If you do show master status, the output is empty (mysql not open log)

In the mysql configuration file in /etc/my.cnf

[Mysqld] add the next:

log-bin=mysql-bin

Two, slave configuration from the server

1, modify the configuration file my.cnf

[the root @ localhost MySQL] Vim /etc/my.cnf #

# in [mysqld] added:
Server-ID = 2
Relay-log-bin = Slave-Relay
Relay-index = log-Slave-Relay-bin.index
# -do-db = replicate the Test
# Note:
# server-id uniquely identifies the server, if there are multiple slave servers, each server's server-id can not be repeated, with the same IP is a unique identifier, if you do not set the server-id or It is set to 0, from the server is not connected to the primary server.
# relay-log start the MySQL binary log can be used to make crash recovery and data backup or primary server hang, from this server as the primary server from another server.
# replicate-do-db specify the synchronization database, if you copy multiple databases, set this option to repeat. If not specified binlog-do-db in the master side, then the slave side can be used to filter replication-do-db.
# replicate-ignore-db database does not require synchronization, if there are multiple databases, set this option to repeat.
# Which needs to be noted that, replicate-do-db and replicate-ignore-db as mutually exclusive options, usually only one can be.

2, restart the mysql service

[root@localhost mysql]# service mysql restart

3, a main server connected to master

# Enter mysql database
[mysql the root @ localhost] # -uroot-mysql -p
the Enter password:

# master primary server connected
mysql> change master to master_host = ' 192.168.17.130', master_port = 3306, master_user = 'masterbackup', master_password = ' 123456 ', MASTER_LOG_FILE =' master-bin.000001 ', MASTER_LOG_POS = 154;
# Note:
#master_host corresponding to the IP address of the primary server.
#master_port corresponding port master server.
File corresponding to the column #master_log_file show master status display: master-bin.000001.
#master_log_pos show master status corresponding to the display columns Position: 154.

4, start the slave data synchronization

# Start slave data synchronization
MySQL> Start slave;

# stop slave data synchronization (if necessary)
MySQL> STOP slave;

5, see the slave information

mysql> show slave status\G;

Slave_IO_Running and Slave_SQL_Running are yes, it means that the synchronization is successful.

Third, resolve errors

1, from the database, use the SET global sql_slave_skip_counter to skip the event, skip this one mistake, and then execute the next event from the beginning of the group.

# Operation from the database
MySQL> STOP Slave;
MySQL> SET = Global sql_slave_skip_counter. 1;
MySQL> Slave Start;

Guess you like

Origin www.cnblogs.com/lingyao/p/11994082.html