Here is a quick description of how to set up complete replication on your current MySQL server. It assumes you want to replicate all your databases and have not configured replication before. You will need to shutdown your master server briefly to complete the steps outlined here.
While this method is the most straightforward way to set up a slave, it is not the only one. For example, if you already have a snapshot of the master, and the master already has server id set and binary logging enabled, you can set up a slave without shutting the master down or even blocking the updates. For more details, please see section 4.10.8 Replication FAQ.
If you want to be able to administrate a MySQL replication setup, we suggest that you read this entire chapter through and try all commands mentioned in section 4.10.7 SQL Commands Related to Replication. You should also familiarise yourself with replication startup options in `my.cnf' in section 4.10.6 Replication Options in `my.cnf'.
FILE
(in MySQL versions older than 4.0.2) or REPLICATION SLAVE
privilege in newer MySQL versions. You must also have given this user
permission to connect from all the slaves. If the user is only doing replication
(which is recommended), you don't need to grant any additional privileges.
For example, to create a user named repl
which can access your
master from any host, you might use this command:
mysql> GRANT FILE ON *.* TO repl@"%" IDENTIFIED BY '<password>'; # master < 4.0.2
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@"%" IDENTIFIED BY '<password>'; # master >= 4.0.2If you plan to use the
LOAD TABLE FROM MASTER
or
LOAD DATA FROM MASTER
commands, you will also need to grant,
on the master, to the above user,
the REPLICATION CLIENT
(or SUPER
if the
master is older than 4.0.13) privilege, the RELOAD
privilege,
and SELECT
privileges on all tables you want to load. All
master tables on which the user can't SELECT
will be ignored by
LOAD DATA FROM MASTER
.
FLUSH TABLES WITH READ LOCK
command.
mysql> FLUSH TABLES WITH READ LOCK;and then take a snapshot of the data on your master server. The easiest way to do this is to simply use an archiving program (
tar
on Unix, PowerArchiver
, WinRAR
,
WinZIP
or any similar software on Windows) to
produce an archive of the databases in your master's data directory.
Include all the databases you want to replicate.
tar -cvf /tmp/mysql-snapshot.tar /path/to/data-dirIf you want to replicate only a database called
this_db
, you
can do just this:
tar -cvf /tmp/mysql-snapshot.tar /path/to/data-dir/this_dbYou may not want to replicate the
mysql
database, then you can
exclude it from the archive too. Into the archive you needn't copy the
master's binary logs, error log,
`master.info' / `relay-log.info' / relay logs
(if the master is itself a slave of another machine). You can exclude
all this from the archive.
After or during the process of taking a snapshot, read the value of the
current binary log name and the offset on the master:
mysql > SHOW MASTER STATUS; +---------------+----------+--------------+-------------------------------+ | File | Position | Binlog_do_db | Binlog_ignore_db | +---------------+----------+--------------+-------------------------------+ | mysql-bin.003 | 73 | test,bar | foo,manual,sasha_likes_to_run | +---------------+----------+--------------+-------------------------------+ 1 row in set (0.06 sec)The
File
column shows the name of the log, while Position
shows
the offset. In the above example, the binary log value is
mysql-bin.003
and the offset is 73. Record the values - you will need
to use them later when you are setting up the slave.
Once you have taken the snapshot and recorded the log name and offset, you can
re-enable write activity on the master:
mysql> UNLOCK TABLES;If you are using InnoDB tables, ideally you should use the InnoDB Hot Backup tool that is available to those who purchase MySQL commercial licenses, support, or the backup tool itself. It will take a consistent snapshot without acquiring any locks on the master server, and record the log name and offset corresponding to the snapshot to be later used on the slave. More information about the tool is avalaible at http://www.innodb.com/hotbackup.html. Without the hot backup tool, the quickest way to take a snapshot of InnoDB tables is to shut the master server down and copy the InnoDB datafiles and logs, and the table definition files (
.frm
). To record the current log file
name and offset, you should do the following before you shut down the server:
mysql> FLUSH TABLES WITH READ LOCK; mysql> SHOW MASTER STATUS;And then record the log name and the offset from the output of
SHOW MASTER STATUS
as was shown earlier. Once you have recorded the
log name and the offset, shut the server down without unlocking the tables to
make sure it goes down with the snapshot corresponding to the current log file
and offset:
shell> mysqladmin -uroot shutdownAn alternative for both MyISAM and InnoDB tables is taking an SQL dump of the master instead of a binary copy like above; for this you can use
mysqldump --master-data
on your master and later run this SQL dump into your slave. This is
however slower than doing a binary copy.
If the master has been previously running without log-bin
enabled,
the values of log name and position displayed by SHOW MASTER
STATUS
or mysqldump
will be empty. In that case, record empty
string ('') for the log name, and 4 for the offset.
log-bin
if it is not there already
and server-id=unique number
in the [mysqld]
section. If those
options are not present, add them and restart the server.
It is very important that the id of the slave is different from
the id of the master. Think of server-id
as something similar
to the IP address - it uniquely identifies the server instance in the
community of replication partners.
[mysqld] log-bin server-id=1
server-id=<some unique number between 1 and 2^32-1 that is different from that of the master>replacing the values in <> with what is relevant to your system.
server-id
must be different for each server participating in
replication. If you don't specify a server-id, it will be set to 1 if
you have not defined master-host
, else it will be set to 2. Note
that in the case of server-id
omission the master will refuse
connections from all slaves, and the slave will refuse to connect to a
master. Thus, omitting server-id
is only good for backup with a
binary log.
skip-slave-start
.
You may want to start the slave server with option
log-warnings
, this way you will get more messages about
network/connection problems for example.
mysqldump
into the
mysql
). Make
sure that the privileges on the files and directories are correct. The
user which MySQL runs as needs to be able to read and write to
them, just as on the master.
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 offset>;replacing the values in <> with the actual values relevant to your system. The maximum string length for the above variables are:
MASTER_HOST | 60 |
MASTER_USER | 16 |
MASTER_PASSWORD | 32 |
MASTER_LOG_FILE | 255 |
mysql> START SLAVE;
After you have done the above, the slave(s) should connect to the master and catch up on any updates which happened since the snapshot was taken.
If you have forgotten to set server-id
for the slave you will get
the following error in the error log file:
Warning: one should set server_id to a non-0 value if master_host is set. The server will not act as a slave.
If you have forgotten to do this for the master, the slaves will not be able to connect to the master.
If a slave is not able to replicate for any reason, you will find error messages in the error log on the slave.
Once a slave is replicating, you will find a file called
`master.info' and one called `relay-log.info'
in the data directory. These two files
are used by the slave to keep track of how much
of the master's binary log it has processed. Do not remove or
edit these files, unless you really know what you are doing. Even in that case,
it is preferred that you use CHANGE MASTER TO
command.
NOTE: the content of `master.info' overrides some options specified on
the command-line or in `my.cnf' (see section 4.10.6 Replication Options in `my.cnf' for more details).
Now that you have a snapshot, you can use it to set up other slaves. To do so, follow the slave portion of the procedure described above. You do not need to take another snapshot of the master.
Posted by George Thiruva on Friday June 28 2002, @9:39am | [Delete] [Edit] |
LOAD DATA FROM MASTER (3.23.41 master and 4.01a slave) doesn't seem to load the mysql.* tables. On my installation it loads the data for additional databases but not the mysql stuff. As a result, the slave fails to start properly to catch up with subsequent updates from the master's binary log. The slave fails with its log file showing that it was unable to run SQL commands that modify data in the mysql.* tables. In my case, the slave's log shows that it stopped with a failure to run a revoke command that the master ran (since the user was never replicated in the first place to the client.) The old tar-ball trick seemed to work OK though.
Posted by lukas rueegg on Friday May 17 2002, @6:24am | [Delete] [Edit] |
V.4.0.1a: To use 'LOAD DATA FROM MASTER;' the
replication-
user has to have also the 'SELECT', 'RELOAD'
and 'PROCESS'
privileges and not only the 'FILE' privilege as
stated in the manual. Otherwise you'll get
an 'access denied' error from the master. From
the moment, where the replication is successfully
running, the 'SELECT', 'RELOAD' and 'PROCESS'
privileges
are no longer needed.
Posted by B.L. Choy on Wednesday December 18 2002, @5:27pm | [Delete] [Edit] |
For those who want their server to be both master
and slave at the same time, please note that you
should have only one 'server-id' in your my.cnf file; if
you copy the two sections of statements (Paras. 5
and 7) above into
your my.cnf , you will end up having a connection
problem like:
020821 17:34:47 Slave: connected to
master 'repl@192.168.60.1:3306', replication
started in log 'FIRST' at position 32
020821 17:34:47 Slave: received 0 length
packet from server, apparent master
shutdown:
020821 17:34:47 Slave: Failed reading log
event, reconnecting to retry, log 'FIRST' position
32
020821 17:34:47 Slave: reconnected to
master 'repl@192.168.60.1:3306',replication
resumed in log 'FIRST' at position 32
020821 17:34:47 Slave: received 0 length
packet from server, apparent master
shutdown:
Posted by marvel.master on Sunday September 22 2002, @5:38am | [Delete] [Edit] |
You do not need the line
master-port=<TCP/IP port for master>
of the slave, if you not changed the port. Normal :
3306
To add a user for the master server you need this
line
mysql> GRANT FILE ON *.* TO repl@"%"
IDENTIFIED BY '<password>';
You have to change "%" to the IP Adress from the
SLAVE.
Example: mysql> GRANT FILE ON *.* TO
repl@192.168.0.2 IDENTIFIED BY 'yourpass';
Posted by Jacob C on Wednesday December 18 2002, @5:27pm | [Delete] [Edit] |
A few things I came across while setting up
replication:
- Passwords can only be 16 characters long. This will
cause 'Access Denied' errors while trying to connect
to the master if set too long.
- When running replication numerous files are
created that can cause problems getting back on
track if something goes wrong. If there are
problems after you edit your my.cnf and restart
mysqld here's some cleaning up that needs to be
done while the server is shutdown (your file names
might differ):
1) On the slave (in the mysql data dir): remove
master.info file, remove all binary files created and
their indexes, remove the .err and .pid files, remove
the log.info file.
2) On the master (in the mysql data dir): remove all
binary files created and their indexes, remove
the .err and .pid files.
3) If for some reason you need to redo replication I
have found it is best to tar up the mnaster and put a
fresh copy of the database on the slave and start
again rather than trying to resolve every issue the
slave spits out. Although, it should be noted that this
is not always possible - it's a judgement call.
Posted by Aaron.Jackson on Friday October 4 2002, @12:54pm | [Delete] [Edit] |
I beat my head against the wall trying to figure
this one out:
These instructions assume that you do not
previously have binary loggin on the master
server. If you do have binary logging on (which
you should have if you follow the install
instructions), and you follow these instructions,
you will have problems.
For instance, if logging is on the master and you
create a database and then follow the replication
instructions, replication will not work. This is
because the replication process will try to
replicate the create database command on the slave
and fail because the database is already exists
(because you brought it over with the tar file
from the master).
To work around this you can either drop the
database on the slave or do a 'reset master;' on
the master (this will delete inactive binary logs
on the master so be careful).
Posted by Michael Babcock on Thursday October 31 2002, @1:05pm | [Delete] [Edit] |
Its worth mentionning that if your server-id
values are too large, the communication dies every
time it starts and you'll have to change the IDs,
then reset things to get started again.
Posted by [name withheld] on Tuesday February 11 2003, @4:51am | [Delete] [Edit] |
The line below:
> GRANT FILE ON *.* TO repl@"%" IDENTIFIED BY '<password>';
doesn't work correctly on v4.0. The correct privileges for this version are:
> GRANT REPLICATION SLAVE ON *.* TO repl@"%" IDENTIFIED BY '<password>';
Posted by Melvyn Sopacua on Friday February 14 2003, @4:05am | [Delete] [Edit] |
You cannot add REPLICATION SLAVE on a table level. You need to grant privileges to the entire database (kinda disturbing, since it would be a nice fallback for a bogus config file). It seems to apply to 'FILE' as well.
In fact - there's no REPLICATION SLAVE in the db table, so it probably should be ignored by the GRANT command as well, since it now gives you OK, while in fact there's nothing happening.
Posted by Nicolas Ross on Monday February 17 2003, @11:13am | [Delete] [Edit] |
Our Main mysql server (3.23.54) has many,many databases. Curently I run a replication setup for redundency purpose, and it's working well.
I want to setup a server with only one of the db's replicated. I can't grant a user file priv only one one db. Is there a way to setup so that the salve won't be able to read all of the dbs' ?
Posted by Justin Finkelstein on Tuesday February 25 2003, @1:46am | [Delete] [Edit] |
When setting up mysql replication, you may find it necessary to test your username and password settings; in my scenario, I do replication through an SSH tunnel to our live server. If you're doing this, there're a couple of things to note:
1. the GRANT FILE command should be set so that the replication user is allowed access from the server
2. when testing the username/password using the mysql login prompt, this will fail if you've followed the instructions above AND have specified a database to connect to. If you leave the dB name out, the login will work.
Posted by Michael Grabenstein on Tuesday April 22 2003, @1:54pm | [Delete] [Edit] |
Items 3 and 4 seem to be reversed, or at least part of 4 has to be done before you can accomplish all of #3 on a system with no previous replication set up. Specifically "show master status" won't show anything until you update the my.cnf as directed in step #4.
Also surprised the replication FAQ and here do not have anything about: "Error updating slave list: query error" in the error log. I am running 4.0.7-gamma and I had to still "grant file" before this error would go away (from step #2). Found at least a direction to go in from this site:
http://www.faqts.com/knowledge_base/index.phtml/fid/398
There they have a question: "What does the Error updating slave list: query error mean ?" ...
Also step 8 should probably mention something about master_port for people like me that have changed the Port number MySQL daemon is running on. Thanks to the earlier note on that...
Also in step #4 you might want to add a line for binlog-ignore-db=< > or at least mention it...
Posted by Patrick Greenwell on Saturday April 26 2003, @10:47am | [Delete] [Edit] |
Executing a FLUSH TABLES WITH READ LOCK doesn't block write queries globally across all of your databases. You have to connect to each database individually and execute the command for it to be effective. (at least on 3.23.56)
Posted by Patrick Greenwell on Saturday April 26 2003, @11:10am | [Delete] [Edit] |
The syntax for starting a slave thread is backwards in the documentation. It's "SLAVE START" not "START SLAVE" (again at least under 3.23.56)
Posted by Kayra Otaner on Thursday May 15 2003, @8:26pm | [Delete] [Edit] |
Tips for minimizing downtime on Hot MySQL Database servers :
- Start with increasing max_connections value in MySQL before restarting server for binary logging. This is necessary since when you lock tables all new connections will be in que for processing.
- Always copy update log to a safe place too. If you forget getting position of update log for some reason, you can always use mysqlbinlog utility to find out position on binary log.
- While getting copies of database files from MySQL
use unix 'cp' instead of tar. If possible try to use different destination hard drive instead of copying into same hard drive (eg don't copy database files on hda1 to hda1 try to copy hdb1)
Posted by [name withheld] on Tuesday June 3 2003, @10:22am | [Delete] [Edit] |
these instructions are erroneous and irresponsible -- just use common sense and you can figure it out but don't read these literally
Posted by Iuri Fiedoruk on Thursday July 17 2003, @9:08am | [Delete] [Edit] |
Here goes a tip that could saved me a LOT of problems if was here instead of in the CHANGE MASTER command page (where wasn't exactaly what I'm posting here besides):
to make a computer stoping to sync from another computer run STOP SLAVE, shutdown the server, delete `master.info'.
I had aproblem when the master previously was a slave, then when I set up the replication both where replicating from the other... just destroyied a database... :(
Posted by snix on Sunday July 27 2003, @6:21pm | [Delete] [Edit] |
Here is what I had to do to set up replication with LOAD DATA FROM MASTER:
1. Edit the my.cnf file on the slave and on the master server:
master my.cnf:
[mysqld]
server-id = 1
log-bin
slave my.cnf:
[mysqld]
server-id = 2
master-host = master-host.net
master-user = repl
master-password = repl_pass
replicate-do-db = database1
replicate-do-db = database2
log-warnings
2. Restart both the slave and the master.
3. On the master do:
GRANT SUPER,REPLICATION CLIENT,REPLICATION SLAVE,RELOAD ON *.* TO repl@"%" IDENTIFIED BY 'repl_pass';
4. On the slave do:
LOAD DATA FROM MASTER;
The slave should now download the databases specified with replicate-do-db in the slaves my.cnf, and be replicating new data.