主从数据库部署

主从数据库的基本概念

主从数据库把数据库架构分为主数据库和从数据库。从数据库是主数据库的备份,这是提高信息安全的手段。主从数据库服务器不在一个地理位置上,当发生意外时数据库可以保存。
以MySQL 为例,MySQL 主从复制是指数据可以从一个MySQL数据库服务器主节点复制到一个或多个从节点。MySQL 默认采用异步复制方式,这样从节点不用一直访问主服务器来更新自己的数据,数据的更新可以在远程连接上进行,从节点可以复制主数据库中的所有数据库、特定的数据库或者特定的表。
主从数据库部署_第1张图片从库生成两个线程:一个 I/O 线程;另一个 SQL 线程。I/O 线程去请求主库的 Binary Log(简称binlog)并将得到的 binlog 日志写到 Relay Log(中继日志)文件中。主库会生成一个Log Dump 线程。用来给从库I/O线程传binlogSQL线程会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,最终达到数据一致。

主从数据库的优点

①方便做数据的热备份。作为后备数据库,主数据库服务器故障后,可切换到从数据库服务器继续工作,避免数据丢失。
②架构的扩展更容易。业务量越来越大,I/O 访问频率过高,单机无法满足,此时做多库的存储,降低磁盘1/0 访问的频率,提高单个机器的I/0 性能。
③ 读写分离,使数据库能支撑更大的并发。这一点在报表中尤其重要。由于部分报表SQL语句非常的慢,导致锁表,影响前台服务,如果前台使用主库,报表使用从库,那么报表 SQL 将不会造成前台锁,保证了前台速度。

常见的主从形式

  • 一主一从
    一主一从形式是最常见的主从架构,实施起来简单并且有效,不仅可以实现HA,而且还能读写分离,进而提升集群的并发能力。
  • 一从多主
    一从多主形式可提高系统的读性能。
  • 多主一从
    多主一从形式可以将多个 MySQL 数据库备份到一台存储性能比较好的服务器上。
  • 双主复制
    双主复制形式,也就是互做主从复制,每个Master 既是Master,又是另外一台服务器的Slave。这样任何一方所做的变更,都会通过复制应用到另外一方的数据库中。
  • 级联复制
    级联复制模式下,部分 Slave的数据同步不连接主节点,而是连接从节点。因为如果生节点有太多的从节点,就会损耗一部分性能用于复制,那么可以让3~5个从节点连接节点,其他从节点作为二级或者三级与从节点连接,这样不仅可以缓解主节点的压力。且对数据一致性没有负面影响。

实战案例

  • 环境准备
IP 主机名 节点
192.168.0.20 mysql1 主数据库节点
192.168.0.21 mysql2 从数据库节点
  • 基础环境安装(两台节点都需要)
setenforce 0
systemctl stop firewalld

vim /etc/hosts

192.168.0.20 mysql1
192.168.0.21 mysql2

yum install -y mariadb mariadb-serve
systemctl start mariadb
systemctl enable mariadb
  • 初始化数据库并配置主从服务
两节点初始化数据库


[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!

配置mysql1主节点
[root@mysql1 ~]# vim /etc/my.cnf
#添加下面三行内容
[mysqld]
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql # 不同步mysql系统数据库
server_id = 20 #数据库集群中每个节点的Id都不同,一般都是ip最后字段

[root@mysql1 ~]# systemctl restart mariadb
[root@mysql1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, 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 '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave  on *.* to 'user'@'mysql2' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye
[root@mysql1 ~]# 
  • 配置mysql2从节点
[root@mysql2 ~]# vim /etc/my.cnf
#添加下面三行内容
[mysqld]
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql # 不同步mysql系统数据库
server_id = 21 #数据库集群中每个节点的Id都不同,一般都是ip最后字段

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

Copyright (c) 2000, 2017, 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='123456';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
  • 验证主从数据库
[root@mysql1 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, 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;
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,'zhansan','shenzhen');
Query OK, 1 row affected (0.01 sec)
MariaDB [test]> select * from company;
+----+---------+----------+
| id | name    | addr     |
+----+---------+----------+
|  1 | zhansan | shenzhen |
+----+---------+----------+
1 row in set (0.00 sec)
  • 从节点验证复制
[root@mysql2 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, 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 | zhansan | shenzhen |
+----+---------+----------+
1 row in set (0.00 sec)

MariaDB [test]> 

你可能感兴趣的:(云计算1+X,数据库)