MySql数据库的主从及实现双机热备

MySql数据库的主从及实现双机热备

安装参考

https://blog.csdn.net/lswzw/article/details/89151740

实现主从复制(最好两个数据的数据一致,不然会出问题,先实现主从,后实现双机热备)

时间也一定要同步

1.主从都操作

vi /etc/my.cnf
添加:
主:
server-id=1  #[必须]服务器唯一ID,默认是1
log-bin=mysql-bin   #[必须]启用二进制日志 

从:
server-id=2
log-bin=mysql-bin

server-id 一定要不一样.

2.主服务器操作

进入MySQL

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

File列和Position列的值一会儿我们要用到。

创建备份专用帐户

mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'backup'@'192.168.1.%' IDENTIFIED BY 'Backup@123';
#添加用户:backup  密码: Backup@123 只允许:192.168.1.0段ip登录
mysql> flush privileges;
#刷新权限

3.从服务器操作

注意:由于此时的mysql已经启动过,会自动保留一个uuid的服务编号,这个编号文件(auto.cnf)如果不删除,无法实现主从。

[root@mysql02 ~]# find / -name 'auto.cnf'
/var/lib/mysql/auto.cnf
[root@mysql02 ~]# cat /var/lib/mysql/auto.cnf 
[auto]
server-uuid=10271616-5a93-11e9-ac44-00505698d778
[root@mysql02 ~]# rm -f /var/lib/mysql/auto.cnf

开启主从

进入MySQL

CHANGE MASTER to MASTER_HOST="192.168.1.44",
MASTER_PORT=3306,
MASTER_user="backup",
MASTER_PASSWORD="Backup@123",
MASTER_LOG_FILE="mysql-bin.000002",
MASTER_LOG_POS=154;

start slave;

参数描述:
master_host=主服务器IP
master_user=在主服务器上创建的备份用户名
master_password=备份用户密码
master_log_file=查询master(主服务器)的状态得到的File列的值
master_log_pos=Position列的值
start slave:启动从服务器复制功能

4.查看状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.44
                  Master_User: backup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1053
               Relay_Log_File: mysql02-relay-bin.000002
                Relay_Log_Pos: 1219
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 

  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
 两都是Yes,表示配置主从成功

新建一个数据库,建几张表,看看有没有同步到从库,同步就OK了(不要在从库添加)

主操作:

mysql> create database db_test_slave; 
mysql> use db_test_slave; 
mysql> create table tb_test(id int(3), name varchar(50)); 
mysql> insert into tb_test values(1,'hello slave'); 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_test_slave      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

从操作:

mysql> show databases; use db_test_slave; select * from tb_test;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_test_slave      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
+------+-------------+
| id   | name        |
+------+-------------+
|    1 | hello slave |
+------+-------------+
1 row in set (0.00 sec)

5.实现双机热备

把搭建主从的流程在反过来执行一遍就可以了

1.从服务器操作

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
创建备份专用帐户
mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'backup'@'192.168.1.%' IDENTIFIED BY 'Backup@123';
#添加用户:backup  密码: Backup@123 只允许:192.168.1.0段ip登录
mysql> flush privileges;
#刷新权限

2.主服务器操作

CHANGE MASTER to MASTER_HOST="192.168.1.45",
MASTER_PORT=3306,
MASTER_user="backup",
MASTER_PASSWORD="Backup@123",
MASTER_LOG_FILE="mysql-bin.000002",
MASTER_LOG_POS=154;
start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.45
                  Master_User: backup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1053
               Relay_Log_File: mysql02-relay-bin.000002
                Relay_Log_Pos: 1219
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 

  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
 两都是Yes,表示配置主从成功

双机热备搭建完成。

两个mysql数据库互为主从,都可进行写入,读取,实现了数据库的高可用。

你可能感兴趣的:(数据库)