Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >主从数据库案例详解

主从数据库案例详解

作者头像
Weiyang
发布于 2020-06-15 08:31:53
发布于 2020-06-15 08:31:53
1.2K00
代码可运行
举报
文章被收录于专栏:九思学舍九思学舍
运行总次数:0
代码可运行

一、案例目标

(1)了解数据库服务的安装。

(2)了解主从数据库集群的配置架构。


二、案例分析

1. 规划节点

以下IP为九思教程实验配置IP,可根据Linux实验环境自行修改。

Linux操作系统的单节点规划,如下图:

IP

主机名

节点

内网IP192.168.0.18

mysql1

主数据库节点

内网IP192.168.0.155

mysql2

从数据库节点


2. 基础准备

使用鲲鹏架构服务器(Linux)进行下述实验。操作系统为CentOS 7.6 64bit with ARM。本实验购买了华为云的弹性公网IP,IP为116.63.38.164(mysql1外网地址)和116.63.34.78(mysq2外网地址),可以使用远程连接工具访问此虚拟机。若在实训平台中进行此实验,不需要公网IP。


三、案例实施

1. 基础配置
(1)修改主机名

修改两台虚拟机的主机名分别为mysql1和mysql2,命令如下:

mysql1节点:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@localhost ~]# hostnamectl set-hostname mysql1
[root@mysql1 ~]# hostnamectl 
   Static hostname: mysql1
         Icon name: computer
        Machine ID: 3f192828770d4d05a3fa58b89c591d71
           Boot ID: 700c9a30c22b4a5daf702cab405d978a
  Operating System: CentOS Linux 7 (AltArch)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 4.18.0-80.7.2.el7.aarch64
      Architecture: arm64

mysql2节点:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@localhost ~]# hostnamectl set-hostname mysql2
[root@mysql2 ~]# hostnamectl 
   Static hostname: mysql2
         Icon name: computer
        Machine ID: 3f192828770d4d05a3fa58b89c591d71
           Boot ID: 1d44cf75207644cfa038c7548dbda530
  Operating System: CentOS Linux 7 (AltArch)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 4.18.0-80.7.2.el7.aarch64
      Architecture: arm64
(2)配置hosts文件

两个节点配置/etc/hosts文件,在文件的下方添加如下两条语句:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
192.168.0.18  mysql1
192.168.0.155 mysql2
(3)配置YUM源

使用提供的LNMP-ARM64.tar到两台主机(使用命令curl -O ftp://192.168.0.196/primary/project3/LNMP-ARM64.tar下载到本地),都解压到当前目录,命令如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# tar -zxvf LNMP-ARM64.tar.gz

然后配置两个节点的YUM源文件,首先将原有的repo文件移除,命令如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# mv /etc/yum.repo.d/* /media

创建并编辑两个节点的repo文件如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# vi /etc/yum.repos.d/local.repo
//创建本地repo文件
# cat /etc/yum.repos.d/local.repo
//文件内容如下所示
[lnmp]
name=lnmp
baseurl=file:///root/LNMP-ARM64
gpgcheck=0
enabled=1

两个节点执行如下命令,刷新YUM源。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# yum clean all
# yum repolist
(4)安装数据库服务

两个节点安装数据库服务,命令如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# yum install -y mariadb mariadb-server

两个节点启动数据库服务并设置开机自启,命令如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 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,命令如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@mysql1 ~]# mysql_secure_installation 
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

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:  #输入数据库root密码000000
Re-enter new password:  #再次输入密码000000
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]增添如下内容。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@mysql1 ~]# cat /etc/my.cnf
[mysqld]
log_bin = mysql-bin        #记录操作日志
binlog_ignore_db = mysql   #不同步mysql系统数据库
server_id = 30             #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.30,server_id就写30
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[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 3
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,并赋予从节点同步主节点数据库的权限。命令如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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中的[mysqld]增添如下内容。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@mysql2 ~]# cat /etc/my.cnf
[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步mysql系统数据库
server_id = 40                            #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.40,server_id就写40
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[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 3
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.01 sec)

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
MariaDB [(none)]> start slave;
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: 530
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 814
        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: 530
              Relay_Log_Space: 1110
              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,配置数据库主从集群成功。


3. 验证数据库主从服务
(1)主节点创建数据库

先在主节点mysql1中创建库test,并在库test中创建表company,插入表数据,创建完成后,查看表company数据,命令如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@mysql1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
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.01 sec)

MariaDB [test]> insert into company values(1,"alibaba","china");
Query OK, 1 row affected (0.01 sec)

MariaDB [test]> select * from company;
+----+---------+-------+
| id | name    | addr  |
+----+---------+-------+
|  1 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)
(2)从节点验证复制功能

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@mysql2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
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)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
配置两台数据库为主从数据库模式(master和slave)
使用VMWare创建两台centos7系统的虚拟机,安装数据库服务,并将两台数据库配置为主从数据库模式(master和slave)。配置完成后,在从节点,执行show status slave\G查看从节点的复制状态。将查看从节点服务状态的返回结果以文本形式提交到答题框。(数据库用户名root,密码000000;关于数据库的命令均使用小写)
kenvie
2021/12/30
1.2K0
MySql 5.7.18 数据库主从(Master/Slave)同步安装与配置详解
MySql复制的优点: 1.如果主服务器出现问题,可以快速切换到从服务器提供的服务 2.可以在从服务器上执行查询操作,降低主服务器的访问压力 3.可以在从服务器上执行备份,以避免备份期间影响主服务器的服务
Javen
2018/08/21
4520
MySql 5.7.18 数据库主从(Master/Slave)同步安装与配置详解
数据库主从分离加读写分离操作步骤
数据库主从分离加读写分离操作步骤 数据库主从分离服务 1、基础环境安装 (1) 修改主机名【mysql1、mysql2】 [root@mysql1 ~]# hostnamectl set-hostname mysql1 [root@mysql1 ~]# su [root@mysql1 ~]# hostnamectl Static hostname: mysql1 Icon name: computer-vm Chassis: vm Machi
宝耶需努力
2022/12/13
1.4K0
MySQL数据库主从同步,你get到了吗?
数据库的主从复制想必大家不是很陌生了,而且我看也有文章介绍讲解。不要傻傻的认为主从复制就是主从同步。今天来学习数据库主从同步的原理及过程,数据库主要是用来存储WEB数据,在企业当中是极为重要的,下面一起来看下。
北京-宏哥
2023/09/28
4.9K0
MySQL数据库主从同步,你get到了吗?
Mysql主从复制部署
  采用Vagrant搭建mysql集群,这里配置三台mysql服务器,操作系统为Centos7
高木工
2019/05/06
8530
Mysql主从复制部署
企业实战(9)Mysql数据库实现主从同步,看这一篇就够了!
当master服务器上的数据发生改变时(增、删、改),则将其改变写入二进制binlog日志中;slave服务器会在一定时间间隔内对master二进制日志进行探测其是否发生改变,如果发生改变,则开启一个I/O 线程请求master二进制事件,同时主节点为每个I/O线程启动一个dump线程,用于向其发送二进制事件,并保存至从库本地的中继日志中,从库(从节点)将启动SQL线程从中继日志中读取二进制日志,在本地重放,使得其数据和主节点的保持一致,最后IO线程和SQL线程将进入睡眠状态,等待下一次被唤醒。
非著名运维
2022/06/22
1.5K0
企业实战(9)Mysql数据库实现主从同步,看这一篇就够了!
mysql主从简单配置
环境 hostnameip端口master128.0.0.1013306slave128.0.0.1023306 安装mysql [root@master ~]# yum -y install mysql mysql-server mysql-devel [root@slave ~]# yum -y install mysql mysql-server mysql-devel 启动mysql [root@master ~]# service mysqld start Initializing MySQL d
零月
2018/04/25
8590
mysql主从简单配置
mycat实现读写分离_mycat主从复制
rpm -ivh mysql-community-release-el6-5.noarch.rpm
全栈程序员站长
2022/11/08
6900
mycat实现读写分离_mycat主从复制
Mysql主从同步架构配置
一、mysql主从介绍: MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的。也就是说,当你在A机器写入一个表,再次查看B机器也会同步一个表。 1.1 MySQL主从是基于binlog的,主上须开启binlog才能进行主从。 主从过程大致有3个步骤: 主将更改操作记录到binlog里。 从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里。 从根据relaylog里面的sql语句按顺序执
老七Linux
2018/05/09
3.7K0
17.4 配置从
主从配置 - 从上操作 安装mysql 查看my.cnf,配置server-id=132,要求和主不一样 修改完配置文件后,启动或者重启mysqld服务 把主上aming库同步到从上 可以先创建aming库,然后把主上的/tmp/mysql.sql拷贝到从上,然后导入aming库 mysql -uroot stop slave; change master to master_host='', master_user='repl', master_password='', master_log_file='
运维小白
2018/02/07
6760
Mysql的主从复制
答:主从同步的核心是二进制日志文件binary log,对数据库所有的增加、修改、删除操作都会在日志表里面记录一下的。mysql主从复制是异步的,串行化的,有延迟的,并不是实时的。
别先生
2020/09/01
1.3K0
Mysql的主从复制
centos 下 mysql+keepalived实现双主自由切换
本文的目的是搭建一个互为主从的mysql高可用架构,用来保证mysql服务器宕机的时候,能够自动的切换的另一台mysql服务器。
庞小明
2018/09/19
6440
centos 下 mysql+keepalived实现双主自由切换
MySQL数据库实现主从复制
我们用的在这篇文章《在CentOS上使用Nginx和Tomcat搭建高可用高并发网站》使用的只有一个MySQL数据库。
夜雨飘零
2020/05/06
2.7K0
Mysql/Mairadb主从复制
4、修改配置文件 编辑配置文件 /etc/my.cnf,添加内容 vi /etc/my.cnf
指剑
2022/07/15
2310
Mysql/Mairadb主从复制
MySQL主从复制读写分离与高可用配置
前面我们说了MySQL的安装配置(并提供一键安装脚本),MySQL语句使用以及备份恢复MySQL数据;本次要介绍的是MySQL的主从复制,读写分离;及高可用MHA。
星哥玩云
2022/08/16
4900
MySQL主从复制读写分离与高可用配置
Linux基础(day61)
17.1 MySQL主从介绍 MySQL主从介绍 ---- MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的 MySQL主从是基于binlog的,主上须开启binlog才能进行主从。 binlog,其实就是一个文件,文件里记录了一些日志,文件是 二进制文件,无法cat 主从过程大致有3个步骤 1)主将更改操作记录到binlog里 2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylo
运维小白
2018/02/07
6440
Linux基础(day61)
mysql双主搭建
之前summer部署过主从,这里记录下部署双主的方法,坑有蛮多但总体还算顺利。MySQL版本为8.0.19。
summerking
2022/09/16
4K0
基于腾讯云CVM构建MySQL MHA Keepalived集群
MHA是由perl语言编写的一款开源的MySQL的高可用程序,为MySQL主从复制架构提供了automating master failover功能。MHA可以自动检测mysql是否宕机,如果宕机,在10-30s内完成new master的选举,应用所有差异的binlog日志到所有slave,将所有的slave切换到新的master上来。
Vicwan
2019/04/06
9.4K0
基于腾讯云CVM构建MySQL MHA Keepalived集群
mysql主从配置与数据移植
配置mysql的主从模式,当一台mysql出现问题后,另外一台可以正常工作。 两台机器IP:192.168.1.60(master)、192.168.1.61(slave) 注意:在配置之前,确保两台节点都有需同步的数据库。 1、master节点配置 1.1、修改配置文件 192.168.1.60:master节点 vim /etc/my.cnf 添加如下内容: [mysqld] log-error=/data/mysql_log/error.log # mysql的error日志 log=
YG
2018/05/23
6370
MySQL主从复制与相关问题解决方法
所有的关系型数据库都存在一个通病性能差,在企业中如果用户量特别打,将所有的数据都存放在一台服务器上,其性能时远远达不到要求的。所以需要使用一些手段来解决其性能的问题。 提升性能的方式有向上扩展以及向外扩展 向上扩展(Scale Up):使用更新更好的硬件,但硬件在怎么更新也有其性能的极限。盲目的向上扩展无法结局根本的问题 向外扩展(Scale Out):就是使用多台机器分摊压力来提供服务
星哥玩云
2022/08/18
8820
相关推荐
配置两台数据库为主从数据库模式(master和slave)
更多 >
LV.0
这个人很懒,什么都没有留下~
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验