Mysql双主(主主互备)部署

文章目录

  • 一.版本
    • 1.1 Centos7 *2
    • 1.2 Mysql 5.7
  • 二. 安装
    • 2.1 Mysql5.7安装:(yum安装,两台一样)
    • 2.2 接下来设置mysql账号密码 及 外网访问权限
  • 三.双主配置
    • 3.1 配置文件
      • 3.1.1 在`192.168.0.131`上配置:
      • 3.1.2 在`192.168.0.132`上配置:
    • 3.2 主从设置
      • 3.2.1 在`192.168.0.131`上执行:
      • 3.2.2 在`192.168.0.132`上执行:
  • 四.主主复制测试


一.版本

1.1 Centos7 *2

192.168.0.131 、192.168.0.132

1.2 Mysql 5.7

二. 安装

2.1 Mysql5.7安装:(yum安装,两台一样)

步骤一:检查是否有低版本的mysql,如果有卸载
Mysql双主(主主互备)部署_第1张图片

步骤二:接下来我们在 Centos7 系统下使用 yum 命令安装 MySQL,需要注意的是 CentOS 7 版本中 MySQL数据库已从默认的程序列表中移除,所以在安装前我们需要先去官网下载 Yum 资源包,下载地址为:https://dev.mysql.com/downloads/repo/yum/
Mysql双主(主主互备)部署_第2张图片

[root@9901388b05f6 ~]# wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

步骤三:安装

[root@9901388b05f6 ~]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm

[root@9901388b05f6 ~]# yum install mysql-server

步骤四:启动

[root@9901388b05f6 ~]# systemctl start mysqld

步骤五: 登录
centos7 上安装mysql5.7后登录报错
错误一: 首次登录时出现 ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password:yes)

于是解决方法:

1,停止mysql服务

[root@9901388b05f6 ~]# systemctl stop mysqld

2,修改配置文件无密码登录

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

在最尾部加上

skip-grant-tables

保存

3,启动mysql

[root@9901388b05f6 ~]# systemctl start mysqld

4,登录mysql

[root@9901388b05f6 ~]#  mysql -u root (此处注意不要加-p)

5, 修改密码,mysql5.7用此语法

mysql> use mysql ;


mysql> update mysql.user set authentication_string=password('123456') where user='root' ;

6.回到第二步骤去掉加上的

skip-grant-tables

保存 重启mysql就ok了

错误二: ERROR 3009 (HY000): Column count of mysql.user is wrong. Expected 45, found 43. Created with MySQL
如果出现此错误:是由于你曾经升级过数据库,升级完后没有使用
mysql_upgrade升级数据结构造成的。
解决办法:
使用mysql_upgrade命令

[root@9901388b05f6 ~]#  mysql_upgrade -u root -p 123456

执行完毕再按照错误一步骤进行一遍.

2.2 接下来设置mysql账号密码 及 外网访问权限

2.1.设置root密码

mysql> set password = password("Trio123.com");

这里写图片描述
2.2.设置远程访问权限

mysql> grant all privileges on *.* to root@'%' identified by "Trio123.com";
mysql> flush privileges;

第一句中”%”表示任何主机都可以远程登录到该服务器上访问。如果要限制只有某台机器可以访问,将其换成相应的IP即可,如:GRANT ALL PRIVILEGES ON . TO root@”172.168.193.25” IDENTIFIED BY “root”;
第二句表示从mysql数据库的grant表中重新加载权限数据。因为MySQL把权限都放在了cache中,所以在做完更改后需要重新加载。

三.双主配置

3.1 配置文件

3.1.1 在192.168.0.131上配置:

[root@9901388b05f6 ~]# vi /etc/my.cnf
server-id=131
log-bin=mysql-bin
auto-increment-increment=2
auto-increment-offset=1

重启Mysql

[root@9901388b05f6 ~]# systemctl restart mysqld

3.1.2 在192.168.0.132上配置:

[root@9901388b05f6 ~]# vi /etc/my.cnf
server-id=132
log-bin=mysql-bin
auto-increment-increment=2
auto-increment-offset=2

重启Mysql

[root@9901388b05f6 ~]# systemctl restart mysqld

3.2 主从设置

3.2.1 在192.168.0.131上执行:

[root@9901388b05f6 ~]# mysql -uroot -p
Enter password: 

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.%' IDENTIFIED  BY 'Xindun123!@';
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      611 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
mysql> stop slave;
mysql> change master to 
       master_host = '192.168.0.132', 
       master_user = 'repl', 
       master_password = 'Xindun123!@', 
       master_log_file = 'mysql-bin.000001',
       master_log_pos = 611;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.132
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1060
               Relay_Log_File: bogon-relay-bin.000002
                Relay_Log_Pos: 769
        Relay_Master_Log_File: mysql-bin.000001
             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: 1060
              Relay_Log_Space: 976
              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: 132
                  Master_UUID: bc35ffd0-57d8-11eb-841a-000c29f08c1b
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

3.2.2 在192.168.0.132上执行:

[root@9901388b05f6 ~]# mysql -uroot -p
Enter password: 

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.%' IDENTIFIED  BY 'Xindun123!@';
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      611 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
mysql> stop slave;
mysql> change master to 
       master_host = '192.168.0.131', 
       master_user = 'repl', 
       master_password = 'Xindun123!@', 
       master_log_file = 'mysql-bin.000001',
       master_log_pos = 611;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.131
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1060
               Relay_Log_File: bogon-relay-bin.000002
                Relay_Log_Pos: 769
        Relay_Master_Log_File: mysql-bin.000001
             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: 1060
              Relay_Log_Space: 976
              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: 131
                  Master_UUID: d8953c29-57d5-11eb-a8f8-000c29a7b2ff
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

四.主主复制测试

测试在其中一个节点mysql进行操作,另一个节点同步成功,主主复制配置成功。

你可能感兴趣的:(centos,linux,mysql,mysql,数据库)