MySQL manual version 5.0.20-MySQL synchronization (II) _ MySQL

Source: Internet
Author: User
MySQL manual version 5.0.20-MySQL synchronization (II) 6.4 set synchronization

The following describes how to quickly set up a MySQL synchronization server. Suppose you want to synchronize all the databases, and you have not set them before. You need to disable the master server to complete all the steps.

The procedure described in this chapter can be used for one slave or multiple slave cases.

This is just the most direct way to set slave, not just one. For example, if you have a snapshot of the master, the master has set the server ID (server_id) and enabled binary logs, this eliminates the need to shut down the master or stop updating data on the master. For details, see "6.9 Replication FAQ ".

To fully master the MySQL synchronization settings, read this chapter, test all the Statements mentioned in "14.6.1 SQL Statements for Controlling Master Servers" and "14.6.2 SQL Statements for Controlling Slave Servers. You must be familiar with various synchronization settings. for details, see "6.8 Replication Startup Options ".

Note: this process and some subsequent SQL statements require the SUPER permission. Before MySQL 4.0.2, it is the PROCESS permission.

Make sure that MySQL Versions are installed on both the master and slave, and these Versions must be compatible. they are listed in "6.5 Replication Compatibility Between MySQL Versions. Make sure there are still problems in the latest version; otherwise, do not report the bug.

A new account is added to the master before the slave can be connected with it. This account must be granted the replication slave permission. If this account is only used for synchronization (recommended), no additional permissions are required. Set your domain to mydomain.com. you want to authorize an account repl to use the password slavepass, allowing it to connect any host in the domain to the master. Use the GRANT statement to create an account:

Mysql> grant replication slave on *.*

-> TO 'repl' @ '% .mydomain.com' identified by 'slavepass ';

Before MySQL 4.0.2, replace replication slave with the FILE permission:

Mysql> grant file on *.*

-> TO 'repl' @ '% .mydomain.com' identified by 'slavepass ';

If you want to execute the load table from master or load data from master statement on slave, you must grant additional permissions to this account:

Grant global SUPER and RELOAD permissions.

Grant SELECT permission to all tables to be loaded. Any table on the master without the SELECT permission will be skipped by the load data from master.

If only the MyISAM table is used, execute the flush tables with read lock statement to refresh all TABLES and prevent other writes:

Mysql> flush tables with read lock;

Do not exit the client that executes the flush tables statement to keep the read lock valid (if it exits, the read lock is released ). Then, obtain the data snapshot from the master. A simple method is to package and compress the data directory. For example, tar, PowerArchiver, WinRAR, WinZip on Unix, or similar programs on Windows. To use tar to create a compressed package, including all databases, run the following command (change the directory to your actual path ):

Shell> tar-cvf/tmp/mysql-snapshot.tar.

If you only want to package a database this_db, run the following command:

Shell> tar-cvf/tmp/mysql-snapshot.tar./this_db

Copy the file to the '/tmp' directory of slave. On slave, execute the following command to uncompress the package (change the directory to your actual path ):

Shell> tar-xvf/tmp/mysql-snapshot.tar

The mysql database may not need to be synchronized because the permission table on the slave is different from that on the master database. In this case, exclude the compressed package when unzipping it. Do not include any log files in the compressed package, or 'Master. info ~ Or the 'relay -log.info 'file. When the flush tables with read lock statement on the master node is still valid, READ the file name and offset location of the current binary file on the master node:

Mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| Mysql-bin.003 | 73 | test | manual, mysql |

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


The File field shows the binary log File name, and the Position field shows the log offset Position. In this example, the log file is a mysql-bin.003 and the offset position is 73. Write down these values and use them later when setting slave. They indicate the offset position of the master from which the slave starts to process updates. After obtaining the snapshot and recording the log name and offset, the master can release the read lock:

Mysql> unlock tables;

If InnoDB tables are used, InnoDB Hot Backup is recommended. It can ensure snapshot consistency without requesting any lock on the master, and the log file name and offset location have been recorded in the snapshots to be used on the slave later. InnoDB Hot Backup is a free (commercial) additional tool that is not included in the MySQL release package. Visit the InnoDB Hot Backup home page http://www.innodb.com/manual.php to view more details. Another quickest way to remove InnoDB Hot Backup is to close the master server, copy InnoDB data files, log files, and table structure definition files ('. frm' files ). To record the current log file and offset, run the following statement before the master is shut down:

Mysql> flush tables with read lock;

Mysql> show master status;

Write down the log file and offset location in the show master status result as soon as possible. Then, disable the master without unlocking, and ensure that the snapshots and records on the master are consistent:

Shell> mysqladmin-u root shutdown

Another method can be used for both MyISAM and InnoDB tables. This means SQL dumping on the master without the need to back up binary logs as described above. Run the mysqldump -- master-data command on the master and then dump the result file to the slave. However, this is slower than copying binary logs. If the -- log-bin option is not enabled for the master, the file name and offset value in the result of executing the show master status statement are empty, then the parameter values specified on the slave are null strings ('') and 4.

Check that the "my. cnf" file [mysqld] on the master has the "log-bin" option. This range must also have the server-id = master_id option, and the value must be a positive integer between 1 and 2 ^ 32-1. For example:

[Mysqld]

Log-bin

Server-id = 1

If these configuration options do not exist, add and restart the master.

Disable the server to be slave and add the following options to the 'My. cnf 'File:

[Mysqld]

Server-id = slave_id

The value of slave_id is similar to master_id, and is a positive integer between 1 and 2 ^ 32-1. In addition, this ID must not be the same as the master ID. For example:

[Mysqld]

Server-id = 2

If there are multiple slave instances, each slave must have a unique server-id, and its value cannot be the same as that of the master instance and other slave instances. You can think of server-id as an IP address: these IDs identify each server in the entire synchronization combination. If the value of server-id is not specified and the master-host is not defined, the value is 1; otherwise, the value is 2. Note: If the server-id is not set, the master rejects all slave connections and slave rejects connections to the master. Therefore, omitting server-id configuration is only advantageous for backing up binary logs.

If you have backed up the data on the master (directly packed and compressed), copy the data before the slave starts. Make sure that the file permission owner settings are correct. Users running MySQL must have the read and write permissions on these files, just like on the master. If mysqldump is used for backup, you can directly start salve (directly jump to the next step ).

Start slave. if it has already run synchronization, use the -- skip-slave-start option when starting slave so that it will not connect to the master immediately. It is best to use the -- log-warnings option (which is enabled by default since MySQL 4.0.19 and 4.1.2) to start slave to learn more detailed information about the problem (for example,NetworkOr connection issues ). From MySQL 4.0.21 and 4.1.3, the connection with an exception stop is no longer recorded in the error log, unless the value of the -- log-warnings option is greater than 1.

If you use mysqldump to back up data on the master node, import the file to the slave:

Shell> mysql-u root-p <dump_file. SQL

Run the following statement on slave to replace the values of each option with the actual values:

Mysql> CHANGE MASTER

-> MASTER_HOST = 'master _ host_name ',

-> MASTER_USER = 'replication _ user_name ',

-> MASTER_PASSWORD = 'replication _ password ',

-> MASTER_LOG_FILE = 'recorded _ log_file_name ',

-> MASTER_LOG_POS = recorded_log_position;

The following table lists the maximum length of each option string:

MASTER_HOST 60
MASTER_USER 16
MASTER_PASSWORD 32
MASTER_LOG_FILE 255

Start the slave thread:

Mysql> start slave;

After completing the above process, slave should be connected to the master and capture all the update operations after obtaining the snapshot.

If you forget to set the server-id value of the master, slave cannot connect to the master.

If you forget to set the server-id value of the master node, the following content will be recorded in the error log:

Warning: You shoshould set server-id to a non-0 value if master_host is set;

We force server id to 2, but this MySQL server will not act as a slave.

If the synchronization fails for other reasons, the error information is recorded in the slave log.

Once slave starts synchronization, you can find the two files 'Master. info' and 'relay -log.info 'in the data file directory '. Slave uses these two files to track the number of master binary logs processed.

Do not delete or modify these two files unless you know how to modify them. However, we recommend that you use the change master to statement.

Note: the content in 'Master. info' overwrites some of the options specified in the command line or 'My. cnf. For details, see "6.8 Replication Startup Options ".

If you have a data snapshot of the master node, you can configure other slave instances according to the preceding steps. You do not need to obtain the master data snapshot again. each slave can use this snapshot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.