mysql主从同步,数据库双机热备份

mysql主从同步:

主服务器:

master:192.168.1.200

从服务器:

slave:192.168.1.225



一、master端配置:

vi /etc/my.cnf

server-id       = 1    master端ID号

log-bin=/data/logbin/mysql-bin    日志路径及文件名

#binlog-do-db = cacti            同步cacti,此处关闭的话,就是除不允许的,其它的库均同步。

binlog-ignore-db = mysql        不同步mysql库,以下同上

binlog-ignore-db = test

binlog-ignore-db = information_schema



[root@localhost ~]# mysql -u root -p

mysql> grant replication slave on *.* to backdata@'192.168.1.225' identified by "backpass";

Query OK, 0 rows affected (0.03 sec)

(添加同步用的账号backdata 密码backpass,从服务器要用到)

mysql> flush privileges;

Query OK, 0 rows affected (0.02 sec)


mysql> flush tables with read lock;

Query OK, 0 rows affected (0.01 sec)


mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000009 |   120366 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

(这里要记录下File和Position的值,在从服务器中要用到)

mysql> unlock tables;


(下面这一步是要在备份服务器上建立这个数据库,如果备份服务器上没有会出错,至于错误下面会有介绍)

#cd /var/lib/mysql

#tar -zcvf cacti.tgz

[root@localhost mysql]# scp cacti.tgz  [email protected]:/var/lib/mysql/cacti.tgz



二、slave端配置:

[root@localhost ~]#vi /etc/my.cnf

server-id=2  #添加这一句


[root@localhost ~]# mysql -u root -p



mysql> slave stop;

Query OK, 0 rows affected, 1 warning (0.00 sec)


mysql> change master to

   -> master_host='192.168.1.200',

   -> master_user='backdata',

   -> master_password='backpass',

   -> master_log_file='mysql-bin.000009',

   -> master_log_pos=120366;

Query OK, 0 rows affected (0.25 sec)

(这里面填的值就是在主服务器上刚才设置 记录的值)

mysql> start slave;

ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO

[root@localhost ~]#service mysqld restart

mysql> slave start;

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id:    4

Current database: *** NONE ***


Query OK, 0 rows affected (0.00 sec)

重启数据库后没有了上面的错误提示


mysql> show slave status \G

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

              Slave_IO_State: Waiting for master to send event

                 Master_Host: 192.168.1.200

                 Master_User: backdata

                 Master_Port: 3306

               Connect_Retry: 60

             Master_Log_File: mysql-bin.000009

         Read_Master_Log_Pos: 155726

              Relay_Log_File: mysqld-relay-bin.000003

               Relay_Log_Pos: 251

       Relay_Master_Log_File: mysql-bin.000009

            Slave_IO_Running: Yes

           Slave_SQL_Running: No

             Replicate_Do_DB:

         Replicate_Ignore_DB:

          Replicate_Do_Table:

      Replicate_Ignore_Table:

     Replicate_Wild_Do_Table:

 Replicate_Wild_Ignore_Table:

                  Last_Errno: 1146

                  Last_Error: Error 'Table 'cacti.settings' doesn't exist' on query. Default database: 'cacti'. Query: 'REPLACE


INTO settings (name,value) VALUES ('poller_lastrun',1375515901)'

                Skip_Counter: 0

         Exec_Master_Log_Pos: 120366

             Relay_Log_Space: 35767

             Until_Condition: None

              Until_Log_File:

               Until_Log_Pos: 0

          Master_SSL_Allowed: No

          Master_SSL_CA_File:

          Master_SSL_CA_Path:

             Master_SSL_Cert:

           Master_SSL_Cipher:

              Master_SSL_Key:

       Seconds_Behind_Master: NULL

Master_SSL_Verify_Server_Cert: No

               Last_IO_Errno: 0

               Last_IO_Error:

              Last_SQL_Errno: 1146

              Last_SQL_Error: Error 'Table 'cacti.settings' doesn't exist' on query. Default database: 'cacti'. Query: 'REPLACE


INTO settings (name,value) VALUES ('poller_lastrun',1375515901)'

1 row in set (0.00 sec)


查看上面的Slave_IO_Running: Yes 和Slave_SQL_Running: Yes  2个都为yes则证明主从同步正常,如果有任一个显示NO,则证明同步有问题。

看错误提示是没有cacti这个数据库,在master复制过来数据库,上面已有介绍

[root@localhost mysql]# scp cacti.tgz  [email protected]:/var/lib/mysql/cacti.tgz

[root@localhost mysql]#tar -zxvf cacti.tgz

[root@localhost mysql]#service mysqld restart


mysql> show slave status \G

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id:    4

Current database: *** NONE ***


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

              Slave_IO_State: Waiting for master to send event

                 Master_Host: 192.168.1.200

                 Master_User: backdata

                 Master_Port: 3306

               Connect_Retry: 60

             Master_Log_File: mysql-bin.000009

         Read_Master_Log_Pos: 169963

              Relay_Log_File: mysqld-relay-bin.000005

               Relay_Log_Pos: 251

       Relay_Master_Log_File: mysql-bin.000009

            Slave_IO_Running: Yes

           Slave_SQL_Running: Yes

             Replicate_Do_DB:

         Replicate_Ignore_DB:

          Replicate_Do_Table:

      Replicate_Ignore_Table:

     Replicate_Wild_Do_Table:

 Replicate_Wild_Ignore_Table:

                  Last_Errno: 0

                  Last_Error:

                Skip_Counter: 0

         Exec_Master_Log_Pos: 169963

             Relay_Log_Space: 407

             Until_Condition: None

              Until_Log_File:

               Until_Log_Pos: 0

          Master_SSL_Allowed: No

          Master_SSL_CA_File:

          Master_SSL_CA_Path:

             Master_SSL_Cert:

           Master_SSL_Cipher:

              Master_SSL_Key:

       Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

               Last_IO_Errno: 0

               Last_IO_Error:

              Last_SQL_Errno: 0

              Last_SQL_Error:

1 row in set (0.00 sec)

正常了


以上是安装过程,同步的情况将继续测试。



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