今天做mysql主从时出现"ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE firs"
Replication consists of following 3 parts….
The master records changes to its data in its binary log (Binary Log Events).
The slave copies the master’s binary log events to its relay log.
The slave replays the events in the relay log, applying the changes to its own
data using Slave Thread.
The basic steps to set up replication are…
Set up replication accounts on master server.
Configure the master and slave.
Instruct the slave to connect to and replicate from the master.
Here are few assumptions….
Master IP Address: 192.168.1.215
Slave IP Address: Â 192.168.1.203
Replication User: replication
User Password: slave
The I/O Slave Thread (it runs on Slave Server) makes a TCP/IP connection to the master. Thats why we need to create an user account on Master Server with proper privileges.
Now run the following query on Master to create a Slave account….
mysql>Â GRANT REPLICATION SLAVE ON *.* TO ‘replication‘@’192.168.1.203′ IDENTIFIED BY ’slave’;
Now edit my.cnf file on Master accordingly.
[mysqld]
server-id                      = 1
log-bin                         = mysql-bin
log-bin-index               = mysql-log-bin.index
relay-log                      = mysql-relay-bin
relay-log-index            = mysql-relay-bin.index
expire_logs_days        = 10
max_binlog_size         = 100M
binlog_do_db              = [Database Name to be Replicated]
binlog_ignore_db        = [Database Name not to be Replicated]
#binlog_ignore_db      = [Database Name not to be Replicated] (for more than one data base which is not supposed to be replicated)
datadir                         = /var/lib/mysql
tmpdir                          = /tmp
Now restart Master mysql server and run the following query…
mysql>Â SHOW MASTER STATUS;
Now edit my.cnf file on Slave accordingly…..
[mysqld]
server_id               = 2
master_host           = 192.168.1.215
master_user           = replication
master_password  = slave
master_port           = 3306
relay_log               = mysql-relay-bin
relay-log-index      = mysql-relay-bin.index
We can set the value for master_host, master_user and master_password using mysql query. So it is better to avoid setting those values directly from configuration file.
If master is having data already, insert the data from master server to slave server database or each of the databases.
Copy master database back up inside slave server and run the following command on slave….
mysql -u[username] -p[password] [database name] < directory_path/[database.sql]
Now Restart Mysql Slave server and run following query to synchronize master and slave….
mysql> CHANGE MASTER TO MASTER_HOST=’192.168.1.215′, MASTER_USER=’replication’, MASTER_PASSWORD=’slave’, MASTER_LOG_FILE=’mysql-bin.000004′, MASTER_LOG_POS=570;
Note: MASTER_LOG_FILE and MASTER_LOG_POS values have been taken from the result of SHOW MASTER STATUS
If a Slave is already running on your Slave Server, Mysql Server will throw a error like…
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
So run..
mysql> STOP SLAVE;
And run the query for CHANGING MASTER again.
If it shows an error like..
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log
Run…
mysql> RESET SLAVE;
And run the query for CHANGING MASTER again.
Finally start slave on slave server using..
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS\G
Congrats! Mysql Master-Slave Replication has been set up
Also we will be able to see the Replication Threads in the process list on both the Master and the Slave.
So simply run…
mysql> SHOW PROCESSLIST.