The following sections describe how to set up slaves. Before you proceed, ensure that you have:
Configured the MySQL master with the necessary configuration properties. See Section 16.1.2.1, “Setting the Replication Master Configuration”.
Obtained the master status information. See Section 16.1.2.3, “Obtaining the Replication Master Binary Log Coordinates”.
On the master, released the read lock:
mysql> UNLOCK TABLES;
Each replication slave must have a unique server ID. If this has not already been done, this part of slave setup requires a server restart.
If the slave server ID is not already set, or the current
value conflicts with the value that you have chosen for the
master server, shut down the slave server and edit the
[mysqld] section of the configuration file
to specify a unique server ID. For example:
[mysqld]
server-id=2After making the changes, restart the server.
If you are setting up multiple slaves, each one must have a
unique server-id value that
differs from that of the master and from any of the other
slaves.
If you omit server-id (or set
it explicitly to its default value of 0), the slave refuses
to connect to a master.
You do not have to enable binary logging on the slave for replication to be set up. However, if you enable binary logging on the slave, you can use the slave's binary log for data backups and crash recovery, and also use the slave as part of a more complex replication topology. For example, where this slave then acts as a master to other slaves.
To set up the slave to communicate with the master for replication, configure the slave with the necessary connection information. To do this, execute the following statement on the slave, replacing the option values with the actual values relevant to your system:
mysql> CHANGE MASTER TO
-> 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;
Replication cannot use Unix socket files. You must be able to connect to the master MySQL server using TCP/IP.
The CHANGE MASTER TO statement
has other options as well. For example, it is possible to set
up secure replication using SSL. For a full list of options,
and information about the maximum permissible length for the
string-valued options, see Section 13.4.2.1, “CHANGE MASTER TO Syntax”.
The next steps depend on whether you have existing data to import to the slave or not. See Section 16.1.2.4, “Choosing a Method for Data Snapshots” for more information. Choose one of the following:
If you do not have a snapshot of a database to import, see Section 16.1.2.5.3, “Setting Up Replication between a New Master and Slaves”.
If you have a snapshot of a database to import, see Section 16.1.2.5.4, “Setting Up Replication with Existing Data”.
When there is no snapshot of a previous database to import, configure the slave to start the replication from the new master.
To set up replication between a master and a new slave:
Start up the MySQL slave and connect to it.
Execute a
CHANGE MASTER TOstatement to set the master replication server configuration. See Section 16.1.2.5.2, “Setting the Master Configuration on the Slave”.
Perform these slave setup steps on each slave.
This method can also be used if you are setting up new servers but have an existing dump of the databases from a different server that you want to load into your replication configuration. By loading the data into a new master, the data is automatically replicated to the slaves.
If you are setting up a new replication environment using the data from a different existing database server to create a new master, run the dump file generated from that server on the new master. The database updates are automatically propagated to the slaves:
shell> mysql -h master < fulldb.dump
When setting up replication with existing data, transfer the snapshot from the master to the slave before starting replication. The process for importing data to the slave depends on how you created the snapshot of data on the master.
Choose one of the following:
If you used mysqldump:
Start the slave, using the
--skip-slave-startoption so that replication does not start.Import the dump file:
shell> mysql < fulldb.dump
If you created a snapshot using the raw data files:
Extract the data files into your slave data directory. For example:
shell> tar xvf dbdump.tarYou may need to set permissions and ownership on the files so that the slave server can access and modify them.
Start the slave, using the
--skip-slave-startoption so that replication does not start.Configure the slave with the replication coordinates from the master. This tells the slave the binary log file and position within the file where replication needs to start. Also, configure the slave with the login credentials and host name of the master. For more information on the
CHANGE MASTER TOstatement required, see Section 16.1.2.5.2, “Setting the Master Configuration on the Slave”.Start the slave threads:
mysql> START SLAVE;
After you have performed this procedure, the slave connects to the master and replicates any updates that have occurred on the master since the snapshot was taken.
If the server-id option for the
master is not correctly set, slaves cannot connect to it.
Similarly, if you have not set the
server-id option correctly for
the slave, you get the following error in the slave's error
log:
Warning: You should set server-id to a non-0 value if master_host
is set; we will force server id to 2, but this MySQL server will
not act as a slave.You also find error messages in the slave's error log if it is not able to replicate for any other reason.
The slave stores information about the master you have
configured in its master info repository. The master info
repository can be in the form of files or a table, as
determined by the value set for
--master-info-repository. When
a slave uses --master-info-repository=FILE,
two files are stored in the data directory, named
master.info and
relay-log.info. If
--master-info-repository=TABLE instead, this
information is saved in the
master_slave_info table in the
mysql database. In either case, do
not remove or edit the files or table.
Always use the CHANGE MASTER TO
statement to change replication parameters. The slave can use
the values specified in the statement to update the status
files automatically. See Section 16.2.4, “Replication Relay and Status Logs”, for
more information.
The contents of the master info repository override some of
the server options specified on the command line or in
my.cnf. See
Section 16.1.6, “Replication and Binary Logging Options and Variables”, for more details.
A single snapshot of the master suffices for multiple slaves. To set up additional slaves, use the same master snapshot and follow the slave portion of the procedure just described.