数据库主从互备

实验环境:

centos 6.3 64bit

mysql版本:mysql-5.5.28

server1=192.168.1.38

server2=192.168.1.39

1.38

1、初始化数据库

#service mysqld stop

#/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var

#service mysqld start         \\重新启动mysld

2、修改root密码

#mysql

mysql>use mysql;

mysql>update user set password=password('root') where user='root';                  //把root密码修改为root

mysql>flush privileges;

mysql>exit

3、创建数据同步用户,并授权只允许master-slave IP登陆

mysql>use mysql;

mysql>insert into mysql.user (Host,User,Password) values ('localhost','repluser',password('repluser')); 

mysql>grant all privileges on *.* to 'repluser'@'192.168.1.39' identified by 'repluser';

mysql>flush privileges;

4、创建普通测试用户soul,允许在任何IP登陆访问

mysql>use mysql;

mysql>insert into mysql.user (Host,User,Password) values ('%',‘soul',password('soul')); 

mysql>grant all privileges on *.* to 'soul'@'%' identified by 'soul';

mysql>flush privileges;

5、导入数据库mobiledb

#mysql -uroot -p

mysql>use mysql;

mysql>create database mobiledb;            //先创建数据库

mysql>quit

#mysql -uroot -p mobiledb < /home/databackup/mobiledb.sql

或者在控制界面source导入

#mysql -uroot -p

mysql>source /home/databackup/mobiledb.sql;

1.39

6、数据库初始化

#service mysqld stop

#/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var

#service mysqld start         \\重新启动mysld

7、把1.38数据库var目录拷贝到1.39代替掉原来的var

1.38#server mysqld stop

1.39#server mysqld stop

1.38#scp -P43998 -r /usr/local/mysql/var [email protected]:/usr/local/mysql/

8、重启两套服务器的mysqld

1.38#service mysqld start

1.39#chown mysql.mysql /usr/local/mysql/var -R        \\务必要对数据存放目录的属主改为mysql放有权限

1.39#service mysqld start

9、在1.39上创建同步用户

mysql>use mysql;

mysql>insert into mysql.user (Host,User,Password) values ('localhost','repluser',password('repluser')); 

mysql>grant all privileges on *.* to 'repluser'@'192.168.1.38' identified by 'repluser';

mysql>flush privileges;

10、先在1.39做单向同步1.38的数据

a:查看主1.38mysql的master

1.38#mysql -uroot -p

mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000011
        Position: 676
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.01 sec)

ERROR:
No query specified

1.39#mysql -uroot -p

mysql> change master to MASTER_HOST='192.168.1.38',
    -> MASTER_USER='repluser',
    -> MASTER_PASSWORD='repluser',
    -> Master_Port=3307,
    -> Master_Log_File='mysql-bin.000011',
    -> Master_Log_Pos=676;
#start slave;                                                                              \\启动同步

查看结果

mysq>show slave status\G;

wKioL1SyV4vQtctlAANS4xDbLtk493.jpg

目前结果:1.39能单步同步1.38的数据

11、在1.38上做同步1.39上的数据

b:查看1.39mysql master 的日志

1.39#mysql -uroot -p

mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000014
        Position: 338
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.01 sec)

ERROR:
No query specified

1.38#mysql -uroot -p

mysql> change master to MASTER_HOST='192.168.1.39',
    -> MASTER_USER='repluser',
    -> MASTER_PASSWORD='repluser',
    -> Master_Port=3307,
    -> Master_Log_File='mysql-bin.000014',
    -> Master_Log_Pos=338;
#start slave;                                                                              \\启动同步

查看结果:

wKiom1SyVxPBptaNAANlZ44nBr0499.jpg

测试结果:1.38  1.39能实时两边同步。

注意:检查1.38  1.39 的所有my.cnf配置文件 server-id 不能相同 server-id必须唯一值