【分布式】部署MySQL主从数据库--LNMP构建(超详细)

目录

主从数据库(MySQL)

主从数据库的基本概念

主从数据库的优点

常见的主从形式

部署主从数据库

基础环境部署

(1)修改主机名

(2)关闭防火墙及SElinux服务

(3)配置hosts主机映射

(4)配置网络yum源安装数据库服务

初始化数据库并配置主从服务

(1)初始化数据库

(2)配置mysql1主节点

(3)配置mysql2从节点

验证数据库主从服务

(1)主节点创建数据库

(2)从节点验证复制功能


主从数据库(MySQL)

主从数据库的基本概念

主从数据库把数据库架构分为主数据库和从数据库。从数据库是主数据库的备份,这是提高信息安全的手段。主从数据库服务器不在一个地理位置上,当发生意外时数据库可以保存。

就以MySQL为例,MySQL主从复制是指数据可以从一个MySQL数据库服务器主节点复制到一个或多个从节点。MySQL默认采用异步复制的方式,这样从节点不用一致访问主服务器来更新自己的数据,数据的更新可以在远程连接上进行,从及诶到哪可以复制主数据库中的所有数据库、特定的数据库或者特定的表。

MySQL主从复制涉及3个线程,一个运行在主节点(Log Dump Thread),其余两个(I/O Thread、SQL Thread)运行在从节点,我们可以看一张图稍微了解一下它的工作原理

主从数据库架构图

从库生成两个线程:一个I/O线程;另一个SQL线程。I/O线程去请求主库的BinaryLog(简称binlog),并将得到的binlog日志写到Relay Log(中继日志)文件中。主库会生成一个Log Dump线程,用来给从库I/O线程传binlog。SQL线程会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,最终达到数据一致。

主从数据库的优点

主从数据库的优点包括:

  1. 提高读取性能:主库负责处理写操作,从库负责处理读操作,将读写负载分散到多个数据库节点上,能够提高读取性能。
  2. 实现数据备份和灾备:主库的数据可以实时同步到从库,从库可以作为主库的备份,当主库出现故障时可以快速切换到从库,实现数据的灾备。
  3. 降低单点故障风险:通过设置多个从库,当主库发生故障时可以快速切换到从库,避免单点故障。
  4. 提高系统的可用性和稳定性:当主库发生故障时,从库可以接替主库的角色,确保系统的持续运行。
  5. 支持数据读写分离:主库处理写操作,从库处理读操作,可以根据业务需求将读操作分发到不同的从库,提高系统的扩展性和并发处理能力。
  6. 支持地理位置分布:主从数据库可以部署在不同的地理位置上,实现数据的分布式存储和处理,提高系统的可用性和响应速度。

总之,主从数据库能够提高系统的读取性能,实现数据备份和灾备,降低单点故障风险,提高系统的可用性和稳定性,支持数据读写分离和地理位置分布等优点。

常见的主从形式

常见的MySQL主从形式包括:

  1. 单主单从:一个主库负责处理写操作,一个从库负责处理读操作。
  2. 单主多从:一个主库负责处理写操作,多个从库负责处理读操作,可以提高读取性能和系统的可用性。
  3. 多主多从:多个主库负责处理写操作,多个从库负责处理读操作,可以进一步提高系统的读取性能和负载均衡能力。
  4. 主从链式复制:多层级的主从复制关系,上层从库作为下层主库的从库,可以实现数据的级联复制和数据的分布式存储。
  5. 级联复制:级联复制模式下,部分Slave的数据同步不连接主节点,而是连接从节点。因为如果主节点有太多的从节点,就会损耗一部分性能用于复制,那么可以让3到5个从节点连接主节点,其他从节点作为二级或者三级与从节点连接,这样不仅可以缓解主节点压力,并且对数据一致性没有负面影响。

这些主从形式根据业务需求和系统性能需求,我们可以选择适合的方式来搭建MySQL主从环境。

部署主从数据库

Linux操作系统节点规划准备

 

基础环境部署

(1)修改主机名

使用远程连接工具连接到两台虚拟机,并对其虚拟机修改主机名

mysql1节点

[root@localhost ~]# hostnamectl set-hostname mysql1
[root@localhost ~]# hostnamectl
   Static hostname: mysql1

mysql2节点

[root@localhost ~]# hostnamectl set-hostname mysql2
[root@localhost ~]# hostnamectl
   Static hostname: mysql2

上面修改完后使用“bash”或重连终端实现更名

(2)关闭防火墙及SElinux服务

两个节点都关闭防火墙firewalld及SElinux服务


systemctl stop firewalld
 
systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

setenforce 0                //这个只是临时关闭,永久关闭要编辑配置文件/etc/selinux/config

        
(3)配置hosts主机映射

两个节点配置/etc/hosts文件

cat >> /etc/hosts << EOF
> 192.168.100.30 mysql1
> 192.168.100.40 mysql2
> EOF
(4)配置网络yum源安装数据库服务

两个节点拉取网络yum源,安装数据库服务

拉取网络源
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

安装数据库服务
yum install -y mariadb mariadb-server

两个节点启动数据库并设置开机自启动

systemctl start mariadb

systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

初始化数据库并配置主从服务

(1)初始化数据库

两个节点初始化数据库,配置数据库root密码为000000

[root@mysql1 ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):                           //回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y                                                   //设置密码,y
New password:                                                                //我这里密码设置为000000
Re-enter new password:                                                       //确认再次输入密码
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y                
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n                                           //注意这里为n
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
(2)配置mysql1主节点

修改mysql1节点的数据库配置文件/etc/my.cnf

[root@mysql1 ~]# vi /etc/my.cnf
在[mysqld]模块下添加下面内容
[mysqld]
log_bin=mysql-bin
binlog_ignore_db=mysql
server_id=30

配置解释:
#log_bin:记录操作日志
#binlog_ignore_db:不同步mysql系统数据库
#server_id:数据库集群中节点id,一般使用IP地址的最后段的数字,我这里IP的最后的一个数字是30,所以我设置为30


修改完后保存退出,重启数据库服务,并进入数据库
[root@mysql1 ~]# systemctl restart mariadb
[root@mysql1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

在mysql1节点中授权在任何客户端机器上可以以root用户登录到数据库,然后再主节点上创建一个user用户连接节点mysql2,并赋予从节点同步主机同步主节点数据库的权限

MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000";
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by "000000";
Query OK, 0 rows affected (0.00 sec)
(3)配置mysql2从节点

修改mysql2节点的数据库配置文件/etc/my.cnf

[root@mysql2 ~]# vi /etc/my.cnf
在[mysqld]模块下添加下面内容
[mysqld]
log_bin=mysql-bin
binlog_ignore_db=mysql
server_id=40

解释#server_id:数据库集群中节点id,一般使用IP地址的最后段的数字,我这里IP的最后的一个数字是40,所以我设置为40,和主节点一个道理

修改完后保存退出,重启数据库服务,并进入数据库
[root@mysql2 ~]# systemctl restart mariadb

[root@mysql2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

配置从接地那连接主节点的连接信息,master_host为主节点主机名mysql1,master_user为mysql1中创建的用户user
MariaDB [(none)]> change master to master_host='mysql1',master_user='user',master_password='000000';
Query OK, 0 rows affected (0.00 sec)


配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status \G命令,并查看从节点服务状态
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql1
                  Master_User: user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 529
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 813
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            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: 529
              Relay_Log_Space: 1109
              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: 30
1 row in set (0.00 sec)
 

当看到Slave_IO_Running和Slave_SQL_Running的状态都是Yes,则说明数据库主从集群配置成功

 

查看数据库集群状态图

验证数据库主从服务

(1)主节点创建数据库

现在主节点mysql1中创建test,并在test库中创建company表,插入表数据,创建完成后,查看company表数据

[root@mysql1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#创建数据库
MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)
#选择数据库
MariaDB [(none)]> use test;
Database changed
#创建表
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
Query OK, 0 rows affected (0.00 sec)
#插入表数据
MariaDB [test]> insert into company values(1,"jiangxi","fuzhou");
Query OK, 1 row affected (0.00 sec)
#查看表数据
MariaDB [test]> select * from company;
+----+---------+--------+
| id | name    | addr   |
+----+---------+--------+
|  1 | jiangxi | fuzhou |
+----+---------+--------+
1 row in set (0.00 sec)


(2)从节点验证复制功能

登录mysql2节点的数据库,查看数据库列表

[root@mysql2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#列出数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
#选择数据库
MariaDB [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
#列出表
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| company        |
+----------------+
1 row in set (0.00 sec)
#查询表内容
MariaDB [test]> select * from company;
+----+---------+--------+
| id | name    | addr   |
+----+---------+--------+
|  1 | jiangxi | fuzhou |
+----+---------+--------+
1 row in set (0.00 sec)


从节点可以看到主节点的数据库、表、信息,说明从数据库的复制功能成功!
从节点验证主从复制图

 

 至此,搭建MySQL主从数据库环境完成!

  • 21
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值