mysql backup master-slave configuration

MySQL replication master-slave principle
recorded thereon the binary mysql-bin.xxx (which can be seen in the data file directory in the directory mysql) are in each operation of the primary database server, dedicated account number from I / O thread server log on to the primary server to read the binary file, the file contents are written to your local relay log relay-log file, and then executes SQL statements from the SQL server threads based on the content relay log.

principle

Preparation
master server needs to have the same initial state

The primary server to synchronize database shackles, avoid synchronous data changes

MySQL> db use;
MySQL> Tables with the flush Read Lock;
primary data export database server (an example where db)

MySQL> -pxxxx the mysqldump -uroot-DB> db.sql;
MySQL> -R & lt -ndt the mysqldump -uroot-DB -pxxxx> db.sql
the backup is complete, the unlocking master database server

mysql> unlock tables;
initial data imported from the server database

MySQL> Create Database DB;
MySQL> use DB;
MySQL> Source db.sql;
At this point, the master from the database consistent. Here configured master-slave synchronization.

The above command is to export all tables and data structures, functions, and if you want to export it to use stored procedures

Primary server is configured
to modify the MySQL configuration

vi /etc/my.cnf
added [mysqld] in which:

# Master database client ID number
server_id = 1
# turn on binary logging
log-bin = mysql-bin
database name # you want to copy, if you copy multiple databases, set this option to repeat
binlog-do-db = db
# from the server update received from the primary server credited to the slave's own binary log files in the
log-Slave-the updates
# binlog write control frequency. How many transactions per execution write-once (this parameter and a great performance, but can reduce the losses caused by the collapse of MySQL)
sync_binlog = 1
# This parameter is generally used in the main main synchronization for staggered from value-added, to prevent key violation
=. 1 auto_increment_offset
# this parameter is generally used in the main primary synchronization, since the offset value is used to prevent the key violation
auto_increment_increment = 1
# when the number of days of binary log automatically deleted, the default value is 0, indicating "not automatically deleted", and start may be removed when the binary log cycle
expire_logs_days = 7
# copy function to Slave
log_bin_trust_function_creators = 1
restart MySQL, allows synchronous data from the server to create an account

# Create a slave account sync_account, password password
MySQL> Grant Replication slave ON . To 'sync_account' @ 'slave_db_ip' IDENTIFIED by 'password';
# update the database privileges
mysql> flush privileges;
view the main server status

Remember the following result values: File and Position, when configuring from the library will be used.

MySQL> Show Master Status \ G;
******************************************************** 1. Row ****************
File: MySQL-bin. # 000033 current log record
position: 337523 # log recording position
Binlog_Do_DB:
Binlog_Ignore_DB:
after this step is not to operate the master database, the status value is changed to prevent

Configuring from the server
to modify the MySQL configuration

vi /etc/my.cnf
added [mysqld] in

= 2 the server_id
log = MySQL-bin bin-
log-Slave-Updates
sync_binlog = 0
#log Buffer once per second to write the log file, the log file and flush (brush disk) operations are performed simultaneously. In this mode when the transaction is committed, will not take the initiative to write to disk operation is triggered
innodb_flush_log_at_trx_commit = 0
# specify which library you want to copy slave
replicate-do-db = db
#MySQL master copy from time, when between Master and Slave network outages, but the case of Master and Slave undetectable (such as a firewall or routing problem). After the Slave will wait for the number of seconds slave_net_timeout set to consider the network fails, then reconnect and catch up before this time the main library data
Slave-NET-timeout = 60
log_bin_trust_function_creators = 1
reboot from the server.

Synchronous command execution

# For performing synchronous commands, the primary server IP, account password synchronization, the synchronization position
mysql> change master to master_host = '10 .10.20.111 ', master_user =' sync_account ', master_password =' password ', master_log_file =' mysql-bin.000033 ' , MASTER_LOG_POS = 337 523;
# turn sync
mysql> start slave;
View from the server status

MySQL> Show Slave Status \ G;
*************************** 1. Row ************ ***************
Slave_IO_State: Waiting for Master to the send Event
MASTER_HOST: 10.10.20.111
MASTER_USER: the Account
MASTER_PORT: 3306
Connect_Retry: 60
Master_Log_File: MySQL-bin.000033
Read_Master_Log_Pos: 337 523
Relay_Log_File: the DB2 bin.000002--relay
RELAY_LOG_POS: 337 686
Relay_Master_Log_File: MySQL-bin.000033
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Slave_IO_Running and Slave_SQL_Running process must run properly, that Yes status, otherwise your synchronization failed. If the failure to see mysql error log specific error details to locate the problem. Finally you can go to the database on the primary server, create a table or update table data to test synchronization.

Guess you like

Origin www.cnblogs.com/MartyCode/p/12512253.html
Recommended