Home Database Mysql Tutorial Centos6.3下mysql主从复制笔记

Centos6.3下mysql主从复制笔记

Jun 07, 2016 pm 05:41 PM
mysql master-slave replication

MySQL主从复制结构是基于mysqlbin-log日志基础上,从库通过打开IO进程收到主库的bin-log日志增量信息,并保存到本地relaylog,而后再通过打开MYSQL进程从relaylog

     MySQL主从复制结构是基于mysql bin-log日志基础上,从库通过打开IO进程收到主库的bin-log日志增量信息,并保存到本地relay log,而后再通过打开MYSQL进程从relay log上获取的增量信息并翻译成SQL语句后写到从数据库。

     主从复制结构,实际上可以实现两个功能:

     1.从库充当主库的数据库备份实例

     2.读写分离,主库负责正常读写数据,从库只负责读数据

     实际生产环境,虚拟主机,因为很多应用实际读数据库的次数远大于写数据库的次数,所以在项目开发初期,编写程序时做一个判断,对所有读的操作全部推到从库,若从库无法获取数据,则再向主库获取数据,从而在一定意义上实现读写分离,缓解主库的IO压力。

     所以生产环境下推荐使用这种架构。


系统环境:centos6.3 x64

数据库:  mysql-5.6.10


mysql master:192.168.100.90

mysql slave:192.168.100.91


 Centos6.3下mysql主从复制笔记


一.部署环境:


1.关闭iptables和SELINUX

# service iptables stop

# setenforce 0

# vi /etc/sysconfig/selinux

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

SELINUX=disabled

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


2.安装配置mysql传送门:


二.主mysql配置:(mysql master)


修改mysql配置文件:

# vi /etc/my.cnf

添加:

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

# Replication Master Server

# bin日志路径

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

# 服务器ID号

server-id = 1

# 忽略mysql系统库复制

binlog-ignore-db=mysql

binlog-ignore-db=information_schema

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

重启服务

# service mysqld restart


登录mysql后台:

# mysql -u root -p123456

查看此刻登录账号:

> select user();


在master为slave添加同步帐号:

> grant replication slave on *.* to 'slave'@'192.168.100.91' identified by '123456';


查看创建的用户:

> select user.host from mysql.user;


查看权限:

> show grants for 'slave'@'192.168.100.91';


mysql锁表只读(其他账户登录mysql后无法进行写表操作,防止备份数据库后,主mysql表更新,导致和从数据库内容不一致)

> flush tables with read lock;


查看锁表倒计时时间:

> show variables like '%timeout%';

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

....


wait_timeout                | 28800

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


将master的数据库表全部备份导出,并传送到slave服务器上。


# /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


查看mysql偏移量(数据库如果有写操作,偏移值会递增)

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

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

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

| File       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| bin.000009 |     120 |              |                  |                   |

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

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

保证FILE列和Position列与从库配置一致:



三.从mysql配置(mysql slave)


修改mysql配置文件:

# vi /etc/my.cnf

添加:

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

# Replication Slave Server

# bin日志路径(无需开bin-log日志)

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

server-id=2

# 只读:

read-only

# 忽略mysql系统库复制

binlog-ignore-db=mysql

binlog-ignore-db=information_schema

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


重启服务

# service mysqld restart


恢复server的数据库到slave

#/usr/local/mysql/bin/mysql -u root -p123456


配置连接同步到server端:

# mysql -u root -p123456;

> stop slave;

> change master to master_host='192.168.100.90',master_user='slave',master_password='123456',master_log_file="bin.000009",master_log_pos= 120 ;

> start slave;

注:master_log_file表示从主数据库哪个bin-log文件开始同步                  

   master_log_pos表示从该bin-log文件哪条记录点开始同步

   需与主库偏移值保持同步


回到主mysql数据库解锁(mysql master)

# mysql -u root -p123456

> unlock tables;


最后登陆从mysql后台查看主从连接状态

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

找到这五行,如下则主从配置成功:

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

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

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


四.测试主从是否同步:

(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            |

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

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

测试成功。。


当server端的数据库数据发生变化时,client端会同步更新,从而实现主库备份和读写分离作用。

这里注意几点:

1.mysql从库中需在my.cnf配置文件中加入 read-only参数,保证从库只读。

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


2.忽略从库mysql与information_schema系统表同步

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

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


生产环境主库用户的授权,授权增删改查权限。

> GRANT SELECT,INSERT,UPDATE,DELETE ON *.* to 'user'@'%' identified by '123456';


生产环境从库的授权,仅授权查权限。

> GRANT SELECT ON *.* to 'user'@'%' identified by '123456';


3.从库默认不开启bin-log日志功能,除非做下级从库级联同步,服务器空间,才需开启从库的bin-log日志。


4.主库由于硬件故障,如何将从库提升为主库(一主多从)

(mysql slave)

(1) 确保从机没有再同步的SQL语句,即出现Has read all relay log再关闭从库IO_Threat进程

# mysql -uroot -p123456

> stop slave IO_THREAD

(2) 关闭从库slave服务,然后将其提升为主库

> stop slave

> reset master

(3) 更换从库IP为故障主库IP(配置方法略)

(4) 删除新的主库master.info和relay-log.info,防止下次重启还会按照从库启动

# cd /usr/local/mysql/log

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

(5) 重新配置从库连接主库的账号同步信息,以及在下级从库重新设置偏移量保持与新的主库一致即可。

最后待主库硬件恢复,将其再设置为从库并更换为上述从库IP地址,完成主从切换。


五.配置脚本:

1.如果想实现无人值守备份主数据库,可添加如下脚本,并在凌晨执行定时任务:

# 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 +%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

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

凌晨3:30执行备份数据库操作:

# crontab -e

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

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

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

# service crond restart


2.如果想实现将主库备份的数据分发到从库,恢复从库数据库,以及开启从库功能,香港服务器租用,可添加如下脚本

注:这里建议主从先做好秘钥认证:

详见传送门:

# 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 +%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 +%F`.sql.gz" $MYSQL_SLAVE_IP:/tmp/

${SSH_CMD} "${GZIP_CMD} -d /tmp/mysql_backup_`date +%F`.sql.gz"

${SSH_CMD} "${MYSQL_CMD}


${SSH_CMD} "cat |$MYSQL_CMD"

stop slave;

change master to

master_host='192.168.100.90',

master_user='slave',

master_password='123456',

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

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

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



-------大功告成---------


本文出自 “一路向北” 博客,请务必保留此出处

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Data backup and failure recovery: Discussion on the importance of MySQL master-slave replication in cluster mode Data backup and failure recovery: Discussion on the importance of MySQL master-slave replication in cluster mode Sep 08, 2023 am 09:03 AM

Data backup and failure recovery: Discussion on the importance of MySQL master-slave replication in cluster mode Introduction: In recent years, with the continuous growth of data scale and complexity, database backup and failure recovery have become particularly important. In distributed systems, MySQL master-slave replication is widely used in cluster mode to provide high availability and fault tolerance. This article will explore the importance of MySQL master-slave replication in cluster mode and give some code examples. 1. Basic principles and advantages of MySQL master-slave replication MySQL master-slave replication is a general

Cope with high concurrency with ease: Analysis of the performance advantages of MySQL master-slave replication as a cluster technology Cope with high concurrency with ease: Analysis of the performance advantages of MySQL master-slave replication as a cluster technology Sep 10, 2023 pm 03:48 PM

Cope with high concurrency with ease: Analysis of the performance advantages of MySQL master-slave replication as a cluster technology. With the rapid development of the Internet, user visits to websites and applications have shown an explosive growth trend. In this high-concurrency situation, how to ensure system stability and performance has become an important task for every developer and system administrator. In databases, MySQL master-slave replication technology is widely used and has become one of the effective solutions to deal with high concurrency. This article will explore the performance advantages of MySQL master-slave replication as a cluster technology. first

Decrypting MySQL master-slave replication: revealing its key implementation mechanism in cluster mode Decrypting MySQL master-slave replication: revealing its key implementation mechanism in cluster mode Sep 10, 2023 am 09:28 AM

Decrypting MySQL master-slave replication: Revealing its key implementation mechanism in cluster mode Introduction: In modern database systems, high availability and flexibility of data are very important. As an open source relational database management system, MySQL has a wide range of applications in meeting user needs. MySQL's master-slave replication is a very critical part of the MySQL database architecture and is used to achieve data backup and high availability. This article will focus on revealing the key implementation mechanism of MySQL master-slave replication, especially in cluster mode.

Optimizing database performance: The best way to use MySQL master-slave replication in cluster technology Optimizing database performance: The best way to use MySQL master-slave replication in cluster technology Sep 10, 2023 am 08:24 AM

Optimizing database performance: The best way to use MySQL master-slave replication in cluster technology Abstract: With the rapid development of the Internet, database performance issues have become the focus of various enterprises and organizations. MySQL master-slave replication technology plays an important role in solving database performance bottlenecks. This article will introduce the concepts and principles of MySQL master-slave replication, as well as the best use methods in cluster technology, to help readers optimize database performance. 1. Introduction As the amount of data continues to increase, database performance problems have become increasingly prominent. How to optimize numbers

Is MySQL master-slave replication a cluster technology or a load balancing technology? Analysis and difference Is MySQL master-slave replication a cluster technology or a load balancing technology? Analysis and difference Sep 10, 2023 am 08:40 AM

Is MySQL master-slave replication a cluster technology or a load balancing technology? Summary of analysis and differences: MySQL master-slave replication is a database replication technology used to synchronize database data on multiple servers. This article will analyze and distinguish the differences between MySQL master-slave replication, cluster technology and load balancing technology in terms of technical principles, application scenarios and functional characteristics. Introduction: In modern Internet applications, high availability and scalability of databases are crucial. MySQL master-slave replication is one of the common solutions, however,

Detailed explanation of the functions and advantages of MySQL master-slave replication in cluster technology Detailed explanation of the functions and advantages of MySQL master-slave replication in cluster technology Sep 09, 2023 am 09:03 AM

Detailed explanation of the functions and advantages of MySQL master-slave replication in cluster technology Introduction MySQL is a powerful relational database management system that is widely used in various large-scale websites and applications. As the amount of data increases and access requests increase, the pressure on a single MySQL server gradually increases. In order to improve the performance and reliability of the database, people begin to adopt cluster technology, among which MySQL master-slave replication is one of the commonly used technologies. means. MySQL master-slave replication principle MySQL master-slave replication refers to the

Uncovering the potential of cluster technology in MySQL master-slave replication: Comparative evaluation of open source solutions and commercial solutions Uncovering the potential of cluster technology in MySQL master-slave replication: Comparative evaluation of open source solutions and commercial solutions Sep 08, 2023 pm 07:16 PM

Tapping into the cluster technology potential of MySQL master-slave replication: Comparative evaluation of open source solutions and commercial solutions. With the continuous development of Internet business and the increasing amount of data, the demand for database cluster solutions has become increasingly strong. MySQL master-slave replication technology just meets this demand. It can process the read and write operations of the database separately on multiple nodes, improving the read performance and availability of the database. This article will explore the potential of cluster technology in MySQL master-slave replication, and conduct a comparative evaluation of open source solutions and commercial solutions.

Understand the cluster features and non-load balancing application scenarios of MySQL master-slave replication Understand the cluster features and non-load balancing application scenarios of MySQL master-slave replication Sep 11, 2023 am 11:04 AM

With the rapid development of the Internet, the amount of data in application systems is increasing, and the requirements for database performance and reliability are also getting higher and higher. As one of the most commonly used open source relational databases, MySQL has high performance and stability and is widely used in various enterprise-level applications. As a commonly used data replication solution, MySQL master-slave replication can improve data reliability and read and write performance, and is widely used in large-scale data applications. The cluster feature of MySQL master-slave replication refers to synchronizing the data of the master database through the replication mechanism.

See all articles