[转]MySQL Replication

[转自:linuxme.blog.51cto.com/1850814/383742]

1   主从 mysql server 的工作原理:(如图及其过程分析)

过程:
   Mysql
的复制(replication )是一个异步的复制,从一个Mysql instace (称之为Master )复制到另一个Mysql instance (称之Slave )。实现整个复制操作主要由三个进程完成的,其中两个进程在SlaveSql 进程和IO 进程),另外一个进程在 MasterIO 进程)上。
  
要实施复制,首先必须打开Master 端的binary logbin-log )功能,否则无法实现。因为整个复制过程实际上就是SlaveMaster 端获取该日志然后再在自己身上完全顺序的执行日志中所记录的各种操作。
复制的基本过程如下:
1Slave 上面的IO 进程连接上Master ,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容;
2Master 接收到来自SlaveIO 进程的请求后,通过负责复制的IO 进程根据请求信息读取制定日志指定位置之后的日志信息,返回给Slave IO 进程。返回信息中除了日志所包含的信息之外,还包括本次返回的信息已经到Master 端的bin-log 文件的名称以及bin-log 的位置;
3SlaveIO 进程接收到信息后,将接收到的日志内容依次添加到Slave 端的relay-log 文件的最末端,并将读取到的Master 端的 bin-log 的文件名和位置记录到master-info 文件中,以便在下一次读取的时候能够清楚的高速Master “我需要从某个bin-log 的哪个位置开始往后的日志内容,请发给我”;
4SlaveSql 进程检测到relay-log 中新增加了内容后,会马上解析relay-log 的内容成为在Master 端真实执行时候的那些可执行的内容,并在自身执行。

好了,了解了其原理后就让我们来安装mysql 及配置主从mysql 服务器吧

为了使用方便,和使mysql 的功能更优一点我们使用二进制包安装,下载地址(需要注册,免费): http://www.mysql.com/downloads/mysql/

 

2 . 二进制安装mysql (过程不做详细解释):

# 解压包及做链接

tar xvf mysql-5.1.50-linux-i686-glibc23.tar.gz /usr/local

cd /usr/local

 

ln -sv mysql-5.1.50-linux-i686-glibc23.tar.gz mysql

cd mysql

 

# 增加用户及该权限(-r : 加一系统用户)

groupadd mysql

useradd -g mysql -s /sbin/nologin -M -r mysql  

 

mkdir /mysql/data

chown -R mysql.mysql /mysql/data

cd /usr/local/mysql

chown mysql:mysql . -R

 

# 初始化mysql 配置

scripts/mysql_install_db --user=mysql --datadir=/mysql/data

 

chown root . -R

chown mysql data/ -R

 

cp support-files/my-large.cnf /etc/my.cnf

vim /etc/my.cnf

datadir = /mysql/data    # 加入这一行

 

# 启动mysql

bin/mysqld_safe --user=mysql &

netstat -nutlp | grep 3306

 

# 使其可以使用mysql 命令

vim /etc/profile

#add

PATH=$PATH:/usr/local/mysql/bin

 

. /etc/profile   # 重读配置文件

 

 

# 加载库函数

vim /etc/ld.so.conf.d/mysql.conf

#add

/usr/local/mysql/lib

ldconfig -v

ln -sv /usr/local/mysql/include /usr/include/mysql

ls /usr/include/mysql/

 

# mysql 加入开机启动

cp support-files/mysql.server /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld on

service mysqld restart

 

3       Mysql 装好了我们就来实现master slave mysql server 架构

 

主:192.168.0.192  station192.example.com

从:192.168.0.193  station193.example.com

 

  Master 端的配置:

vim /etc/my.cnf

# 确保有一下两行 并开启

log_bin = mysql-bin

server_id = 24

 

授权可以来读取日志文件的用户:

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENTON *.*

-> TO tom@'192.168.0.%' IDENTIFIED BY 'password';

 

查看一下主mysql 的状态(结果能不一样,已使用情况而定)

mysql> SHOW MASTER STATUS;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000001 | 108      |              |                   |

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

1 row in set (0.00 sec)

 

从( slave )端的设置:

vim /etc/my.cnf

# 确保有一下几行

log_bin = mysql-bin

server_id = 2

relay_log = mysql-relay-bin

log_slave_updates = 1

read_only = 1

 

定义怎样连接 master mysql

mysql> CHANGE MASTER TO MASTER_HOST='station192.example.com',

-> MASTER_USER='tom',

-> MASTER_PASSWORD='password',

-> MASTER_LOG_FILE='mysql-bin.000001',

-> MASTER_LOG_POS=0;

  4. 查看状态

# 查看从服务器的状态:

mysql> SHOW SLAVE STATUS\G

*************************** 1. row ***************************

Slave_IO_State:

Master_Host: station192.example.com

Master_User: tom

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 5

Relay_Log_File: mysql-relay-bin.000001

Relay_Log_Pos: 5

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: No

Slave_SQL_Running: No

……………… .

mysql> START SLAVE;

注意:这个命令的不能有错误产生

 

# 再次查看状态

mysql> SHOW SLAVE STATUS\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: station192.example.com

Master_User: tom

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 175

Relay_Log_File: mysql-relay-bin.000001

Relay_Log_Pos: 175

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

………………………

 5. 查看进程

查看 Master IO 进程):

mysql> SHOW PROCESSLIST\G

*************************** 1. row ***************************

Id: 24

User: tom

Host: station193.example.com:54831

db: NULL

Command: Binlog Dump

Time: 610237

State: Has sent all binlog to slave; waiting for binlog to be updated

Info: NULL

 

查看 Slave Sql 进程和 IO 进程):

mysql> SHOW PROCESSLIST\G

*************************** 1. row ***************************

Id: 12

User: system user

Host:

db: NULL

Command: Connect

Time: 611116

State: Waiting for master to send event

Info: NULL

*************************** 2. row ***************************

Id: 13

User: system user

Host:

db: NULL

Command: Connect

Time: 33

State: Has read all relay log; waiting for the slave I/O thread to update it

Info: NULL

注意 1.row I/O 进程 2.row sql 进程,已经空闲 33

好了到此简单主从 MySQL Replication 就已经配置完成了,你学会了吗???

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值