主从数据库的介绍与部署

部署主从数据库

主从数据库的概念:
主从数据库把数据库架构分为主数据库和从数据库。从数据库是主数据库的备份,这是一个提高信息安全的手段。主从数据库服务器不在一个地理位置上,当发生意外时,数据库可以保存。
主从数据库的优点:
1、 方便做数据热备份。作为后备数据库,主数据库服务器故障后,可切换从数据库继续工作,避免数据丢失。
2、 架构扩展更容易。业务量越来越大, I/O访问频率过高,单机无法满足,此时做多库的存储,降低磁盘I/O访问的频率,提高单个机器I/O性能。
3、 读写分离,使数据库能支撑更大的并发。
常见的主从形式:
1、 一从一主
实施简单有效,不仅可以实现HA,而且还能读写分离,进而提升集群的并发能力。
2、 一从多主
提高系统的读写性能
3、 多主一从
多主一从可以将多个MySQL数据库备份到一台存储性能比较好的服务器上。
4、 双主复制
就是互作主从复制/任何一方所做的变更,都会通过复制应用到另外一方的数据库中。
5、 级联复制
此模式下,部分slave的数据同步不连接主节点,而是连接从节点。如果主节点有太多的从节点,就会损耗一部分性能用于replication,那么可以让3~5个从节点连接主节点,其他从节点作为二级或者三级与从节点连接,这样不仅可以缓解主节点的压力,并且对数据一致性没有负面影响。
一、目标
(1)了解数据库服务的安装。
(2)了解主从数据库集群的配置架构。
二、要求
1、规划节点
IP 主机名 节点
192.168.200.11 Mysql1 主数据库节点
192.168.200.12 Mysql2 从数据库节点
2、使用本地 PC 环境的 VMWare Workstation 软件进行实操练习,镜像使用提供的
CentOS-7-x86_64-DVD-1511.iso。虚拟机配置为 1 核/2G 内存/20G 硬盘
步骤:
1、 基础环境安装
(1) 修改主机名
192.168.200.11为mysql1,192.168.200.12为mysql2
mysql1 节点:

[root@localhost ~]# hostnamectl set-hostname mysql1 
[root@localhost ~]# logout 
[root@mysql1 ~]# hostnamectl  
   Static hostname: mysql1 
         Icon name: computer-vm 
           Chassis: vm 
        Machine ID: 179f6c8f2e7942ef81b0f5565a6883fa 
           Boot ID: 69ad020d53e54892b9005f82e182c140 
    Virtualization: vmware 
  Operating System: CentOS Linux 7 (Core) 
       CPE OS Name: cpe:/o:centos:centos:7 
            Kernel: Linux 3.10.0-327.el7.x86_64 
      Architecture: x86-64 

mysql2节点:

[root@localhost ~]# hostnamectl set-hostname mysql2 
[root@localhost ~]# logout 
[root@mysql2 ~]# hostnamectl  
   Static hostname: mysql2 
         Icon name: computer-vm 
           Chassis: vm 
        Machine ID: 179f6c8f2e7942ef81b0f5565a6883fa 
           Boot ID: 816b270a1275496caa3254300fc359c4 
    Virtualization: vmware 
  Operating System: CentOS Linux 7 (Core) 
       CPE OS Name: cpe:/o:centos:centos:7 
            Kernel: Linux 3.10.0-327.el7.x86_64 
      Architecture: x86-64

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

# setenforce 0 
# systemctl stop firewalld

(3) 配置hosts文件
配置两个节点的/etc/hosts文件

# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.11 mysql1
192.168.200.12 mysql2
(4)配置yum源并安装数据库服务
#  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.

2.初始化数据库并配置主从服务
(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
New password: 
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
 ... 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!
[root@mysql2 ~]# 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
New password: 
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
 ... 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中的[mysqld]增添如下内容。

[root@mysql1 ~]# vim /etc/my.cnf

在这里插入图片描述
命令解释:
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql #不同步 mysql 系统数据库
server_id = 30 #数据库集群中的每个节点 id 都要不同,
一般使用 IP 地址的最后段的数字,例如 192.168.200.11,server_id 就写 11

重启数据库服务,并进入数据库,命令如下:

[root@mysql1 ~]# vim /etc/my.cnf
[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.65-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 '0000000';
Query OK, 0 rows affected (0.00 sec)

(3)配置 mysql2 从节点 修改mysql2节点的数据库配置文件,在配置文件/etc/my.cnf中的[mysqld]增添如下内容。

[root@mysql2 ~]# vim /etc/my.cnf

在这里插入图片描述

修改部分解释:
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql #不同步 mysql 系统数据库
server_id = 40 #数据库集群中的每个节点 id 都要不同,
一般使用 IP 地址的最后段的数字,例如 192.168.200.12,server_id 就写 12

在从节点 mysql2 上登录 MariaDB 数据库,配置从节点连接主节点的连接信息。
master_host 为主节点主机名 mysql1,master_user 为上一步中创建的用户 user,命令如下:

[root@mysql2 ~]#  mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.65-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)]>  change master to 
    -> master_host='mysql1',master_user='user',master_password='000000'; 
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]>

配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用 show slave status\G
命令,并查看从节点服务状态,如果 Slave_IO_Running 和 Slave_SQL_Running 的状态都为YES,则从节点服务开启成功。命令如下:

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: 815
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 1099
        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: 815
              Relay_Log_Space: 1395
              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: 11
1 row in set (0.00 sec)

Slave_IO_Running 和 Slave_SQL_Running 的状态都是 Yes,配置数据库主从集
群成功。

3.验证数据库主从服务
(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 2
Server version: 5.5.65-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)]> 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 '0000000';
Query OK, 0 rows affected (0.00 sec)

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)

MariaDB [(none)]> exit
Bye
[root@mysql1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.65-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,"alibaba","china");
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select * from company;
+----+---------+-------+
| id | name    | addr  |
+----+---------+-------+
|  1 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)

MariaDB [test]>

(2)从节点验证复制功能
登录 mysql2 节点的数据库,查看数据库列表。找到 test 数据库,查询表,并查询内容
验证从数据库的复制功能,命令如下:

[root@mysql2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 5.5.65-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 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)

MariaDB [test]>

可以查看到主数据库中刚刚创建的库、表、信息,验证从数据库的复制功能成功。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值