Mysql双机热备

       在日常中,一个公司的的大量数据都会存在数据库里。当然,一台数据库没法保证数据的安全性和可靠性,如果哪天这台服务器出现故障,没法立即解决。这就会对公司造成一定的损失,所以今天我们的实验就会做到一个mysql双击热备,不管哪台mysql服务器是写入修改还是添加任何数据,另外一台服务器都会把数据同步过来。

 一、环境要求:

两台CentOS 6.5 系统服务器
服务器A:192.168.1.10
服务器B:192.168.1.20

 二、服务器A配置

[root@loaclhost ~]#yum -y install mysql mysql-server mysql-devel
[root@loaclhost ~]#service mysqld start            
[root@loaclhost ~]#chkconfig mysqld on 
[root@loaclhost ~]#ckconfig --add mysqld 
[root@loaclhost ~]#mysqladmin -u root password 123456
[root@loaclhost ~]#mysql -u root -p123456      // 登陆数据库
mysql>grant replication slave,file on *.* to 'B'@'192.168.1.20' identified by '123456';   //授权B
mysql>flush privileges;     // 刷新权限
mysql>exit
[root@loaclhost ~]#service mysqld stop  //停止数据库
[root@loaclhost ~]#vim /etc/my.cnf
#修改添加My.cnf配置文件如下:
log-bin=mysql-bin
server-id       = 1
binlog-do-db=test      //我这儿是对test数据库进入热备的
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates=true
slave-skip-errors=all 
skip-name-resolve
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
[root@loaclhost ~]#service mysqld start
[root@loaclhost ~]#mysql -u root -p123456
mysql>flush tables with read lock;    //对表进行加锁
mysql>show master status; 
#显示以下内容:A
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      106 | test         | mysql            |
+------------------+----------+--------------+------------------+
mysql>change master to      //以下内容是先show master status;查看到服务器B的
-> master_host='192.168.1.20',    
-> master_user='B',
-> master_password='123456',
->master_log_file='mysql-bin.000001',
->master_log_pos=106;
mysql>start slave;      //启动slave
mysql>show slave status\G;    //查看IO进程和 SQL进程是否都开启
#显示以下内容IO SQL进程都必需是YES则成功
         Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql


 三、服务器B配置

[root@loaclhost ~]#yum -y install mysql mysql-server mysql-devel
[root@loaclhost ~]#service mysqld start            
[root@loaclhost ~]#chkconfig mysqld on 
[root@loaclhost ~]#ckconfig --add mysqld 
[root@loaclhost ~]#mysqladmin -u root password 123456
[root@loaclhost ~]#mysql -u root -p123456      // 登陆数据库
mysql>grant replication slave,file on *.* to 'A'@'192.168.1.10' identified by '123456';   //授权A
mysql>flush privileges;     // 刷新权限
mysql>exit
[root@loaclhost ~]#service mysqld stop  //停止数据库
[root@loaclhost ~]#vim /etc/my.cnf
#修改添加My.cnf配置文件如下:
log-bin=mysql-bin
server-id       = 1
binlog-do-db=test      //我这儿是对test数据库进入热备的
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates=true
slave-skip-errors=all 
skip-name-resolve
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
[root@loaclhost ~]#service mysqld start
[root@loaclhost ~]#mysql -u root -p123456
mysql>flush tables with read lock;    //对表进行加锁
mysql>show master status; 
#显示以下内容:A
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      106 | test         | mysql            |
+------------------+----------+--------------+------------------+
mysql>change master to      //以下内容是先show master status;查看到服务器A的
-> master_host='192.168.1.10',    
-> master_user='B',
-> master_password='123456',
->master_log_file='mysql-bin.000001',
->master_log_pos=106;
mysql>start slave;      //启动slave
mysql>show slave status\G;    //查看IO进程和 SQL进程是否都开启
#显示以下内容IO SQL进程都必需是YES则成功
         Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql

 

四、测试A服务器
释放掉各自的锁,然后进行插数据测试
mysql>unlock tables;
mysql>use test  #(切换到test数据库)
mysql>create table t11_replicas
-> (id int not null auto_increment primary key,
-> str varchar(255) not null) engine myisam;

mysql>insert into t11_replicas(str) values
-> ('This is a master to master test table');

mysql>show tables;
现在我们就可以查看到一个名为t11_replicas 表,我们切换到服务器B也用查看命令一定也可以查看到这个表。
 五、服务器B测试
释放掉各自的锁,然后进行插数据测试
mysql>unlock tables;
mysql>use test  #(切换到test数据库)
mysql>create table t22_replicas
-> (id int not null auto_increment primary key,
-> str varchar(255) not null) engine myisam;

mysql>insert into t11_replicas(str) values
-> ('This is a master to master test table');

mysql>show tables;
现在我们就可以查看到一个名为t22_replicas 表,我们切换到服务器A也用查看命令一定也可以查看到这个表。
 

你可能感兴趣的:(Mysql双机热备)