How do i know if mysql is master or slave?

There is an interesting way to report all registered slaves connect to the master.

The command is called SHOW SLAVE HOSTS;

This will not directly show the IP of the slaves but you can configure the master and slaves to do so in a unique way.

With MySQL 5.5, just run SHOW SLAVE HOSTS; and you just get something like this:

MySQL> show slave hosts;
+-----------+------+------+-----------+
| Server_id | Host | Port | Master_id |
+-----------+------+------+-----------+
| 106451148 |      | 3306 | 106451130 |
+-----------+------+------+-----------+
1 row in set (0.00 sec)

MySQL> show variables like 'server_id';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| server_id     | 106451130 |
+---------------+-----------+
1 row in set (0.00 sec)

As shown
column 1 is the Slave's server-id
column 2 is the Slave's name as specfied in report-host variable (blank by default)
column 3 is the Slave's port number connecting to master
column 4 is the Slave's Master server-id (run this from the Master)

With versions MySQL 5.1 and back, you get this by default:

MySQL> show slave hosts;
Empty set (0.01 sec)

MySQL>

You can assign a hostname to each slave by adding this to the slave's /etc/my.cnf

report-host=MySQLSlave_10.1.2.3

Restart mysql and hopefully the name will appear as you typed it in /etc/my.cnf
If the periods are not acceptable, make them dashes like this:

report-host=MySQLSlave_10-1-2-3

Then do the following

  1. SHOW SLAVE HOSTS;
  2. Use the PHP explode function, delimiting by underscore character, and take the second element of the array
  3. Use the PHP function str_replace, replacing dash (-) with period (.)

And WA LA, you have an IP address

Script To Check If MySQL Master Master Replication Is Working Correctly

This short article explains how you can use a short script to check whether your MySQL master master replication is working as expected or not.

First add a user named "check" with the following mysql queries:

CREATE USER 'check'@'localhost' IDENTIFIED BY 'YOURPASSWORD';

GRANT SUPER , REPLICATION CLIENT ON * . * TO 'check'@'localhost' IDENTIFIED BY 'YOURPASSWORD' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;

Then add this bash script to your /root directory. Add it to your crontab if needed :

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

###check if already notified###
cd /root
if [ -f slave_problem.txt ]; then
exit 1;
fi

###Check if slave running###
(
echo "show slave status \G;"
) | mysql -u check -pYOURPASSWORD 2>&1 | grep "Slave_IO_Running: No"
if [ "$?" -ne "1" ]; then
echo "Replication failed" > /root/slave_problem.txt
fi

###Send notification if replication down###
cd /root
if [ -f slave_problem.txt ]; then
mail -s "Replication problem" [email protected] < /root/slave_problem.txt
fi

Suggested articles

5 Comment(s)

Comments

×

This feature is only available to subscribers. Get your subscription here.

Don’t make the mistake of understanding Mysql cluster in the same way we know Hadoop HA

How do i know if mysql is master or slave?

Let's take one example of a simple SQL statement fired from client to the master server. “ update player set name=’THIAGO’ where shirt_no = ‘6’ ”

player table will be updated in the database of the master server. Now, this event will also be stored in another file which is a binary log file(only if binary logging is enabled on the server). Now, this log file will help the slave server in replicating this update event. Basically binary log file contains all the events to the SQL server along with some enough meta information/data to replicate the same events on the corresponding slave servers.

On the slave server, two threads keep running as part of the replication process.

Slave IO thread: This thread responsible for copying bin log files from the master server and save it into the relay log files on the slave server.

Slave SQL thread: This thread does not interact with the master server. It executes all the events which are copied into relay logs and as a result of this execution, slave database gets updated.

These two threads which are running on the slave server are responsible for keeping the slave database in sync with the master.

show slave status;

You can check the status of IO and SQL thread by running the above command on the slave server. Look for the Slave_IO_Running & Slave_SQL_Running keys in output. Apart from these, you will see some other keys in the output.

Master_Log_File: Name of bin log file on the master server which is being copied by slave IO thread. For ex. mysql-bin.000082

Read_Master_Log_Pos: This tells us till what position slave IO thread has copied from Master_Log_File. Ex: 92, it means slave IO thread has copied 92 events/transactions from mysql-bin.000082 bin log file from the master server and saved it into Relay_Log_File(Ex. mysql-relay.00003).

Slave SQL thread keeps executing transactions from Relay_Log_File. Now below given values will explain other part of the story which is “how much data has been replicated on the slave server”

Relay_Log_File: Name of relay log file on slave server from which slave SQL thread is currently executing events/transactions. For ex. mysql-relay.00001

Relay_Log_Pos: Position upto which slave SQL thread has executed events from Relay_Log_File. For ex. 101, it means slave SQL thread has executed 101 events from mysql-relay.00001 log file.

Relay_Master_Log_File&Exec_Master_Log_Pos: Now these 2 values gives us the information in a relative manner. Explaining these values separately would create confusion as these values are like co-ordinates which tells us how much data has been replicated from master’s perspective instead of slave’s perspective. Don’t confuse it with the Master_Log_File(which is the log file on the master which slave IO thread is currently copying, it could be mysql-bin.000082), but Relay_Master_Log_File could still be mysql-bin.000080. It means from master’s perspective bin log files till 000082 have been copied to slave server but events/transactions have only been executed till Exec_Master_Log_Pos of Relay_Master_Log_File(mysql-bin.000080 in our example). So it means the slave is still not in sync with the master.

How to ensure that slave is 100 percent in sync with the master server?

Now we know that Relay_Log_File, Relay_Log_Pos alone could not tell about the completion of data replication. Just follow these steps to know whether master-slave are in sync or not.

  1. Run “show master status” command on the master server and note the value of File, Postion from the output.
  2. Run “show slave status” command on the slave server and note the value of Relay_Master_Log_File,Exec_Master_Log_Pos from the output.
  3. Run “show slave status” command on the slave server and note the value of Slave_IO_Running & Slave_SQL_Running.
  4. Slave_IO_Running should be Yes, Slave_SQL_Running should be Yes, filename and position which we got from 1st and 2nd step should be same(File == Relay_Master_Log_File && Postion == Exec_Master_Log_Pos).
  5. If step 4 is holding true then your master and slave SQL servers are completely in sync with each other.

A typical Mysql cluster has 3 machines where 1 acts as a master and 2 machines run as a slave server. In a practical scenario, it could happen that one of the slave machines went for maintenance or becomes unserviceable(as we know cloud services keeps taking down our instances for maintenance or just kill them). Keep in mind that slaves do not know each other and they only interact with the master. Now when the machine comes back from maintenance then we need to restart the MySQL server and slave process.

Now there is one problem, if expire_logs_days config on the master server has X days as a value and you are doing this restart activity on slave after X days then the slave can never ever be in sync with the master. After X days (or expire_logs_days ) Mysql server purges all the bin log files. So here slave IO thread will copy new bin log files from the master server and when the slave SQL thread will try to execute events then errors will come as old bin log events have not been executed yet on the slave due to which data is inconsistent on the slave. In this case, you have to rebuild the slave again.

How do I check the status of a slave in MySQL?

TO ‘slave’@’12.34.56.222 ‘; mysql> FLUSH PRIVILEGES; mysql> FLUSH TABLES WITH READ LOCK; You will use the following command to know the current status of the master server: This command will also tell the slave to follow the master from this position.

How do I test if my MySQL master slave replication works?

To test if your MySQL master slave replication works, just create a database in your master server and see if it is replicated in the slave server. If you can see the database in the slave, then it is working fine. Create a test database in a master server called ‘sampledb’.

How to stop MySQL in the slave server?

Once the data is imported, you need to stop MySQL in the slave server using the following command: You have finally imported the dump files and updated the master IP address, password, log file name, and position, to enable the master to communicate with the slave without any issues.

What is slave_Io_running in MySQL?

According to MySQL doc - Checking Replication Status: Slave_IO_Running: Whether the I/O thread for reading the master's binary log is running. Normally, you want this to be Yes unless you have not yet started replication or have explicitly stopped it with STOP SLAVE.

How can I tell if MySQL is master or slave?

Check MySQL Replication Status on Query Servers.
Start the MySQL command-line utility on the slave server: # cd /opt/mysql/mysql/bin. ... .
Check the replication status using the show slave status command (the status of the slave server is conveyed by the Slave_IO_Running and Slave_SQL_Running column values):.

How do I know if my DB is master?

OPTION #3 : Check for presence of master.info If replication is setup on a DB Server, look for master.info. By default, master.info is usually in /var/lib/mysql or wherever datadir is defined. Simply run 'cat master.info' multiple times (For Windows community, type master.info).

What is slave and master in MySQL?

What Is Master-Slave Replication? The master-slave replication process enables database administrators to replicate or copy data stored in more than one server simultaneously. This helps the database administrator to create a live backup of the database all the time.

How do I determine MySQL version?

To check the version your MySQL is running, type and execute mysql -V (note the uppercase V) in the command line. As you can see, the MySQL version for this system is 10.4. 12.