1要求  192.168.1.233   192.168.1.46两台服务器互相为主进行备份 2 软件安装

两台机器都运行数据库安装 yum –y  install mysql  mysql-server

 

3配置my.cnf文件和数据库内部设置

3.1 /etc/my.cnf文件

[root@233 ~]# cat /etc/my.cnf   添加红色字段

 

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Default to using old password format for compatibility with mysql 3.x

# clients (those using the mysqlclient10 compatibility package).

#######################################

log-bin=mysql-bin                   ##开启日志

server-id=2                        ##ID不能一样 这个为2   192.168.1.461

binlog-do-db=test                   ## 需要同步的数据库

binlog-ignore-db=mysql              ## 不同步的数据库

########################################

old_passwords=1

# Disabling symbolic-links is recommended to prevent assorted security risks;

# to do so, uncomment this line:

# symbolic-links=0

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

3.2进入数据库进行配置

3.2.1 233上建立用户,给予192.168.1.46服务器权限:

grant replication slave,file on *.*to 'copy'@'192.168.1.46' identified by '123456';

 

3.2.146上建立用户,给予192.168.1.233服务器权限:

grant replication slave,file on *.*to 'copy'@'192.168.1.233' identified by '123456';

 

3.2.2 233上查看 mysql运行日志文件

 

mysql> show    master status\G

*************************** 1. row ***************************

            File: mysql-bin.000010             

        Position: 98

    Binlog_Do_DB: test

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

 

3.2.2 46上查看 mysql运行日志文件

 

mysql> show    master status\G

*************************** 1. row ***************************

            File: mysql-bin.000009

        Position: 98

    Binlog_Do_DB: test

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

 

 

 

3.2.3 192.168.1.233 进行同步账号设置

mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

 

mysql> change master to master_host='192.168.1.233', master_user='copy', master_password='123456', master_log_file='mysql-bin.000009',master_log_pos=98;

Query OK, 0 rows affected (0.01 sec)

                                  #####红色为192.168.1.46mysql日志文件

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

 

3.2.3 192.168.1.46 进行同步账号设置

mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

 

mysql> change master to master_host='192.168.1.233', master_user='copy', master_password='123456', master_log_file='mysql-bin.000010',master_log_pos=98;

Query OK, 0 rows affected (0.01 sec)

                                  #####红色为192.168.1.233mysql日志文件

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

检测

当在两个服务器执行一下命令 是两个YES    成功

mysql> show slave status\G

*************************** 1. row ***************************

             Slave_IO_State: Waiting for master to send event

                Master_Host: 192.168.1.46 

                Master_User: copy

                Master_Port: 3306

              Connect_Retry: 60

            Master_Log_File: mysql-bin.000009

        Read_Master_Log_Pos: 281

             Relay_Log_File: mysqld-relay-bin.000002

              Relay_Log_Pos: 418

      Relay_Master_Log_File: mysql-bin.000009

           Slave_IO_Running: Yes

          Slave_SQL_Running: Yes