如何理解Mysql Replication

37次阅读
没有评论

共计 3796 个字符,预计需要花费 10 分钟才能阅读完成。

如何理解 Mysql Replication,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面丸趣 TV 小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Mysql Replication

类似于从一台服务器拷贝数据库到另一台服务器上,但它是通过定义 Master 和 Slave 的关系去实时地保证两个数据库的完全同步,这个功能在 Mysql 的 3.23 版中开始出现。

Master/Slave 模式备份

TestEnv:

Master:Mysql-4.1.12 on Redhat9.0 IP:192.168. 0.217

Slave:Mysql-4.1.12 on Redhat9.0 IP:192.168.10.244

1、编译,安装

1. #tar –zxvf Mysql-4.1.12.tar.gz

2. #cd Mysql-4.1.12

3. .#/configure –prefix=/var/eyou/mysql

4. #make

5. #make install

6. #chown –R root /var/eyou/mysql

7. # chown –R mysql /var/eyou/mysql/var

8. #chgrp –R mysql /var/eyou/mysql

9. #scripts/mysql_install_db

10. #cp support-files/my-medium.cnf /etc/my.cnf

2、Master 机器设置权限,赋予 Slave 机器 FILE 及 Replication Slave 权利,并打包要同步的数据库结构。

Master# pwd

/var/eyou/mysql/bin

Master#./mysql –u root –p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or g.

Your MySQL connection id is 2 to server version: 4.1.12

Type help; or h for help. Type c to clear the buffer.

mysql GRANT FILE ON *.* TO rep@192.168.0.244 IDENTIFIED BY‘eyou’;

mysql GRANT REPLICATION SLAVE ON *.* TO rep@192.168.0.244 IDENTIFIED BY‘eyou’;

赋予 192.168.10.244 也就是 Slave 机器有 File 权限, 这个 4.1.12 版对 replication 的权限好像做了调整,只赋予 Slave 机器有 File 权限还不行,还要给它 REPLICATION SLAVE 的权限才可以。

然后打包要复制的数据库

Master# cd var

Master# tar czvf reptest.tar.gz reptest

这样,我们得到一个 reptest 数据库的打包文件 reptest.tar.gz

2 设置主服务器 Master 的 my.cnf,启动 Mysql 服务

Master# vi /etc/my.cnf

在 [mysqld] 添加或修改以下的

[mysqld]

log-bin #打开 logbin 选项以能写到 slave 的 I/ O 线程;

server-id=1 #表示是本机的序号为 1, 一般来讲就是 master 的意思.

sql-bin-update-same

binlog-do-db= reptest #表示同步 reptest 数据库;

然后把 Master 主服务器的 Mysql 重启。

Master# /var/eyou/mysql/bin/mysqladmin –u root –p shutdown

Master# /var/eyou/mysql/bin/safe_mysqld –user=mysql

3、建立 Slave 数据库

刚才在 Master 中打包了 reptest.tar.gz,它的作用就是要在 Slave 恢复成一样的数据库。先把 Master 的 reptest.tar.gz 文件传到 Slave 机器中去。然后

Slave# tar zxvf reptest.tar.gz -C /var/eyou/mysql/var/

4、修改 Slave 服务器的 my.cnf

Slave# vi /etc/my.cnf

在 [mysqld] 添加或修改以下的

master-host=192.168.10.217

master-user=rep

master-password=eyou

master-port=3306

server-id=2

master-connect-retry=60

replicate-do-db=reptest [要更新的数据库]

log-slave-updates

5、删除 Slave 端数据库目录中的 master.info

Slave# rm /var/eyou/mysql/var/master.info

6、重启动 Slave 的 slave start。

Slave# /var/eyou/mysql/bin/mysqladmin –u root –p shutdown

Slave# /var/eyou/mysql/bin/safe_mysqld –user=mysql

7、测试

先检测两个 Mysql 数据库中的 reptest 是否正常。

正常情况应该是 Master 和 Slave 中的 Mysql 都有相同的 reptest 数据库,并且里面的数据都一样。

然后测试 replication 功能是否起用。

在 Master 中的 reptest 数据库添加一笔数据:

Master# /var/eyou/mysql/bin/mysql –u root -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or g.

Your MySQL connection id is 12 to server version: 4.1.12

Type help; or h for help. Type c to clear the buffer.

mysql use reptest;

Database changed

mysql INSERT INTO rep_table VALUES (test1 , 4321 , T ,24);

Query OK, 1 row affected (0.00 sec)

mysql

然后查看 Slave 机器的 reptest 数据库:

Slave#/var/eyou/mysql/bin/mysql –u root –p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or g.

Your MySQL connection id is 12 to server version: 4.1.12

Type help; or h for help. Type c to clear the buffer.

mysql use reptest;

Database changed

mysql select * from reptable;;

+——+——+——+——+

| id | name | sex | age |

+——+——+——+——+

| test1| 4321 | T | 24 |

+——+——+——+——+

1 row in set (0.00 sec)

PS :

1,Slave 机器的权限问题,不但要给 slave 机器 File 权限,还要给它 REPLICATION SLAVE 的权限。

2.在修改完 Slave 机器 /etc/my.cnf 之后,slave 机器的 mysql 服务启动之前,记得要删除掉 master.info

3,在 show master status 或着 show slave status 不正常时,看看.err 是怎样说的。

4,Slave 上 Mysql 的 Replication 工作有两个线程, I/O thread 和 SQL thread。I/O 的作用是从 master 3306 端口上把它的 binlog 取过来(master 在被修改了任何内容之后, 就会把修改了什么写到自己的 binlog 等待 slave 更新), 然后写到本地的 relay-log, 而 SQL thread 则是去读本地的 relay-log, 再把它转换成本 Mysql 所能理解的语句,于是同步就这样一步一步的完成. 决定 I /O thread 的是 /var/lib/mysql/master.info, 而决定 SQL thread 的是 /var/lib/mysql/relay-log.info.

双向复制模式

1:

Slave 机器设置权限,赋予 Master 机器 FILE 及 Replication Slave 权利.

Master#./var/eyou/mysql –u root –p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or g.

Your MySQL connection id is 2 to server version: 4.1.12

Type help; or h for help. Type c to clear the buffer.

mysql GRANT FILE ON *.* TO rep@192.168.0.217 IDENTIFIED BY‘eyou’;

mysql GRANT REPLICATION SLAVE ON *.* TO rep@192.168.0.217 IDENTIFIED BY‘eyou’;

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注丸趣 TV 行业资讯频道,感谢您对丸趣 TV 的支持。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计3796字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)