Mysql master-slave copy notes under Centos6.3

Source: Internet
Author: User
The MySQL master-slave replication structure is based on the mysqlbin-log. The slave database receives the bin-log incremental information of the master database by opening the IO process and saves it to the local relaylog, then start the MYSQL process from relaylog

The MySQL master-slave replication structure is based on the mysqlbin-log. The slave database receives the bin-log incremental information of the master database by opening the IO process and saves it to the local relaylog, then start the MYSQL process from relaylog

The MySQL master-slave replication structure is based on the mysql bin-log. The slave database receives the bin-log incremental information of the master database by opening the IO process and saves it to the local relay log, then, the incremental information obtained from the relay log by opening the MYSQL process and translating it into an SQL statement and writing it to the slave database.

The master-slave replication structure can implement two functions:

1. Back up an instance from a database that acts as the master database

2. read/write splitting. The master database is responsible for reading and writing data normally, and the slave database is only responsible for reading data.

In the actual production environment, virtual hosts, because many applications actually read databases much more frequently than write databases, make a judgment when writing programs at the initial stage of project development, all read operations are pushed to the slave database. If data cannot be obtained from the slave database, data is retrieved from the master database. In this way, read/write splitting is implemented to relieve the I/O pressure on the master database.

This architecture is recommended in the production environment.


System Environment: centos6.3 x64

Database: mysql-5.6.10


Mysql master: 192.168.100.90

Mysql slave: 192.168.100.91



I. Deployment environment:


1. Disable iptables and SELINUX

# Service iptables stop

# Setenforce 0

# Vi/etc/sysconfig/selinux

---------------

SELINUX = disabled

---------------


2. install and configure the mysql Portal:


Ii. master mysql configuration: (mysql master)


Modify the mysql configuration file:

# Vi/etc/my. cnf

Add:

-----------------

# Replication Master Server

# Bin Log Path

Log-bin =/usr/local/mysql/log/bin. log

# Server ID

Server-id = 1

# Ignore mysql System Database Replication

Binlog-ignore-db = mysql

Binlog-ignore-db = information_schema

------------

Restart service

# Service mysqld restart


Log on to the mysql Background:

# Mysql-u root-p123456

View the current Logon account:

> Select user ();


Add a synchronization account for slave on the master:

> Grant replication slave on *. * to 'slave '@ '192. 168.100.91' identified by '123 ';


View the created user:

> Select user. host from mysql. user;


View permissions:

> Show grants for 'slave '@ '192. 168.100.91 ';


Mysql lock table read-only (Other accounts cannot write tables after logging on to mysql. This prevents updates to the primary mysql table after the database is backed up, resulting in inconsistency with the contents of the slave database)

> Flush tables with read lock;


View the lock table Countdown time:

> Show variables like '% timeout % ';

------------------------

....


Wait_timeout | 28800

------------------------


Back up and export all the database tables of the master database and transmit them to the slave server.


#/Usr/local/mysql/bin/mysqldump-u root-p123456 -- opt -- flush-logs -- all-database>/root/allbak. SQL

# Cd ~

# Scp allbak. SQL root@192.168.100.91:/root


View the mysql offset (the offset value increases progressively if the database has a write operation)

# Mysql-u root-p123456-e "show master status"

----------------------

+ ------------ + ---------- + -------------- + ------------------ + ------------------- +

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+ ------------ + ---------- + -------------- + ------------------ + ------------------- +

| Bin.000009 | 120 |

+ ------------ + ---------- + -------------- + ------------------ + ------------------- +

----------------------

Ensure that the FILE column and Position column are consistent with the slave Database Configuration:



3. Configure from mysql (mysql slave)


Modify the mysql configuration file:

# Vi/etc/my. cnf

Add:

--------------------

# Replication Slave Server

# Bin log Path (bin-log is not required)

# Log-bin =/usr/local/mysql/log/bin. log

Server-id = 2

# Read-only:

Read-only

# Ignore mysql System Database Replication

Binlog-ignore-db = mysql

Binlog-ignore-db = information_schema

---------------------


Restart service

# Service mysqld restart


Restore the server database to slave

#/Usr/local/mysql/bin/mysql-u root-p123456 </root/allbak. SQL


Configure the connection to synchronize to the server:

# Mysql-u root-p123456;

> Stop slave;

> Change master to master_host = '192. 168.100.90 ', master_user = 'slave', master_password = '000000', master_log_file = "bin.000009", master_log_pos = 192;

> Start slave;

Note: master_log_file indicates which bin-log file of the master database is synchronized.

Master_log_pos indicates which record point of the bin-log file to start synchronization.

Synchronization with the Offset Value of the master database


Return to the primary mysql database to unlock (mysql master)

# Mysql-u root-p123456

> Unlock tables;


Finally, log on to the mysql background to view the master-slave connection status.

# Mysql-u root-p123456-e "show slave status \ G ;"

Find the five lines, and the Master/Slave configuration is successful as follows:

------------------------

Slave_IO_State: Waiting for master to send event

Slave_IO_Running: Yes

Slave_ SQL _Running: Yes

Read_Master_Log_Pos: 120

Relay_Master_Log_File: bin.000009

-------------------------


4. test whether the master and slave nodes are synchronized:

(Server)

# Mysql-u root-p123456-e "create database test02 ;"

# Mysql-u root-p123456-e "show databases like 'test02 ';"

------------

+ ------------------- +

| Database (test02) |

+ ------------------- +

| Test02 |

+ ------------------- +

------------

(Cilent)

# Mysql-u root-p123456-e "show databases like 'test02 ';"

-------------

+ ------------------- +

| Database (test02) |

+ ------------------- +

| Test02 |

+ ------------------- +

-------------

The test is successful ..


When the database data on the server side changes, the client side will synchronously update the data to implement the backup and read/write splitting of the master database.

Note the following points:

1. the read-only parameter must be added to the my. cnf configuration file in the mysql slave database to ensure that the slave database is read-only.

# Echo "read-only">/etc/my. cnf


2. Ignore synchronization between the slave database mysql and the information_schema system table

# Echo "binlog-ignore-db = mysql">/etc/my. cnf

# Echo "binlog-ignore-db = information_schema">/etc/my. cnf


Authorize the master database user in the production environment to add, delete, modify, and query permissions.

> Grant select, INSERT, UPDATE, delete on *. * to 'user' @ '% 'identified by '123 ';


Authorization of the slave database in the production environment. Only the query permission is authorized.

> Grant select on *. * to 'user' @ '%' identified by '123 ';


3. The bin-log function is disabled by default in the slave database. You must enable the bin-log function in the slave database unless you perform sub-level synchronization from the slave database and the server space.


4. How to upgrade the master database to the master database (one master multiple slaves) due to hardware faults)

(Mysql slave)

(1) ensure that there are no SQL statements to be synchronized from the slave machine, that is, the Has read all relay log occurs and then the slave database IO_Threat process is disabled.

# Mysql-uroot-p123456

> Stop slave IO_THREAD

(2) disable the slave database slave service and upgrade it to the master database.

> Stop slave

> Reset master

(3) change the slave database IP address to the IP address of the faulty master database (configuration method omitted)

(4) Delete the new master database master.info and relay-log.info, to prevent the next restart will follow the slave database to start

# Cd/usr/local/mysql/log

# Rm-rf master.info relay-log.info

(5) Reconfigure the account synchronization information for connecting the slave database to the master database, and reset the offset at the lower-level slave database to be consistent with that of the new master database.

Finally, after the master database hardware is restored, it is set to slave database and replaced with the slave database IP address to complete master-slave switchover.


5. configuration script:

1. If you want to implement unattended backup of the primary database, you can add the following script and execute the scheduled task in the early morning:

# Vi/etc/rc. d/mysql_bak.sh

---------------------

#! /Bin/sh


MYSQL_USER = root

MYSQL_PW = "123456"


LOG_PATH =/usr/local/mysql/log

DATA_PATH =/usr/local/mysql/data

LOG_FILE =$ {LOG_PATH}/mysqllog _ 'date + % F'. log

DATA_FILE =$ {DATA_PATH}/mysql_backup _ 'date when using f'. SQL .gz


BIN_PATH =/usr/local/mysql/bin

MYSQL_CMD = "$ BIN_PATH/mysql-u $ MYSQL_USER-p $ MYSQL_PW"

MYSQL_DUMP = "$ BIN_PATH/mysqldump-u $ MYSQL_USER-p $ MYSQL_PW -- opt -- flush-logs -- all-database"


$ MYSQL_CMD-e "flush tables with read lock ;"

Echo "------- show master status result -------"> $ LOG_FILE

$ MYSQL_CMD-e "show master status"> $ LOG_FILE

$ {MYSQL_DUMP} | gzip> $ DATA_FILE

$ MYSQL_CMD-e "unlock tables"

Mail-s "mysql slave log" 1234567@qq.com <$ LOG_FILE

---------------------

Back up the database at am:

# Crontab-e

---------------------

30 3 ***/bin/sh/etc/rc. d/mysql_bak.sh>/dev/mull 2> & 1

---------------------

# Service crond restart


2. If you want to distribute the data backed up by the master database to the slave database, restore the slave database, and enable the slave database function, and rent a Hong Kong server, you can add the following script

Note: It is recommended that the master and slave databases perform key authentication first:

For details, see Portal:

# Vi/etc/rc. d/mysql_bak1.sh

------------------

#! /Bin/sh


MYSQL_USER = root

MYSQL_PW = "123456"


MYSQL_SLAVE_IP = "192.168.100.91"

SSH_PATH = "/usr/bin/ssh"

SSH_CMD = "$ {SSH_PATH }$ {MYSQL_SLAVE_IP }"

GZIP_CMD = "/bin/gzip"


LOG_PATH =/usr/local/mysql/log

DATA_PATH =/usr/local/mysql/data

LOG_FILE =$ {LOG_PATH}/mysqllog _ 'date + % F'. log

DATA_FILE =$ {DATA_PATH}/mysql_backup _ 'date when using f'. SQL .gz


BIN_PATH =/usr/local/mysql/bin

MYSQL_CMD = "$ BIN_PATH/mysql-u $ MYSQL_USER-p $ MYSQL_PW"

MYSQL_DUMP = "$ BIN_PATH/mysqldump-u $ MYSQL_USER-p $ MYSQL_PW -- opt -- flush-logs -- all-database"


$ MYSQL_CMD-e "flush tables with read lock ;"

Echo "------- show master status result -------"> $ LOG_FILE

$ MYSQL_CMD-e "show master status"> $ LOG_FILE

$ {MYSQL_DUMP} | gzip> $ DATA_FILE


# Config slave

Cd $ {DATA_PATH}

Scp "mysql_backup _ 'date when using f'. SQL .gz" $ MYSQL_SLAVE_IP:/tmp/

$ {SSH_CMD} "$ {GZIP_CMD}-d/tmp/mysql_backup _ 'date when using f'. SQL .gz"

$ {SSH_CMD} "$ {MYSQL_CMD} </tmp/mysql_backup _ 'date + % F'. SQL"


$ {SSH_CMD} "cat | $ MYSQL_CMD" <EOF

Stop slave;

Change master

Master_host = '1970. 168.100.90 ',

Master_user = 'slave ',

Master_password = '000000 ',

Master_log_file = "bin.000009 ",

Master_log_pos = 120;

Start slave;

EOF


$ SSH_CMD $ MYSQL_CMD-e "show slave status \ G;" | egrep "IO_Running | SQL _Running" >>> $ LOG_FILE


$ MYSQL_CMD-e "unlock tables"

Mail-s "mysql slave log" 1234567@qq.com <$ LOG_FILE

$ SSH_CMD mail-s "mysql slave log" 1234567@qq.com <$ LOG_FILE

------------------



------- Success ---------


This article is from the "all the way to the North" blog. Please keep this source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.