参考资料:winyee先生,谢谢
Redhat5,mysql 6.0
实验环境:mysql主服务器A 192.168.0.2 从服务器:192.168.0.3 搭建双机热备的mysql集群
① 分别在主机A,B上面建立用于Replication的用户.给予REPLICATION SLAVE权限即可
shell>mysqld_safe -user=mysql&
shell>mysql -uroot -ppassword
mysql>GRANT REPLICATION SLAVE ON *.*
-> TO 'repl'@'192.168.0.%' IDENTIFIED BY 'repl';
mysql>quit;
##停止mysql Server.
shell>mysqladmin shutdown -uroot -ppassword
②如若两台机器的数据库不一致,则将某一台的数据转移到另外一台,例如将A数据库备份到B.主机A打包要复制的数据库,转移到B机器.否则,直接执行步骤③即可..
shell> cd /usr/local/mysql/
/usr/local/mysql/#shell#> tar -zcvf var.tar.gz var
这样,我们得到一个mysql数据库的打包文件var.tar.gz
在B机器上删除以前的数据库文件,将var.tar.gz复制到此处,修改权限
shell> cd /usr/local/mysql/
/usr/local/mysql/#shell#>rm -fr var
/usr/local/mysql/#shell#>tar -zxvf var.tar.gz
/usr/local/mysql/#shell#>cd var
/usr/local/mysql/var/#shell#>chown -R root .
/usr/local/mysql/var/#shell#>chown -R mysql .
③分别修改A,B主机的mysql 配置文件.均如下操作
shell>vi /etc/my.cnf
##找到[mysqld],添加一行'log-bin',并且设置A,B主机的server-id为两个不同的整数即可.例如##
## A主机修改为
[mysqld]
log-bin
server-id=1
## B主机修改为
[mysqld]
log-bin
server-id=2
##保存退出
④A主机用如下方式启动mysql.
shell>mysqld_safe --skip-slave-start --user=mysql&
##登陆mysql
shell>mydql -uroot -ppassword
mysql>change master to
->MASTER_HOST='192.168.0.3', ##主机B的ip
->MASTER_USER='repl',
->MASTER_PASSWORD='repl';
mysql> start slave;
⑤B主机也用如下方式启动mysql.
shell>mysqld_safe --skip-slave-start --user=mysql&
##登陆mysql
shell>mydql -uroot -ppassword
mysql>change master to
->MASTER_HOST='192.168.0.2', ##主机A的ip
->MASTER_USER='repl',
->MASTER_PASSWORD='repl';
mysql> start slave;
⑥现在已基本完成了操作,在任意一台运行如下命令查看master/slave状态
mysql> show master status; ##如果Server-id没有设置,此处将会有警告信息
+----------------------+--------------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+--------------+--------------+------------------+
| localhost-bin.000003 | 98 | | |
+----------------------+--------------+--------------+------------------+
1 row in set (0.03 sec)
****************************************
mysql> show slave status;
-----+--------------------+-----------------+-------------------+----------------+-----------------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table
-----+--------------------+-----------------+-------------------+----------------+-----------------------+
| Waiting for master to send event | 192.168.0.3 | repl | 3306 | 60 | localhost-bin.000003 | 98 | localhost-relay-bin.000008 | 239 | localhost-bin.000003 | Yes | Yes | | | | | | | 0 | | 0 | 98 | 239 |
......
1 row in set (0.01 sec)
*****************************************
mysql> show processlist;
+---+-------------+-------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+
| Id| User | Host | db | Command | Time | State | Info |
+---+-------------+-------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+
| 1 | system user | | NULL | Connect | 2073 | Waiting for master to send event | NULL |
| 2 | system user | | NULL | Connect | 1426 | Has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 5 | repl | 192.168.0.3:48356 | NULL | Binlog Dump | 832 | Has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 6 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+-------------+-------------------+------+-------------+------+----------------------------------------------------------------------+------------------+
4 rows in set (0.00 sec)
***此处应该有3个以上的id才正确.
****************************************************************************************************************************************
为了方便,AB主机都添加了repl热备用户,此处用户名,密码等你可以任意选择,只要在change master 的时候,添加正确即可.初次做这个的时候,你也可以给予all privileges. mysql并
不赞同这么给予权限,说是因为安全的原因. GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY 'repl';这个语句授权的前提是两台主机位于同一
个网段.其实,随你怎么设置,只要主机A能在主机B上用repl用户访问,主机B能在主机A上用 repl用户访问即可.
配置文件:按照官方手册,添加log-bin即表示当前主机将作为master. log-bin也可以带参数:log-bin=sql-bin (用与记录操作的二进制文件名). 初次做这个的朋友,最好不要带
参数,否则会比较麻烦的.在任何时候,你都可以用
mysql> CHANGE MASTER TO
-> MASTER_HOST='master_host_name',
-> MASTER_USER='replication_user_name',
-> MASTER_PASSWORD='replication_password';
后重启来更改master.