Mysql主从复制

主从复制

  • 准备
  • 安装mysql
    • yum安装mysql
    • 修改密码授权远程登录
  • 配置主从
    • Master
    • Slave
  • 测试
  • 遇到问题
    • 当Slave_SQL_Running:no
    • 当Slave_io_running:no
    • 当Slave_SQL_Running:no且Slave_io_running:no
    • 从库设置只读

准备

操作系统: CentOS 7.6 64bit
Mysql -v : 5.7.36(主从数据库版本一致)

查看是否已经安装了mariadb 因为mariadb是mysql的分支可能会有冲突
检查命令:yum list installed | grep mariadb
卸载命令:yum  remove mariadb-libs.x86_64
按Y

安装mysql

yum安装mysql

1从Mysql官网下载MySqrpm包。

wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm

2安装源

yum install mysql57-community-release-el7-11.noarch.rpm

3安装mysql服务

yum install mysql-community-server

4启动mysql服务

systemctl start mysqld

5.开启防火墙(我基本上会开启防火墙因为被比特币勒索过...)

systemctl start firewalld

6.开放3306端口

firewall-cmd --add-port=3306/tcp --permanent

7.重启防火墙策略
firewall-cmd --reload
systemctl restart firewalld

服务器控制台也要开启3306端口
在这里插入图片描述

查看mysql启动状态

systemctl start mysqld开启
systemctl stop mysqld关闭
systemctl status mysqld 查看状态

systemctl status mysqld
Mysql主从复制_第1张图片

修改密码授权远程登录

查看mysql初始密码
cat /var/log/mysqld.log |grep password
修改一个自己记得住的密码
mysql -uroot -p
初始化的密码
进来之后进行操作会报:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
需要更新下密码策略
//降低安全级别
set global validate_password_policy=LOW;
//设置密码长度
set global validate_password_length=6;
//设置新密码
alter user 'root'@'localhost' identified by '123456';
//授权远程链接
grant all privileges on *.* to root@'%' identified by '123456';
//刷新mysql权限
flush privileges;

配置主从

Master

mysql -uroot -p
//192.168.4.170是slave从机的IP 让从机能够链接主机
GRANT REPLICATION SLAVE ON *.* to 'root'@'192.168.4.170' identified by '123456'

进入/etc/my.cnf中
# 开启binlog
log-bin=mysql-bin
server-id=1
# 需要同步的数据库,如果不配置则同步全部数据库
binlog-do-db=test
# binlog日志保留的天数,清除超过10天的日志
# 防止日志文件过大,导致磁盘空间不足
expire-logs-days=10 

//重启mysql
service mysqld restart

show master status\G;可查看配置信息
Mysql主从复制_第2张图片

Slave

进/etc/my.cnf
# 开启binlog
log-bin=mysql-bin
# 不要和其他mysql服务id重复即可
server-id=2

进msql>

CHANGE MASTER TO 
MASTER_HOST='192.168.0.xxx',//主机IP
MASTER_USER='root',//之前创建的用户账号
MASTER_PASSWORD='J123456',//之前创建的用户密码
MASTER_LOG_FILE='mysql-bin.000001',//master主机的binlog日志名称
MASTER_LOG_POS=862,//binlog日志偏移量
master_port=3306;//端口

//如下
CHANGE MASTER TO MASTER_HOST='1.116.253.83',MASTER_USER='root',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=862,master_port=3306;

# 启动slave服务
start slave;

**show slave status\G;**查看状态
Mysql主从复制_第3张图片

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.4.170 //主机ip
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001 //binlog日志文件名称
          Read_Master_Log_Pos: 4822
               Relay_Log_File: VM-4-5-centos-relay-bin.000003
                Relay_Log_Pos: 1643
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes /Slave_IO线程、SQL线程都在运行
            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: 4822
              Relay_Log_Space: 5416
              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: 101
                  Master_UUID: 0600
             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: (test_db,test_db)
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

测试

在Master中创建表

CREATE  TABLE  test
 (
id varchar(20) not null,
name VARCHAR(10),
PRIMARY key(id)
)ENGINE=INNODB DEFAULT CHARSET=utf8mb4

从表也会出现

遇到问题

当Slave_IO_Running:或Slave_SQL_Running:为no时 从库无法复制主库
mysql replication 中slave机器上有两个关键的进程,一个是slave_sql_running,一个是Slave_IO_Running,一个负责与主机的io通信,一个负责自己的slave mysql进程

当Slave_SQL_Running:no

mysql>stop slave;                                                      
mysql>SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE;            
mysql>start slave;                                                      

当Slave_io_running:no

//主库
> show master status\G 
 File: mysql-bin.000001
//从库
slave stop;                 
>CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001',//与主库 保持一致MASTER_LOG_POS=0;  
 slave start;                               

当Slave_SQL_Running:no且Slave_io_running:no

以上都执行一遍

从库设置只读

--查看全局只读状态是否开打
show global variables like “%read_only%”; 
 
--锁定表用户更新,限定普通账户不解锁情况下不能写入数据
flush tables with read lock; 
 
--打开全局只读开关
set global read_only=1;
 
--再次查看开关是否打开
show global variables like “%read_only%”;

//打开
--解除表锁定
 unlock tables; 
 
--关闭只读开关
set global read_only=0;
 

参照:https://blog.csdn.net/alitech2017/article/details/108241782.
https://blog.csdn.net/qq_21108311/article/details/82377763.

你可能感兴趣的:(Mysql,mysql)