微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MySQL两主配置

双主模式:两台服务器互为主从,任何一台服务器数据变更,都会通过复制应用到另外一方的数据库中。

  1. 安装wget依赖

    yum install wget -y

  2. 下载MysqL5.7.26到data目录

    wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

  3. 解压MysqL

    tar -zxvf MysqL-5.7.26-linux-glibc2.12-x86_64.tar.gz

  4. 修改MysqL文件

    mv MysqL-5.7.26-linux-glibc2.12-x86_64.tar.gz MysqL-5.7.26

  5. 创建MysqL数据存储文件

    cd /MysqL-5.7.26

    mkdir data

    chmod -R 777 /data/MysqL-5.7.26/data/

  6. 创建组和用户,并将用户加入组

    groupadd MysqL

    useradd -g MysqL MysqL

  7. 修改Master1 MysqL配置文件

    vi /etc/my.cnf

    [MysqLd]
    bind-address=0.0.0.0
    port=3306
    log-bin=MysqL-bin
    sync-binlog=1
    #双主配置
    relay_log=MysqL-relay-bin
    log_slave_updates=1
    auto_increment_offset=1
    auto_increment_increment=2
    #skip-grant-tables
    user=MysqL
    basedir=/data/MysqL-5.7.26
    datadir=/data/MysqL-5.7.26/data
    socket=/tmp/MysqL.sock
    log-error=/data/MysqL-5.7.26/data/MysqL.err
    pid-file=/data/MysqL-5.7.26/data/MysqL.pid
    #character config
    character_set_server=utf8mb4
    symbolic-links=0
    explicit_defaults_for_timestamp=true
  8. 修改Master2 my.cnf文件

    [MysqLd]
    server-id=2
    log-bin=MysqL-bin
    sync-binlog=1
    relay_log=MysqL-relay-bin
    log_slave_updates=1
    auto_increment_offset=2
    auto_increment_increment=2
    bind-address=0.0.0.0
    port=3306
    #skip-grant-tables
    user=MysqL
    basedir=/data/MysqL-5.7.26
    datadir=/data/MysqL-5.7.26/data
    socket=/tmp/MysqL.sock
    log-error=/data/MysqL-5.7.26/data/MysqL.err
    pid-file=/data/MysqL-5.7.26/data/MysqL.pid
    log-slave-updates = 1
    #character config
    character_set_server=utf8mb4
    symbolic-links=0
    explicit_defaults_for_timestamp=true
  9. 进入bin目录,初始化MysqL

    cd /data/MysqL-5.7.26/bin

    ./MysqLd --initialize --user=MysqL --datadir=/data/MysqL-5.7.26/data/ --basedir=/data/MysqL-5.7.26/

  10. 查看MysqL初始化密码

    cat /data/MysqL-5.7.26/data/MysqL.err

  11. 添加链接,可以在任意目录执行MySQL命令

    ln -s /data/MysqL-5.7.26/support-files/MysqL.server /etc/init.d/MysqL

    ln -s /data/MysqL-5.7.26/bin/MysqL /usr/bin/MysqL

    service MysqL restart

  12. 登录MysqL修改密码

    vi /etc/my.cnf

    去掉#skip-grant-tables的注释

    MysqL -u root -p

    use MysqL

    修改密码

    update user set authentication_string=password('Pw!123456') where user='root';

    |alter user USER() identified by 'Pw!123456';

    开启远程访问权限

    update user set host ='%' where user ='root';

    刷新权限

    flush privileges;

    重启MysqL

    service MysqL restart

  13. MysqL端口加入防火墙的白名单

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

    systemctl restart firewalld.service

  14. 重启MysqL验证

    service MysqL restart

    MysqL -u root -p

    查看二进制日志是否开启

    show global variables like '%log_bin%';

    log_bin value 等于ON 说明开启

    查看主节点二进制日志列表 (以后会有多个)

    show master logs;

    查看主节点的serverid

    show global variables like '%server%';

  15. MysqL中创建从服务器的用户和权限

    GRANT REPLICATION SLAVE ON . TO 'root'@'%' IDENTIFIED BY 'abc123';

    flush privileges;

  16. 查看状态

    show master status;

  17. 配置双主关系

    Maser01
    MysqL> stop slave;
    MysqL> change master to master_host='192.168.0.103',master_port=3306,master_user='root',master_password='Pw!123456',master_log_file='MysqL-bin.000003',master_log_pos=403;



    master02
    MysqL> stop slave;
    MysqL> change master to master_host='192.168.0.102',master_port=3306,master_user='root',master_password='123456',master_log_file='MysqL-bin.000004',master_log_pos=403;

    #查看状态
    MysqL> start slave;
    MysqL> show slave status \G;

    注意没报错并且Slave_IO_Running和Slave_sql_Running都为YES,则配置成功。
  18. 设置开机自启动

    查看自启动的服务

    chkconfig --list

  19. 设置自启动

    cd /etc/rc.d/init.d

    cp /data/MysqL-5.7.26/support-files/MysqL.server /etc/rc.d/init.d/

    赋予可执行权限:chmod +x /etc/init.d/MysqL.server

    添加为服务: chkconfig --add MysqL.server

  20.  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐