Posts Tagged ‘Mysql’

How to insert data in MySQL Table

September 28th, 2011

Let’s start inserting data in the table created in our post, table name was employee, we also discussed how to check the table structure in one of our post

Step 1) Login to MySQL

we will be using user : demo and password as demouser

mysql -u demo -p demouser

Step 2) use database in which we have created table

mysql> use demo;

Step 3) check the structure of the database

mysql> desc employee;
+————–+————–+——+—–+———+——-+
| Field        | Type         | Null | Key | Default | Extra |
+————–+————–+——+—–+———+——-+
| id           | int(11)      | YES  |     | NULL    |       |
| employeename | varchar(252) | YES  |     | NULL    |       |
| address      | varchar(256) | YES  |     | NULL    |       |
+————–+————–+——+—–+———+——-+
3 rows in set (0.07 sec)

so this tells us that the table has 3 columns id , employeename  and address , lets start by writing the insert statement  syntax used will be

Insert into employee(id ,employeename , address) values ( 1, ‘DemoEmployee’, ‘US’) ;

mysql> Insert into employee(id ,employeename , address) values ( 1, ‘DemoEmployee’, ‘US’) ;

Query OK, 1 row affected (0.02 sec)

Lets check if the record is been inserted properly.

mysql> select * from employee;

+——+————–+———+

| id   | employeename | address |

+——+————–+———+

|    1 | DemoEmployee | US      |

+——+————–+———+

1 row in set (0.00 sec)

Excellent we have successfully inserted out first record.

How to get list of tables in a MySQL database

September 28th, 2011

lets discuss now  how we can check  how many tables exist in a MySQL database .

Step 1) Login to MySQL

we will be using user : demo and password as demouser

mysql -u demo -p demouser

Step 2) use database in which we need to check table , as in our previous post we created database demo we will be using same database

mysql> use demo;

Step 3) we will use command show tables ;

+—————-+
| Tables_in_demo |
+—————-+
| employee       |
+—————-+
1 row in set (0.00 sec)

this will list all the tables which are under demo database.

In our next post we will discuss how to insert data in a MySQL Table.

How to Check table structure in MySQL

September 28th, 2011

in our previous  post we discussed how to  add more columns in MySQL table , lets see how we can check table structure in MySQL.

Step 1) Login to MySQL

we will be using user : demo and password as demouser

mysql -u demo -p demouser

Step 2) use database in which we need to check table , as in our previous post we created database demo we will be using same database

mysql> use demo;

Step 3) we have 2 methods of checking the table structure.

a) Using Desc , we will now check the table structure of table employee

mysql> desc employee;
+————–+————–+——+—–+———+——-+
| Field        | Type         | Null | Key | Default | Extra |
+————–+————–+——+—–+———+——-+
| id           | int(11)      | YES  |     | NULL    |       |
| employeename | varchar(252) | YES  |     | NULL    |       |
| address      | varchar(256) | YES  |     | NULL    |       |
+————–+————–+——+—–+———+——-+
3 rows in set (0.07 sec)

This command lists all the columns and data type used for each column.

b) Using show create table command

syntax : show create table tablename

mysql> show create table employee;

+———-+———————————————————————————————————————————————————————————+

| Table    | Create Table                                                                                                                                                                    |

+———-+———————————————————————————————————————————————————————————+

| employee | CREATE TABLE `employee` (

`id` int(11) default NULL,

`employeename` varchar(252) default NULL,

`address` varchar(256) default NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1 |

+———-+———————————————————————————————————————————————————————————+

1 row in set (0.00 sec)

This command gives us create table structure for table employee.

In next post we will discuss how to check how many tables exists in a database.

How to add more columns in exsiting MySQL table

September 27th, 2011

in our previous  post we discussed how to create table in MySQL Database , lets check now how we can add more columns to existing table .

Step 1) Login to MySQL

we will be using user : demo and password as demouser

mysql -u demo -p demouser

Step 2) use database in which we need to create table , as in our previous post we created database demo we will be using same database

mysql> use demo;

Step 3)  we created table employee , now we will be looking how to add more columns in existing table .lets add one more column address having data type varchar and length as 256

mysql> alter table employee add column address varchar(256) ;

Query OK, 0 rows affected (0.07 sec)

Records: 0  Duplicates: 0  Warnings: 0

here we go we can see that the table is being altered . in our next post we will be discussing how to check the  existing table structure.

mysqld_safe A mysqld process already exists

March 5th, 2011

Today I faced   issue again with MySQL running of NFS .I was trying to shutwon mysql

using /etc/rc.d/init.d/mysql   stop

MySQL shutdown was successful .

after making changes in my.cnf file I tried to start MySQL using

/usr/bin/mysqld_safe –defaults-file=/etc/my.cnf &

I got error

mysqld_safe A mysqld process already exists

I checked that no other instance is running using

ps aux| grep MySQL

I got no MySQL process running, as I have faced so many errors because of the NFS

locking the files mentioned in my previous posts

http://onaxer.com/blog/blog/2011/02/26/error-1033-hy000-incorrect-information-in-file/

http://onaxer.com/blog/blog/2011/02/23/innodb-unable-to-lock-ib_logfile0-error-11/

http://onaxer.com/blog/blog/2010/06/01/innodb-unable-to-lock-ibdata1-error-37/

I tried the same solution again

1) /etc/init.d/portmap restart
2) /etc/init.d/nfslock restart

And it worked. I was able to start MySQL after that successfully.

ERROR 1033 (HY000): Incorrect information in file

February 26th, 2011

I got this error while restoring data from Netapp snapshot backup , I restored the last days backup

on a new directory on the NFS , after the restoring the MySQL data files from Netapp I tried to start

the mysql , MySQL Started , I noticed that queries are failing , on checking the error log

I got the error ERROR 1033 (HY000): Incorrect information in file for an Innodb table

I realised that I had not created the tmp file as per given in the my.cnf , I created the /tmp

file made mysql owner of the /tmp file , restarted the MySQL and error was gone.

the other possible reasons are
1) actual corruption of the table
2) Innodb plugin regsitration failed. reasons may be various

How to quickly Restore a single table from MySQLDump Backup

February 24th, 2011

Recently I had to recover a table with less then 200 records from backup file take from mysqldump of 50 GB. Normally there are different approaches which we can use to get the data from mysqldump for single table or set of tables.

1) Restore the mysqldump file on another server or same server with different name , then take the mysqldump of the tables using –tables option of mysqldump , then restore this dump file on the database we want. In mycase time was very important and I could not afford waiting for 50 Gb dump file to restore and then take backup and then restore , so this option was out of question.

2) using awk the logic here is that mysqldump has the table structure in the dump and then the actual insert commands , so we need to know the sequence of the tables in the mysqldump which will be same as of the tables listing in any tool in case you do show tables so say the table sequence in the show tables is

table1
table2
table3

and we need to restore the table2 , structure of table can be figure out using

show create table tablename

awk can be used like this

$ awk ‘/ structure for table .table2./,/structure for table .table3./{print}’ mydumpfile.sql > /tmp/recovered_table.sql
this will give all the insert commands for that table that can be restore in mysql.

This approach also could not work for me as it will take some time to parse the 50GB mysqldump.

3) Using mysql privileges .
what I did was used the mysql privileges , I created a table with the same name and structure on another Database say RecoverDB
I created a new user say recovery and in the RecoverDB database I gave all permission on the specific table only , I used the –force option of mysql which  keeps the restore process continue even if there is an error in the restoration process .now from the mysqldump file I did the restoration on the  RecoverDB

mysql -urecovery -ppassword RecoverDB </path of mysqldump file

as I have given write permission to only the table which I want to restore , restoration fails for all other tables except the one where
I have given priviledge I was able to restore the table in 2 mins, it was quick isn’t it . then I took the mysqldump of this table and restored on the actual DB where I wanted to restore. There might be other approaches as well and depending upon the platform (Windows, linux,solaris) the  (perl,awk) might  not be available (windows) but 3rd approach works irrespective of the platform.

InnoDB: Unable to lock ./ib_logfile0, error: 11

February 23rd, 2011

I got the error after an hardware failure on MySQL server   while I tried to restart  MySQL server innodb was not showing as a plugin , on checking the error log I got “InnoDB: Unable to lock ./ib_logfile0, error: 11 “  as I use NFS to store the mysql directory  , from my previous expierence which is explained in http://onaxer.com/blog/blog/2010/06/01/innodb-unable-to-lock-ibdata1-error-37 ,

I tried doing these things

first -  /etc/init.d/portmap restart

Second – /etc/init.d/nfslock restart

restarted the mysql and it worked like a charm.

How to enable slow query log in MySQL 5.0

January 12th, 2011

Entries required in my.cnf

log-slow-queries

If no file_name value is given for log-slow-queries, the default name is host_name-slow.log. The server creates the file in the data directory unless an absolute path name is given to specify a different directory. if we want to give the specific file name then give the path of the filename

log-slow-queries=/var/log/slow.log

in case we want to log the queries which do not use indexes

log-queries-not-using-indexes

to set the long query time in seconds , 1 sec is ideal

long_query_time =1

in mysql 5.0  long_query_time  can be changed dynamically

set global long_query_time=1

Restart MySQL and slow query log will be enabled.

How to enable performance_schema in MySQL 5.5

December 29th, 2010

One of the cool feature of MySQL 5.5 performance schema is not enabled by default to enable it in the my.cnf for linux add variable under mysqld

[mysqld]
performance_schema

to check if the performance_schema 

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

enjoy mysqling ...

How to modify column definition in MySQL

November 22nd, 2010

assume we want to change column named address from varchar(1000) to varchar(512) for table  employee

alter table  employee  modify address  varchar(512)

it will modify the column length from 1000 to varchar(512)

How to Change Innodb Log File

November 19th, 2010

How to Change Innodb Log File
Step 1
Have a clean MySQL Server Stop , check error log for any error.
Step 2
change value of innodb_log_file_size  in my.cnf file as you want it
Step 3
Move/rename the log file sizes ib_log*  .
Step 4
Restart MySQL Server.
Step 5
Check MySQL error log for any error .
Step 6
Check Innodb Log File size it should be changed

How to fix corrrupted relay log

November 13th, 2010

When we get this type of error and MySQL replicaton stops

Last_Error: Could not parse relay log event entry. The possible reasons are: the master’s binary log is corrupted (you can check this by running ‘mysqlbinlog’ on the binary log), the slave’s relay log is corrupted (you can check this by running ‘mysqlbinlog’ on the relay log), a network problem, or a bug in the master’s or slave’s MySQL code. If you want to check the master’s binary log or slave’s relay log, you will be able to know their names by issuing ‘SHOW SLAVE STATUS’ on this slave.

Possible reasons of the relay log corruption

1)      Network

2)      MySQL bugs on master/Slave

3)      Hardware problems

4)      max_allowed_packet value reset without restarting the replication

Solution:

Run show slave status  and get these values Relay_Master_Log_File and Exec_Master_Log_Pos, Now reset the replication on slave

CHANGE master TO master_log_file=Relay_Master_Log_File,master_log_pos=Exec_Master_Log_Pos ;

Start Slave ;

we are good to go, issue should be fixed now.for this to work Relay_Master_Log_File file must exist on master.

Increasing MySQL Insert capacity

November 11th, 2010

I have seen people struggling to increase the insert capacity in MySQL,based on the setup of Mysql Stand alone and Replication normally does not help in increasing the insert capacity because of the single threaded replication , normally people think that active master master replication will increase  the insert capacity which is wrong .xaprb has written a nice article on this

http://www.xaprb.com/blog/2008/08/06/how-to-scale-writes-with-master-master-replication-in-mysql/

there are three methods which help in increasing the insert capacity
Option 1
use MySQL’s multi values capability with INSERT

INSERT INTO table (col1, col2, col3) VALUES (?, ?, ?),  (?, ?, ?), (?, ?, ?);
as this reduces the network overhead and also saves time opening and closing tables for each insert which every high insert site will make goos difference plus the index update happens once rather then n times of inserts.

things to consider

max_allowed_packet for large multi insert  statement if you are using replication this needs to be modified on all the servers used  in replication
Option 2

batch load your data from a flat file. Depending on how you source the information you are inserting, that may also be a significant improvement in throughput in bulk inserting data.

LOAD DATA [CONCURRENT] INFILE ‘sourcefile’
INTO TABLE (col1, col2, col3)

there are other non ANSI options including DELAYED, IGNORE and LOW_PRIORITY.

Master-Master Mysql Replication

September 14th, 2010

MySQL Master-Master Replication
In my Setup I will explains how you can set up MySQL master-master replication on four MySQL nodes .The difference to a two node master-master replication is that if you have more than two nodes, the replication goes in a circle, i.e., with four nodes, the replication goes from node1 to node2, from node2 to node3, from node3 to node4, and from node4 to node1. The advantages of master-master replication over the traditional master-slave replication is that you don’t have to modify your applications to make write accesses only to the master, and that it is easier to provide high-availability because if the master fails, you still have the other master.

I will show how to replicate the database “sampled” on four MySQL nodes:
• machine1.domain.com: IP address 172.16.1.100
• machine2.domain.com: IP address 172.16.1.101
• machine3.domain.com: IP address 172.16.1.102
• machine4.domain.com: IP address 172.16.1.103
Each node is a master and a slave at the same time. Replication will work in a circle, i.e., the replication goes from machine1 to machine2, from machine2 to machine3, from machine3 to machine4, and from machine4 back to machine1:
… –> machine1 –> machine2 –> machine3 –> machine4 –> machine1 –> …

If MySQL 5.0 isn’t already installed on machine1 to machine4, install it now:
machine1/machine2/machine3/machine4:
yum install mysql-server-5.0 mysql-client-5.0
To make sure that the replication can work, we must make MySQL listen on all interfaces, therefore we comment out the line bind-address = 127.0.0.1 in /etc/mysql/my.cnf:
machine1/machine2/machine3/machine4:
vi /etc/mysql/my.cnf
[...]
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
[...]
Restart MySQL afterwards:
machine1/machine2/machine3/machine4:
/etc/init.d/mysql restart
Then check with
machine1/machine2/machine3/machine4:
netstat -tap | grep mysql
that MySQL is really listening on all interfaces:
machine1:~# netstat -tap | grep mysql
tcp 0 0 *:mysql *:* LISTEN 2671/mysqld
machine1:~#
Afterwards, set a MySQL password for the user root@localhost:
machine1/machine2/machine3/machine4:
mysqladmin -u root password myrootsqlpassword
Next we create MySQL passwords for root@machine1.domain.com, root@machine2.domain.com, root@machine3.domain.com, and root@machine4.domain.com:
machineN:
mysqladmin -h machineN.domain.com -u root password myrootsqlpassword

Now we set up a replication user “slaveuser_for_machineN” that can be used by machineN to access the MySQL database on machineN-1:
E.g. On machine1 ,slaveuser_for_machine2
On machine4 , slaveuser_for_machine1
First we will set the configuration on Machine1 , Replace N by 2 ,3,4,1 (maintain order)…..
Machine1/machine2/machine3/machine4:
mysql -u root -p
GRANT REPLICATION SLAVE ON *.* TO ‘slaveuser_for_machineN’@'%’ IDENTIFIED BY ‘slave_user_for_machineN_password’;
FLUSH PRIVILEGES;
quit;

I assume that the database sampledb is already existing on machine1, and that there are tables with records in it.
Before we start setting up the replication, we create an empty database sampledb on machine2, machine3, and machine4:
machine2/machine3/machine4:
mysql -u root -p
CREATE DATABASE sampledb;
quit;

Setting Up Replication
Now we set up master-master replication in /etc/mysql/my.cnf. The crucial configuration options for master-master replication are auto_increment_increment and auto_increment_offset:
• auto_increment_increment controls the increment between successive AUTO_INCREMENT values.
• auto_increment_offset determines the starting point for AUTO_INCREMENT column values.
Let’s assume we have N MySQL nodes (N=4 in this example), then auto_increment_increment has the value N on all nodes, and each node must have a different value for auto_increment_offset (1, 2, …, N).
We also need to configure log-slave-updates because otherwise replication will work only, for example, from machine1 to machine2, but not to machine3 and machine4.
Now let’s configure our four MySQL nodes , :
#vim /etc/my.cnf

machine1: auto-increment-offset=1
master-host = 172.16.1.103
master-user = slaveuser_for_machine1
master-password= slave_user_for_machine1_password
machine2: auto-increment-offset=2
master-host = 172.16.1.100
master-user = slaveuser_for_machine2
master-password= slave_user_for_machine2_password
machine3: auto-increment-offset=3
master-host = 172.16.1.101
master-user = slaveuser_for_machine3
master-password= slave_user_for_machine3_password
machine4: auto-increment-offset=4
master-host = 172.16.1.102
master-user = slaveuser_for_machine4
master-password= slave_user_for_machine4_password

vi /etc/mysql/my.cnf
Search for the section that starts with [mysqld], and put the following options into it (commenting out all existing conflicting options):

Sample configuration file for machine 1-4
[...]
[mysqld]
server-id = 1
replicate-same-server-id = 0
auto-increment-increment = 4
auto-increment-offset = 1 or 2 or 3 or 4

master-host = 172.16.1.100 or 101 or 102 or 103
master-user = slaveuser_for_machine1 / 2/ 3/ 4
master-password = slave_user_for_machine1/2/3/4_password
master-connect-retry = 60
replicate-do-db = sampledb

log-bin = /var/log/mysql/mysql-bin.log
binlog-do-db = sampledb
log-slave-updates

relay-log = /var/lib/mysql/slave-relay.log
relay-log-index = /var/lib/mysql/slave-relay-log.index

expire_logs_days = 10
max_binlog_size = 500M
[...]
Then restart MySQL:
/etc/init.d/mysql restart
Before we continue, we must make sure that no slave processes are running on machine1 to machine4:
machine1/machine2/machine3/machine4:
/usr/bin/mysqladmin –user=root –password=myrootsqlpassword stop-slave
Next we lock the sampledb database on machine1, find out about the master status of machine1, create an SQL dump of sampledb (that we will import into sampledb on machine2, machine3, and machine4 so that all four databases contain the same data), and unlock the database so that it can be used again:
machine1:
mysql -u root -p
On the MySQL shell, run the following commands:
machine1:
USE sampledb;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
The last command should show something like this (please write it down, we’ll need it later on):
mysql> SHOW MASTER STATUS;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000010 | 98 | sampledb | |
+——————+———-+————–+——————+
1 row in set (0.00 sec)

mysql>
Now don’t leave the MySQL shell, because if you leave it, the database lock will be removed, and this is not what we want right now because we must create a database dump now. While the MySQL shell is still open, we open a second command line window where we create the SQL dump snapshot.sql and transfer it to machine2, machine3, and machine4 (using scp):
machine1:
cd /tmp
mysqldump -u root -pmyrootsqlpassword –opt sampledb > snapshot.sql
scp snapshot.sql root@172.16.1.101:/tmp
scp snapshot.sql root@172.16.1.102:/tmp
scp snapshot.sql root@172.16.1.103:/tmp
Afterwards, you can close the second command line window. On the first command line window, we can now unlock the database and leave the MySQL shell:
machine1:
UNLOCK TABLES;
quit;

4.1 Setting Up Replication On machine2
On machine2, we can now import the SQL dump snapshot.sql like this:
machine2:
/usr/bin/mysqladmin –user=root –password=myrootsqlpassword stop-slave
cd /tmp
mysql -u root -pmyrootsqlpassword sampledb SHOW MASTER STATUS;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000010 | 1067 | sampledb | |
+——————+———-+————–+——————+
1 row in set (0.00 sec)

mysql>
Then unlock the tables:
UNLOCK TABLES;
and run the following command to make machine2 a slave of machine1 (it is important that you replace the values in the following command with the values you got from the SHOW MASTER STATUS; command that we ran on machine1!):
CHANGE MASTER TO MASTER_HOST=’172.16.1.100′, MASTER_USER=’slaveuser_for_machine2’, MASTER_PASSWORD=’slave_user_for_machine2_password’, MASTER_LOG_FILE=’mysql-bin.000010′, MASTER_LOG_POS=98;
Finally start the slave:
START SLAVE;
Then check the slave status:
SHOW SLAVE STATUS \G
It is important that both Slave_IO_Running and Slave_SQL_Running have the value Yes in the output (otherwise something went wrong, and you should check your setup again and take a look at /var/log/syslog to find out about any errors):
mysql> SHOW SLAVE STATUS \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.1.100
Master_User: slaveuser_for_s2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 98
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: sampledb
Exec_Master_Log_Pos: 98
Relay_Log_Space: 235

we will configure replication from machine2 to machine3.

4.2 Setting Up Replication On machine3
On machine3, we can now import the SQL dump snapshot.sql like this:
/usr/bin/mysqladmin –user=root –password=myrootsqlpassword stop-slave
cd /tmp
mysql -u root -pmyrootsqlpassword sampledb SHOW MASTER STATUS;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000010 | 1067 | sampledb | |
+——————+———-+————–+——————+
1 row in set (0.00 sec)

mysql>
Then unlock the tables:
UNLOCK TABLES;
and run the following command to make machine3 a slave of machine2 (it is important that you replace the values in the following command with the values you got from the SHOW MASTER STATUS; command that we ran on machine2!):
CHANGE MASTER TO MASTER_HOST=’172.16.1.101′, MASTER_USER=’slaveuser_for_machine3′, MASTER_PASSWORD=’slave_user_for_machine3_password’, MASTER_LOG_FILE=’mysql-bin.000010′, MASTER_LOG_POS=1067;
Finally start the slave:
START SLAVE;
Then check the slave status:
SHOW SLAVE STATUS \G
It is important that both Slave_IO_Running and Slave_SQL_Running have the value Yes in the output (otherwise something went wrong, and you should check your setup again and take a look at /var/log/syslog to find out about any errors):
mysql> SHOW SLAVE STATUS \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.1.101
Master_User: slaveuser_for_s3
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 1067
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: sampledb
Exec_Master_Log_Pos: 1067
Relay_Log_Space: 235

Next we must configure replication from machine3 to machine4.

4.3 Setting Up Replication On machine4
On machine4, we can now import the SQL dump snapshot.sql like this:
/usr/bin/mysqladmin –user=root –password=myrootsqlpassword stop-slave
cd /tmp
mysql -u root -pmyrootsqlpassword sampledb SHOW MASTER STATUS;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000010 | 1067 | sampledb | |
+——————+———-+————–+——————+
1 row in set (0.00 sec)

mysql>
Then unlock the tables:
UNLOCK TABLES;
and run the following command to make machine4 a slave of machine3 (it is important that you replace the values in the following command with the values you got from the SHOW MASTER STATUS; command that we ran on machine3!):
CHANGE MASTER TO MASTER_HOST=’172.16.1.102′, MASTER_USER=’slaveuser_for machen4′, MASTER_PASSWORD=’slave_user_for_machine4_password’, MASTER_LOG_FILE=’mysql-bin.000010′, MASTER_LOG_POS=1067;
Finally start the slave:
START SLAVE;
Then check the slave status:
SHOW SLAVE STATUS \G
It is important that both Slave_IO_Running and Slave_SQL_Running have the value Yes in the output (otherwise something went wrong, and you should check your setup again and take a look at /var/log/syslog to find out about any errors):
mysql> SHOW SLAVE STATUS \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.1.102
Master_User: slaveuser_for_s4
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 1067
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: sampled
Exec_Master_Log_Pos: 1067
Relay_Log_Space: 235

1 row in set (0.00 sec)

Finally we must configure replication from machine4 to machine1 to close the replication circle.

4.4 Setting Up Replication On machine1
To do this, we stop the slave on machine1 and make it a slave of machine4:
mysql -u root -p
STOP SLAVE;
Make sure that you use the values of the SHOW MASTER STATUS; command that you ran on machine4 in the following command:
CHANGE MASTER TO MASTER_HOST=’172.16.1.103′, MASTER_USER=’slave_user_for_machine1′, MASTER_PASSWORD=’slave_user_for_machine1_password’, MASTER_LOG_FILE=’mysql-bin.000010′, MASTER_LOG_POS=1067;
Then start the slave on machine1:
START SLAVE;
Then check the slave status:
SHOW SLAVE STATUS \G
It is important that both Slave_IO_Running and Slave_SQL_Running have the value Yes in the output (otherwise something went wrong, and you should check your setup again and take a look at /var/log/syslog to find out about any errors):
mysql> SHOW SLAVE STATUS \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.1.103
Master_User: slaveuser_for_s1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 1067
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: sampledb:
Exec_Master_Log_Pos: 1067
Relay_Log_Space: 235
1 row in set (0.00 sec)

If nothing went wrong, MySQL master-master replication should now be working. If it isn’t, please check /var/log/syslog for MySQL errors.