双机互备和热备的区别:
互备所指的是backup,保证数据的完整性而做的备份。主要目的是防止数据丢失。
热备是为了保证应用的连续性。利用故障转移的方法实现。
Linux下实现的双机互备:
主从备份:
1、设置同步Master
修改 my.cnf 文件,在
# Replication Master Server (default)
# binary logging is required for replication
添加如下内容:
#log-bin=/var/log/mysql/updatelog
server-id = 1
binlog-do-db=discuz
binlog-ignore-db=mysql
重启MySQL,创建一个MySQL帐号为同步专用
# /usr/local/mysql/bin/mysql -u root -p
mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
如果想要在Slave上有权限执行 "LOAD TABLE FROM MASTER" 或 "LOAD DATA FROM MASTER" 语句的话,必须授予全局的 FILE 和 SELECT 权限:
mysql>GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> FLUSH PRIVILEGES ;
2、设置同步Slave
修改my.cnf文件,添加
server-id = 2
master-host = 192.168.1.123
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=discuz
重启MySQL
3、启动同步
在主服务器master MySQL命令符下
view plaincopy to clipboardprint?# /usr/local/mysql/bin/mysql -u root -p
mysql> show master status;
显示(当然这个是我机器的情况,你的不可能跟我一样哈,只是个例子):
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000009 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+
在从服务器master MySQL命令符下
view plaincopy to clipboardprint?# /usr/local/mysql/bin/mysql -u root -p
mysql> slave stop;
mysql> change master to master_host='192.168.1.123', master_user='back', master_password='back', master_log_file='mysql-bin.000009', master_log_pos=98;
mysql> slave start;
用show slave status/G;看一下从服务器的同步情况
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果都是yes,那代表已经在同步
往表里面写点数据测试一下看是否同步成功,如果不成功,绝对不是你的RP问题,再检查一下操作步骤!
4、设置双向同步
修改slave服务器的my.cnf,添加
log-bin=/var/log/mysql/updatelog
binlog-do-db=discuz
binlog-ignore-db=mysql
重启MySQL,创建一个MySQL帐号为同步专用
view plaincopy to clipboardprint?mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> FLUSH PRIVILEGES ;
修改master服务器的my.cnf,添加
master-host = 192.168.1.124
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=discuz
重启MySQL
在主服务器slave MySQL命令符下:
show master status;
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000013 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+
在服务器A MySQL命令符下:
view plaincopy to clipboardprint?mysql> slave stop;
mysql> change master to master_host='192.168.1.124', master_user='back', master_password='back', master_log_file='mysql-bin.000013', master_log_pos=98;
mysql> slave start;
mysql> slave stop;
mysql> change master to master_host='192.168.1.124', master_user='back', master_password='back', master_log_file='mysql-bin.000013', master_log_pos=98;
mysql> slave start;
mysql做互备时要注意mysql 5.5以上版本不能把
master-host = 192.168.1.124
master-user = back
master-password = back
master-port = 3306
写道my.ini文件中。要在执行完 slave stop;后 用 change master to master_host='主的ip',master_user='用户名',mater_password='密码',master_log_file='日志',master_log_pos='同步点';
然后slave status;