中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 数据库开发 > 数据库综合
Mysql Replication
作者:未知 时间:2005-09-13 23:43 出处:Blog.ChinaUnix.net 责编:chinaitpower
              摘要:Mysql Replication

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

Master/Slave模式备份

TestEnv:

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

Mysql Replication

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

Master/Slave模式备份

TestEnv:

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

2Master 机器设置权限,赋予Slave机器FILEReplication 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设置主服务器Mastermy.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、重启动Slaveslave 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 :

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

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

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

4SlaveMysqlReplication工作有两个线程, I/O threadSQL 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机器FILEReplication 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’;

2

在原有的master/slave设置基础上做下列修改:

Master# vi /etc/my.cnf

添加:

master-host=192.168.0.244     #表示本机做关闭本页

 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有