mysql的双机热备

mysql的双机热备
mysql

       双机热备,就是要保持两个数据库的状态自动同步。对任何一个数据库的操做都自动应用到另一个数据库,始终保持两个数据库数据一致。nginx

       这样的作法好处在于:laravel

一、能够作灾备,其中一个坏了能够切换到另外一个;sql

二、能够作负载均衡,能够请求分摊到其中任何一台上,提升网站吞吐量。数据库


这里我使用的是mariadbvim

[root@nginx2 ~]# mysql -V服务器

mysql  Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1负载均衡


一、在数据库A上建立用户并受权登陆ide

grant replication slave on *.* to 'rep'@'172.25.254.131' identified by '123456';网站


二、开启A服务器的binarylog

vim /etc/my.cnf

log-bin = mysql-bin         #开启mysql的binlog日志功能

binlog_format = mixed    #binlog日志格式

server-id = 1                    #服务器id号

read-only=0                    #关闭只读,能够读写

binlog-do-db=laravel       #指定对db_nameA记录二进制日志

binlog-ignore-db=mysql  #指定不对db_nameB记录二进制日志

#relay_log=mysql-relay-bin#开启relay-log日志,relay-log日志记录的是从服务器I/O线程将主服务器的二进制日志读取过来记录到从服务器本地文件,而后SQL线程会读取relay-log日志的内容并应用到从服务器


三、登陆到B服务器上,开启中继

vim /etc/my.cnf

log-bin = mysql-bin         #开启mysql的binlog日志功能

binlog_format = mixed    #binlog日志格式

server-id = 1                    #服务器id号

read-only=0                     #关闭只读,能够读写

binlog-do-db=laravel       #指定对db_nameA记录二进制日志

binlog-ignore-db=mysql  #指定不对db_nameB记录二进制日志

relay_log=mysql-relay-bin#开启relay-log日志,relay-log日志记录的是从服务器I/O线程将主服务器的二进制日志读取过来记录到从服务器本地文件,而后SQL线程会读取relay-log日志的内容并应用到从服务器


四、在B服务器上开启同步

CHANGE MASTER TO MASTER_HOST='172.25.254.134', MASTER_USER='rep', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=610;


五、重启B服务器数据库

systemctl restart mariadb


六、检查同步的状态

MariaDB [(none)]> show slave status\G

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 172.25.254.134

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000006

          Read_Master_Log_Pos: 610

               Relay_Log_File: mariadb-relay-bin.000003

                Relay_Log_Pos: 529

        Relay_Master_Log_File: mysql-bin.000006

             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: 610

              Relay_Log_Space: 825

              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: 

  Replicate_Ignore_Server_Ids: 

             Master_Server_Id: 2


七、上述,这样的只能实现服务器A改变同步到服务器B,是单向的。

要将服务器B的改变反回来也能够同步到服务器A,只需将以上操做再来一次便可!

相关文章
相关标签/搜索