Mysql8.0主从部署

Mysql8.0主从部署

1、mysql双主规划

初始规划

服务器ip 功能
10.0.0.1 主机点、从节点
10.0.0.2 从节点、主节点

2、mysql安装

主节点操作(10.0.0.1)

创建目录

mkdir /data/mysql
mkdir /data/mysql/data
mkdir /data/mysql/log

2、上次rpm包

$ll /data/mysql/
mysql80-community-release-el7-6.noarch.rpm

3、安装mysql

$rpm -ivh /data/mysql/mysql80-community-release-el7-6.noarch.rpm
$rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
$yum -y install mysql-community-server

4、更改配置文件

$vi /etc/my.cnf
[mysqld]
#数据存储目录
datadir=/data/mysql/data
socket=/var/lib/mysql/mysql.sock
#日志存储目录
log-error=/data/mysql/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

$chown -R mysql:mysql /data/mysql/

5、启动

systemctl start mysqld.service
systemctl enable mysqld.service
systemctl status mysqld.service

6、查看启动

ps -ef|grep mysql
ss -lntup|grep 3306

7、登录修改密码(是否修改密码根据实际要求)

#查看临时密码
$grep 'temporary password' /data/mysql/log/mysqld.log
#登录
mysql -u root -p
#设置密码
mysql> set password="123456";
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements  #mysql有密码复杂度要求
mysql> set password="eUok!3qdQ3Dk";
Query OK, 0 rows affected (0.01 sec)
mysql> use mysql;
mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+

8、开启主从

修改 master 的配置( my.cnf)加入下面的内容

$vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
# id 必须唯一!!!
server-id=1

重启

systemctl restart mysqld.service

创建同步用户

#指定了内网ip段
mysql>  CREATE USER 'slave'@'10.0.0.%' IDENTIFIED  BY 'gWOp^$8z9L2D';

授权

mysql>  GRANT REPLICATION SLAVE ON *.* TO 'slave'@'10.0.0.%';
mysql> flush privileges;

查看状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      876 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

#记录下 File 和 Position 的值,之后要用到
从节点操作(10.0.0.2)

创建目录

mkdir /data/mysql -p
mkdir /data/mysql/data
mkdir /data/mysql/log

2、上次rpm包

$ll /data/mysql/
mysql80-community-release-el7-6.noarch.rpm

3、安装mysql

$rpm -ivh /data/mysql/mysql80-community-release-el7-6.noarch.rpm
$rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
$yum -y install mysql-community-server
$chown -R mysql:mysql /data/mysql/

4、更改配置文件

$vi /etc/my.cnf
[mysqld]
#数据存储目录
datadir=/data/mysql/data
socket=/var/lib/mysql/mysql.sock
#日志存储目录
log-error=/data/mysql/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

5、启动

systemctl start mysqld.service
systemctl enable mysqld.service
systemctl status mysqld.service

6、查看启动

ps -ef|grep mysql
ss -lntup|grep 3306

7、登录修改密码(是否修改密码根据实际要求)

#查看临时密码
$grep 'temporary password' /data/mysql/log/mysqld.log
#登录
$mysql -u root -p
#设置密码
mysql> set password="123456";
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements  #mysql有密码复杂度要求
mysql> set password="eUok!3qdQ3Dk";
Query OK, 0 rows affected (0.01 sec)

3、主从部署

主节点操作(10.0.0.1)

修改 master 的配置( my.cnf)加入下面的内容

$vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
# id 必须唯一!!!
server-id=2

重启

systemctl restart mysqld.service

创建同步用户

#指定了内网ip段
mysql>  CREATE USER 'slave'@'10.0.0.%' IDENTIFIED  BY 'gWOp^$8z9L2D';

授权

mysql>  GRANT REPLICATION SLAVE ON *.* TO 'slave'@'10.0.0.%';
mysql> flush privileges;

查看状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      876 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

#记录下 File 和 Position 的值,之后要用到
从节点操作(10.0.0.2)

修改slave 的配置( my.cnf)加入下面的内容

$vi /etc/my.cnf
[mysqld]
# id 必须唯一!!!
server-id=2

重启

systemctl restart mysqld.service

设置主节点参数(根据实际修改)

mysql> CHANGE MASTER TO
    -> MASTER_HOST='10.0.0.1',
    -> MASTER_USER='slave',
    -> MASTER_PASSWORD='gWOp^$8z9L2D',
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=876;

开启主从

mysql> start slave;

查看状态

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 10.0.0.1
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 876
               Relay_Log_File: middleware2-relay-bin.000002
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

#保证
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

4、反向主从

主节点操作(10.0.0.2)
mysql> CREATE USER 'slave'@'10.0.0.%' IDENTIFIED  BY 'gWOp^$8z9L2D';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'10.0.0.%';
Query OK, 0 rows affected (0.01 sec)

mysql>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      876 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


从节点操作(10.0.0.1)
mysql> stop slave;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql>  CHANGE MASTER TO
    ->     MASTER_HOST='10.0.0.2',
    ->     MASTER_USER='slave',
    ->     MASTER_PASSWORD='gWOp^$8z9L2D',
    ->     MASTER_LOG_FILE='mysql-bin.000001',
    ->     MASTER_LOG_POS=876;
Query OK, 0 rows affected, 8 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 10.0.0.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 876
               Relay_Log_File: middleware1-relay-bin.000002
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

5、keepalived高可用

主节点操作(10.0.0.1)

安装keepalived

$yum install -y keepalived
$rpm -ql keepalived
/etc/keepalived
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
/usr/bin/genhash
/usr/lib/systemd/system/keepalived.service
/usr/libexec/keepalived
/usr/sbin/keepalived
/usr/share/doc/keepalived-1.3.5

修改配置文件

$vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id mysql
}

vrrp_script chk_mysql_port {            #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
    script "/data/keepalived/chk_mysql.sh"          #这里通过脚本监测
    interval 5                          #脚本执行间隔,每2s检测一次
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.16   #vip外网ip

    }
    track_script {
       chk_mysql_port
    }
}

创建chk_mysql.sh

$mkdir /data/keepalived
$vi /data/keepalived/chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
    systemctl stop keepalived
fi 

$chmod +x /data/keepalived/chk_mysql.sh
$chmod 777 /data/keepalived/chk_mysql.sh

启动服务

systemctl start keepalived.service
systemctl status keepalived.service
systemctl stop keepalived.service
systemctl enable keepalived.service
systemctl restart keepalived.service

查看vip

$ip a|grep eth0
2: eth0:  mtu 1450 qdisc mq state UP group default qlen 1000
    inet 10.0.0.1/20 brd 10.0.111.255 scope global noprefixroute dynamic eth0
    inet 192.168.200.16/32 scope global eth0

从节点操作(10.0.0.2)

安装keepalived

$yum install -y keepalived
$rpm -ql keepalived
/etc/keepalived
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
/usr/bin/genhash
/usr/lib/systemd/system/keepalived.service
/usr/libexec/keepalived
/usr/sbin/keepalived
/usr/share/doc/keepalived-1.3.5

修改配置文件

$vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id mysql
}

vrrp_script chk_mysql_port {            #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
    script "/data/keepalived/chk_mysql.sh"          #这里通过脚本监测
    interval 5                          #脚本执行间隔,每2s检测一次
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.16  #vip外网ip

    }
    track_script {
       chk_mysql_port
    }
}

创建chk_mysql.sh

$mkdir /data/keepalived
$vi /data/keepalived/chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
    systemctl stop keepalived
fi 

$chmod +x /data/keepalived/chk_mysql.sh
$chmod 777 /data/keepalived/chk_mysql.sh

启动服务

systemctl start keepalived.service
systemctl status keepalived.service
systemctl stop keepalived.service
systemctl enable keepalived.service

查看vip(master停止时)

$ip a|grep eth0
2: eth0:  mtu 1450 qdisc mq state UP group default qlen 1000
    inet 10.0.0.2/20 brd 10.0.111.255 scope global noprefixroute dynamic eth0
    inet 192.168.200.16/32 scope global eth0

你可能感兴趣的:(Mysql8.0主从部署)