搭建Mysql主从数据库

23
五月
2021

mysql 5.7 安装

下载yum源

$ vagrant up		# 启动虚拟机
$ vagrant ssh		# 连接到虚拟机
# 下载mysql 5.7
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 

本地安装yum源

yum install -y mysql-community-server

检查 mysql 源是否安装成功

yum repolist enabled | grep "mysql.*-community.*"

出现以下信息,说明安装成功:

mysql-connectors-community/x86_64 MySQL Connectors Community                 20
mysql-tools-community/x86_64      MySQL Tools Community                      12
mysql57-community/x86_64          MySQL 5.7 Community Server                 50

使用 yum install 命令安装Mysql:

$ yum install -y mysql-community-server
...
已安装:
  mysql-community-libs.x86_64 0:5.7.34-1.el7                                            
  mysql-community-libs-compat.x86_64 0:5.7.34-1.el7                                     
  mysql-community-server.x86_64 0:5.7.34-1.el7                                          

作为依赖被安装:
  mysql-community-client.x86_64 0:5.7.34-1.el7                                          
  mysql-community-common.x86_64 0:5.7.34-1.el7                                          

替代:
  mariadb-libs.x86_64 1:5.5.68-1.el7                                                    

完毕!
# 提示安装成功

启动mysql:

systemctl start mysqld

启动完成后,查看Mysql的状态:

systemctl status mysqld

上述命令显示以下信息,则安装成功:

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2021-05-18 20:50:27 CEST; 2min 18s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 12715 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 12496 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 12718 (mysqld)
   Memory: 158.3M
   CGroup: /system.slice/mysqld.service
           └─12718 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid...

5月 18 20:50:13 k8s-master systemd[1]: Starting MySQL Server...
5月 18 20:50:27 k8s-master systemd[1]: Started MySQL Server.

设置开机启动:

systemctl enable mysqld

修改MySQL密码

mysql 安装完成之后,生成的默认密码在 /var/log/mysqld.log 文件中。使用 grep 命令找到日志中的密码。

 grep 'temporary password' /var/log/mysqld.log
2021-05-18T18:50:21.200923Z 1 [Note] A temporary password is generated for root@localhost: ujTio<m:g5S%

首次登录mysql,修改密码:

mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Test@123'; 

允许远程登录

修改 root 为允许远程连接,生产环境不建议:

mysql> use mysql;
mysql> UPDATE user SET Host='%' WHERE User='root';
mysql> flush privileges;

开启日志(非必须)

(非必须)开启sql语句的日志,生产环境不建议开启:

查看日志目录,并开启sql语句的日志:

mysql>  show variables like '%general_log%';
mysql>  set global general_log=on;

开启后,重启Mysql ,上述开启日志配置将失效。

Mysql的主从配置

机器准备

在二台机器上按照上面的步骤安装完Mysql5.7,二台机器的ip分别为:

  • 10.0.2.15 172.28.128.3 主
  • 10.0.2.15. 172.28.128.4 从

在二台机器上分别创建2个数据库,分别为test和test2,字符编码为utf8:

mysql> CREATE DATABASE `test` CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (2.42 sec)
mysql> CREATE DATABASE `test2` CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (1.64 sec)
mysql> show databases;

关闭防火墙

要保证防火墙3306端口开放,如果只是为了学习,可以直接关闭防火墙。

centos关闭防火墙方法:service iptables stop或者systemctl stop firewalld

master 节点配置

[root@k8s-master vagrant]# hostname -I
10.0.2.15 172.28.128.3 172.17.0.1 

在 172.28.128.3 机器上配置

vim /etc/my.cnf

#server-id给数据库服务的唯一标识
server-id=1
#
##log-bin设置此参数表示启用binlog功能,并指定路径名称
log-bin=/var/lib/mysql/mysql-bin
sync_binlog=0
##设置日志的过期天数
expire_logs_days=7
binlog-do-db=test
binlog-do-db=test2
binlog-ignore-db=information_schema
binlog-ignore-db=sys
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
  • 这里的server-id用于标识唯一的数据库,在从库必须设置为不同的值。
  • binlog-ignore-db:表示同步的时候忽略的数据库
  • binlog-do-db:指定需要同步的数据库

重启mysql,配置生效,执行以下的命令:systemctl restart mysqld

重启成功后,登录mysql。

赋予从库权限账号,允许用户在主库上读取日志,赋予 172.28.128.4 也就是Slave机器有File权限,
只赋予Slave机器有File权限还不行,还要给它REPLICATION SLAVE的权限才可以。

mysql> grant FILE on *.* to 'root'@'172.28.128.4' identified by 'Test@124';
mysql> grant replication slave on *.* to 'root'@'172.28.128.4' identified by 'Test@124';
mysql> flush privileges;

这里的用户是同步的时候从库使用的用户。

重启mysql (systemctl restart mysqld),登录mysql,查看主库信息

mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000001 |      154 | test,test2   | information_schema,sys,mysql,performance_schema |                   |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (1.67 sec)

配置从库

1、修改/etc/my.cnf

log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=sys
binlog-ignore-db=mysql
replicate-do-db=test
replicate-do-db=test2
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60

注意:两个从库的server-id不一样,需要唯一。

2、修改完/etc/my.cnf后,重启一下mysql (systemctl restart mysqld)

进入Slave的mysql控制台,执行下面操作:

mysql>  mysql -uroot -p
mysql> stop slave;
mysql> 
change master to   master_host='172.28.128.3',master_user='root',master_password='Test@123',master_log_file='mysql-bin.000001', master_log_pos=154;
mysql> start slave;

注意:上面的master_log_file是在Master中show master status显示的File,
而master_log_pos是在Master中show master status显示的Position。

配置第二个从库的时候,需要重新从matser获取File和position。
3、然后可以通过show slave status查看配置信息。

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 172.28.128.3
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: k8s-node1-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test,test2
          Replicate_Ignore_DB: mysql
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0

上面的信息有Slave_IO_Running: Yes和Slave_SQL_Running: Yes,证明主从同步成功。

参考

https://dev.mysql.com/doc/refman/5.7/en/replication.html

https://segmentfault.com/a/1190000010867488

TAG

网友评论

共有访客发表了评论
请登录后再发布评论,和谐社会,请文明发言,谢谢合作! 立即登录 注册会员