mysql主从复制

一、环境

  1. 服务器A:188.188.188.3

    运行Mysql主数据库和Amoeba

  2. 服务器B:188.188.188.4

    运行Mysql从数据库

二、主从复制

  1. 查看A服务器是否已经安装了Mysql数据库:[root@localhost ~]# rpm -aq | grep mariadb

    若无消息显示,则进行Mysql安装,否则跳过此步骤。

    卸载原本数据库:[root@localhost ~]# yum remove mariadb*

    安装数据库:[root@localhost ~]# yum install -y mariadb*

    启动:[root@localhost ~]# systemctl start mariadb

    开机自启:[root@localhost ~]# systemctl enable mariadb

  2.初始化数据库:[root@localhost ~]# mysql_secure_installation

    按enter

    n

    y                                           

    y

    y

    y

  3.连接数据库:[root@localhost ~]# mysql

  4.授权:MariaDB [(none)]> grant all on *.* to root@'%' identified by '123456';

  5.刷新:MariaDB [(none)]> flush privileges;

  6.退出:MariaDB [(none)]> exit

  7.服务器B也同样安装数据库

主数据库配置(188.188.188.3):

  1. 备份:[root@localhost ~]# cp /etc/my.cnf /etc/my.cnfbak
  2. 修改数据库配置文件:[root@localhost ~]# vi /etc/my.cnf

文件内容为:

                   [mysqld]

     max_connections=1000

                  

     binlog-ignore-db=mysql #新增

     binlog-ignore-db=information_schema #新增

                  

     log-bin=mysql-bin #新增

     server-id=1 #新增

                  

     datadir=/var/lib/mysql

     socket=/var/lib/mysql/mysql.sock

     user=mysql

     # Disabling symbolic-links is recommended to prevent assorted security risks

     symbolic-links=0

                  

     [mysqld_safe]

     log-error=/var/log/mysqld.log

     pid-file=/var/run/mysqld/mysqld.pid

  3.重启数据库:[root@localhost ~]# systemctl restart mariadb

  4.登陆主数据库:[root@localhost ~]# mysql

  5.查看主数据库状态:MariaDB [(none)]> show master status\G

 

 可以看出,Binlog_Ignore_DB显示的信息就是刚才我们在配置文件所配置的信息。此外,还有两个重要的参数需要记下:mysql-bin.000001和245。从数据库就是根据这两个参数,完成主从复制,以达到数据同步的效果。

  6.主数据库开发授权用户:MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by '123456';

  7.修改iptables:[root@localhost ~]# vi /etc/sysconfig/iptables

文件内容为:-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

  8.主数据库配置完成

从数据库配置(188.188.188.4):

  1. 备份:[root@root ~]# cp /etc/my.cnf /etc/my.cnfbak
  2. 修改配置文件:[root@root ~]# vi /etc/my.cnf

[mysqld]

max_connections=1000

server-id=2 #新增

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

relay-log=relay-bin

relay-log-index=slave-relay-bin.index

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

3.设置主从数据库同步点:[root@root ~]# mysql

MariaDB [(none)]> change master to master_host='188.188.188.3',master_user='root',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=245;

还记得mysql-bin.000001和245这两个参数吗?没错,就是我们在主数据库查看master状态所显示的信息。

4.启动主从复制: MariaDB [(none)]> slave start;

5.查看slave状态:MariaDB [(none)]> show slave status\G

 

 只有当Slave_IO_Running和Slave_SQL_Running都显示Yes时,才表示主从复制配置成功。否则失败,检查上述配置过程。

主从复制验证:

  1. 在主数据建立一个demo数据库,看两个从数据库是否会自动进行复制。

在服务器A登录主数据库,查看现有数据库:MariaDB [(none)]> show databases;

 

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3.新增一个测试数据库demo:MariaDB [(none)]> create database demo;

4.登录从数据库,查询数据库:MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| demo               |

| mysql              |

| performance_schema |

+--------------------+

可以发现,当主数据库发生改动,从数据库会相应同步,并且同步的过程是异步进行的。因此,可以验证我们配置的主从复制已经生效。

posted @ 2020-07-27 15:20  小祖宗的运维之路  阅读(8)  评论(0)    收藏  举报