MySQL Replication Commands:
SQL Statements for Controlling Master Servers
- PURGE MASTER LOGS Syntax
PURGE {MASTER | BINARY} LOGS TO ‘log_name‘
PURGE {MASTER | BINARY} LOGS BEFORE ‘date‘
Deletes all the binary logs listed in the log index prior to the specified log or date. The logs also are removed from the list recorded in the log index file, so that the given log becomes the first.
Example:
PURGE MASTER LOGS TO ‘mysql-bin.010′;
PURGE MASTER LOGS BEFORE ‘2003-04-02 22:46:26′;
To safely purge logs, follow this procedure:
1. On each slave server, use SHOW SLAVE STATUS to check which log it is reading.
2. Obtain a listing of the binary logs on the master server with SHOW BINARY LOGS.
3. Determine the earliest log among all the slaves. This is the target log. If all the slaves are up to date, this is the last log on the list.
4. Make a backup of all the logs you are about to delete. (This step is optional, but always advisable.)
5. Purge all logs up to but not including the target log.
- RESET MASTER Syntax
Deletes all binary logs listed in the index file, resets the binary log index file to be empty, and creates a new binary log file. This statement was named FLUSH MASTER before MySQL 3.23.26.
- SET SQL_LOG_BIN Syntax
SET SQL_LOG_BIN = {0|1}
Disables or enables binary logging for the current connection (SQL_LOG_BIN is a session variable) if the client that has the SUPER privilege. The statement is refused with an error if the client does not have that privilege. (Before MySQL 4.1.2, the statement was simply ignored in that case.)
- SHOW BINLOG EVENTS Syntax
SHOW BINLOG EVENTS
[IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
Shows the events in the binary log. If you do not specify ‘log_name‘, the first binary log is displayed. The LIMIT clause has the same syntax as for the SELECT statement. This statement is available as of MySQL 4.0.
- SHOW BINARY LOGS Syntax
SHOW BINARY LOGS
SHOW MASTER LOGS
Lists the binary log files on the server. This statement is used as part of the procedure described in Section 13.6.1.1, “PURGE MASTER LOGS Syntax”, that shows how to determine which logs can be purged.
mysql> SHOW BINARY LOGS;
+—————+———–+
| Log_name | File_size |
+—————+———–+
| binlog.000015 | 724935 |
| binlog.000016 | 733481 |
+—————+———–+
SHOW MASTER LOGS was added in MySQL 3.23.38. As of MySQL 4.1.1, you can also use SHOW BINARY LOGS, which is equivalent. The File_size column is displayed as of MySQL 5.0.7.
- SHOW MASTER STATUS Syntax
SHOW MASTER STATUS
Provides status information about the binary log files of the master.
Example:
mysql > SHOW MASTER STATUS;
+—————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+—————+———-+————–+——————+
| mysql-bin.003 | 73 | test | manual,mysql |
+—————+———-+————–+——————+
- SHOW SLAVE HOSTS Syntax
SHOW SLAVE HOSTS
Displays a list of replication slaves currently registered with the master. Only slaves started with the
–report-host=slave_name option are visible in this list.
The list is displayed on any server (not just the master server). The output looks like this:
mysql> SHOW SLAVE HOSTS;
+————+———–+——+———–+
| Server_id | Host | Port | Master_id |
+————+———–+——+———–+
| 192168010 | iconnect2 | 3306 | 192168011 |
| 1921680101 | athena | 3306 | 192168011 |
+————+———–+——+———–+
• Server_id: The unique server ID of the slave server, as configured in the server’s option file, or on the command line with –server-id=value.
• Host: The host name of the slave server, as configured in the server’s option file, or on the command line with –report-host=value. Note that this can differ from the machine name as configured in the operating system.
• Port: The port the slave server is listening on.
• Master_id: The unique server ID of the master server that the slave server is replicating from. Some MySQL versions report another variable, Rpl_recovery_rank. This variable was never used, and was eventually removed.
SQL Statements for Controlling Slave Servers
- CHANGE MASTER TO Syntax
CHANGE MASTER TO master_def [, master_def] …
master_def:
MASTER_HOST = ‘host_name‘
| MASTER_USER = ‘user_name‘
| MASTER_PASSWORD = ‘password‘
| MASTER_PORT = port_num
| MASTER_CONNECT_RETRY = count
| MASTER_LOG_FILE = ‘master_log_name‘
| MASTER_LOG_POS = master_log_pos
| RELAY_LOG_FILE = ‘relay_log_name‘
| RELAY_LOG_POS = relay_log_pos
| MASTER_SSL = {0|1}
| MASTER_SSL_CA = ‘ca_file_name‘
| MASTER_SSL_CAPATH = ‘ca_directory_name‘
| MASTER_SSL_CERT = ‘cert_file_name‘
| MASTER_SSL_KEY = ‘key_file_name‘
| MASTER_SSL_CIPHER = ‘cipher_list‘
CHANGE MASTER TO changes the parameters that the slave server uses for connecting to and communicating with the master server. It also updates the contents of the master.info and relay- log.info files.MASTER_USER, MASTER_PASSWORD, MASTER_SSL, MASTER_SSL_CA, MASTER_SSL_CAPATH, MASTER_SSL_CERT, MASTER_SSL_KEY, and MASTER_SSL_CIPHER provide information to the slave about how to connect to its master. The relay log options (RELAY_LOG_FILE and RELAY_LOG_POS) are available beginning with MySQL 4.0.
You want to set up the slave to replicate the master:
CHANGE MASTER TO
MASTER_HOST=’master2.mycompany.com’,
MASTER_USER=’replication’,
MASTER_PASSWORD=’bigs3cret’,
MASTER_PORT=3306,
MASTER_LOG_FILE=’master2-bin.001′,
MASTER_LOG_POS=4,
MASTER_CONNECT_RETRY=10;
- MASTER_POS_WAIT() Syntax
SELECT MASTER_POS_WAIT(‘master_log_file‘, master_log_pos)
This is actually a function, not a statement. It is used to ensure that the slave has read and executed events up to a given position in the master’s binary log. See Section 12.9.4, “Miscellaneous Functions”, for a full description.
- RESET SLAVE Syntax
RESET SLAVE
RESET SLAVE makes the slave forget its replication position in the master’s binary logs. This statement is meant to be used for a clean start: It deletes the master.info and relaylog. info files, all the relay logs, and starts a new relay log.
Note: All relay logs are deleted, even if they have not been completely executed by the slave SQL thread. (This is a condition likely to exist on a replication slave if you have issued a STOP SLAVE statement or if the slave is highly loaded.) Connection information stored in the master.info file is immediately reset using any values specified in the corresponding startup options. This information includes values such as master host, master port, master user, and master password. If the slave SQL thread was in the middle of replicating temporary tables when it was stopped, and RESET SLAVE is issued, these replicated temporary tables are deleted on the slave. This statement was named FLUSH SLAVE before MySQL 3.23.26.
- SET GLOBAL SQL_SLAVE_SKIP_COUNTER Syntax
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = N
This statement skips the next N events from the master. This is useful for recovering from replication stops caused by a statement. This statement is valid only when the slave thread is not running. Otherwise, it produces an error. Before MySQL 4.0, omit the GLOBAL keyword from the statement.
- SHOW SLAVE STATUS Syntax
SHOW SLAVE STATUS
This statement provides status information on essential parameters of the slave threads. If you issue this statement using the mysql client, you can use a \G statement terminator rather than a semicolon to obtain a more readable vertical layout:
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: localhost
Master_User: root
Master_Port: 3306
Connect_Retry: 3
Master_Log_File: gbichot-bin.005
Read_Master_Log_Pos: 79
Relay_Log_File: gbichot-relay-bin.005
Relay_Log_Pos: 548
Relay_Master_Log_File: gbichot-bin.005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 79
Relay_Log_Space: 552
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 8
Depending on your version of MySQL, you may not see all the fields just shown. In particular, several fields are present only as of MySQL 4.1.1. SHOW SLAVE STATUS returns the following fields:
• Slave_IO_State
A copy of the State field of the output of SHOW PROCESSLIST for the slave I/O thread.This tells you what the thread is doing: trying to connect to the master, waiting for events from the master, reconnecting to the master, and so on. Possible states are listed in Section 6.3, “Replication Implementation Details”. It is necessary to check this field for older versions of MySQL (prior to 4.1.14) because in these versions the thread could be running while unsuccessfully trying to connect to the master; only this field makes you aware of the connection problem.
The state of the SQL thread is not copied because it is simpler. If it is running, there is no problem; if it is not, you can find the error in the Last_Error field (described below). This field is present beginning with MySQL 4.1.1.
• Master_Host
The current master host.
• Master_User
The current user used to connect to the master.
• Master_Port
The current master port.
• Connect_Retry
The current value of the –master-connect-retry option.
• Master_Log_File
The name of the master binary log file from which the I/O thread is currently reading.
• Read_Master_Log_Pos
The position up to which the I/O thread has read in the current master binary log.
• Relay_Log_File
The name of the relay log file from which the SQL thread is currently reading and executing.
• Relay_Log_Pos
The position up to which the SQL thread has read and executed in the current relay log.
• Relay_Master_Log_File
The name of the master binary log file containing the most recent event executed by the SQLthread.
• Slave_IO_Running
Whether the I/O thread is started and has connected successfully to the master. For older versions of MySQL (prior to 4.1.14 and 5.0.12) Slave_IO_Running is YES if the I/O thread is started, even if the slave hasn’t connected to the master yet.
• Slave_SQL_Running
Whether the SQL thread is started.
• Replicate_Do_DB, Replicate_Ignore_DB
The lists of databases that were specified with the –replicate-do-db and - -replicate-ignore-db options, if any. These fields are present beginning with MySQL 4.1.1.
• Replicate_Do_Table, Replicate_Ignore_Table,
Replicate_Wild_Do_Table, Replicate_Wild_Ignore_Table
The lists of tables that were specified with the –replicate-do-table, –replicate-ignore-table, –replicate-wild-do-table, and –replicate-wild-ignore_table options, if any.These fields are present beginning with MySQL 4.1.1.
• Last_Errno, Last_Error
The error number and error message returned by the most recently executed query. An error number of 0 and message of the empty string mean “no error.” If the Last_Error value is not empty, it also appears as a message in the slave’s error log. For example:
Last_Errno: 1051
Last_Error: error ‘Unknown table ‘z” on query ‘drop table z’
The message indicates that the table z existed on the master and was dropped there, but it did not exist on the slave, so DROP TABLE failed on the slave. (This might occur, for example, if you forget to copy the table to the slave when setting up replication.)
• Skip_Counter
The most recently used value for SQL_SLAVE_SKIP_COUNTER.
• Exec_Master_Log_Pos
The position of the last event executed by the SQL thread from the master’s binary log
(Relay_Master_Log_File). (Relay_Master_Log_File, Exec_Master_Log_Pos) in the master’s binary log corresponds to (Relay_Log_File, Relay_Log_Pos) in the relay log.
• Relay_Log_Space
The total combined size of all existing relay logs.
• Until_Condition, Until_Log_File, Until_Log_Pos
The values specified in the UNTIL clause of the START SLAVE statement.
Until_Condition has these values:
• None if no UNTIL clause was specified
• Master if the slave is reading until a given position in the master’s binary logs
• Relay if the slave is reading until a given position in its relay logs Until_Log_File and Until_Log_Pos indicate the log filename and position values that define the point at which the SQL thread stops executing. These fields are present beginning with MySQL 4.1.1.
• Master_SSL_Allowed, Master_SSL_CA_File, Master_SSL_CA_Path, Master_SSL_Cert, Master_SSL_Cipher, Master_SSL_Key
These fields show the SSL parameters used by the slave to connect to the master, if any. Master_SSL_Allowed has these values:
• Yes if an SSL connection to the master is permitted
• No if an SSL connection to the master is not permitted
• Ignored if an SSL connection is permitted but the slave server does not have SSL support enabled The values of the other SSL-related fields correspond to the values of the –master-ca, - -master-capath, –master-cert, –master-cipher, and –master-key options.These fields are present beginning with MySQL 4.1.1.
• Seconds_Behind_Master
This field is present beginning with MySQL 4.1.1. It is been experimental and has been changed in MySQL 4.1.9. The following applies to slaves running MySQL 4.1.9 or newer. This field is an indication of how “late” the slave is:
• When the slave SQL thread is actively running (processing updates), this field is the number of seconds that have elapsed since the timestamp of the most recent event on the master executed by that thread.
- START SLAVE Syntax
START SLAVE [thread_type [, thread_type] … ]
START SLAVE [SQL_THREAD] UNTIL
MASTER_LOG_FILE = ‘log_name‘, MASTER_LOG_POS = log_pos
START SLAVE [SQL_THREAD] UNTIL
RELAY_LOG_FILE = ‘log_name‘, RELAY_LOG_POS = log_pos
thread_type: IO_THREAD | SQL_THREAD
START SLAVE with no thread_type options starts both of the slave threads. The I/O thread reads queries from the master server and stores them in the relay log. The SQL thread reads the relay log and executes the queries. START SLAVE requires the SUPER privilege. If START SLAVE succeeds in starting the slave threads, it returns without any error. However, even in that case, it might be that the slave threads start and then later stop (for example, because they do not manage to connect to the master or read its binary logs, or some other problem).
START SLAVE does not warn you about this. You must check the slave’s error log for error messages generated by the slave threads, or check that they are running satisfactorily with SHOW SLAVE STATUS. As of MySQL 4.0.2, you can add IO_THREAD and SQL_THREAD options to the statement to name which of the threads to start. As of MySQL 4.1.1, an UNTIL clause may be added to specify that the slave should start and run until the SQL thread reaches a given point in the master binary logs or in the slave relay logs. When the SQL thread reaches that point, it stops. If the SQL_THREAD option is specified in the statement, it starts only the SQL thread. Otherwise, it starts both slave threads. If the SQL thread is running, the UNTIL clause is ignored and a warning is issued.For an UNTIL clause, you must specify both a log filename and position. Do not mix master and relay log options.
Any UNTIL condition is reset by a subsequent STOP SLAVE statement, a START SLAVE statement that includes no UNTIL clause, or a server restart. The UNTIL clause can be useful for debugging replication, or to cause replication to proceed until just before the point where you want to avoid having the slave replicate a statement. For example, if an unwise DROP TABLE statement was executed on the master, you can use UNTIL to tell the slave to execute up to that point but no farther. To find what the event is, use mysqlbinlog with the master logs or slave relay logs, or by using a SHOW BINLOG EVENTS statement. If you are using UNTIL to have the slave process replicated queries in sections, it is recommended that you start the slave with the –skip-slave-start option to prevent the SQL thread from running when the slave server starts. It is probably best to use this option in an option file rather than on the command line, so that an unexpected server restart does not cause it to be forgotten. The SHOW SLAVE STATUS statement includes output fields that display the current values of the UNTIL condition. This statement is called SLAVE START before MySQL 4.0.5. SLAVE START is still accepted for backward compatibility, but is now deprecated.
- STOP SLAVE Syntax
STOP SLAVE [thread_type [, thread_type] … ]
thread_type: IO_THREAD | SQL_THREAD
Stops the slave threads. STOP SLAVE requires the SUPER privilege. Like START SLAVE, as of MySQL 4.0.2, this statement may be used with the IO_THREAD and SQL_THREAD options to name the thread or threads to be stopped. This statement is called SLAVE STOP before MySQL 4.0.5. SLAVE STOP is still accepted for backward compatibility, but is deprecated.
February 13, 2009 at 12:56 pm
for a some reason at replication process… is there any posibilities to read status some global variables in a master at a slave ?, for instance…..i’ve set var global at a master.. named G_SKIP_INSERT values ‘Y’…could a trigger in slave read that vars and values ?
regards
pwt