【主从数据库?】centos7,快速配置Mariadb主从

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: 【主从数据库?】centos7,快速配置Mariadb主从

节点规划

IP 主机名 节点
100.111.8.88 db1 MariaDB数据库集群主节点
100.111.8.131 db2 MariaDB数据库集群从节点

基础环境配置

修改主机名:

[root@test-1 ~]# hostnamectl set-hostname db1  #修改主机名
[root@test-1 ~]# bash  #刷新
[root@db1 ~]#
[root@test-2 ]#  hostnamectl set-hostname db2
[root@test-2 ]# bash
[root@db2 ]#

关闭防火墙、selinux

db1、db2两节点:

[root@db1 ~]# systemctl stop firewalld  #关闭防火墙
[root@db1 ~]# setenforce 0  #临时关闭selinux,永久关闭selinux修改配置文件/etc/selinux/config
[root@db2 ~]# systemctl stop firewalld
[root@db2 ~]# setenforce 0

配置hosts

db1、db2两节点:配置一样

[root@db1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.43.8.88 db1
10.43.8.131 db2

配置yum源

db1、db2两节点:配置一样

[root@db1 ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=http://10.120.11.15/centos/
gpgcheck=0
enabled=1

安装mariadb数据库并启动

db1、db2两节点

[root@db1 ~]# yum install -y mariadb mariadb-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.56-2.el7 will be installed
---> Package mariadb-server.x86_64 1:5.5.56-2.el7 will be installed
--> Processing Dependency: perl-DBI for package: 1:mariadb-server-5.5.56-2.el7.x86_64
....
....
[root@db1 ~]# systemctl start mariadb
[root@db1 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

初始化数据库

db1、db2两节点

一直按 y 就行

[root@db1 ~]# mysql_secure_installation   #初始化数据库
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
....
....
Set root password? [Y/n] y   
New password: #输入数据库root密码
Re-enter new password:  #重复输入密码
Password updated successfully!
Reloading privilege tables..
 ... Success!
....
....
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] y  #是否开启远程登陆数据库
 ... Success!
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!
....
....
Reload privilege tables now? [Y/n] y  #是否重新加载特权表
 ... Success!

修改配置文件并重启

db1、db2两节点

[root@db1 ~]# cat /etc/my.cnf.d/server.cnf
... ...
[mysqld]
log_bin = mysql-bin            #记录操作日志
binlog_ignore_db = mysql         #不同步mysql系统数据库     
server_id = 88              #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.30.11.12,server_id就写88
... ...
[root@db1 ~]#  systemctl restart mariadb
[root@db2 ~]# cat /etc/my.cnf.d/server.cnf
... ...
[mysqld]
log_bin = mysql-bin            
binlog_ignore_db = mysql        
server_id = 131           
... ...
[root@db2 ~]#  systemctl restart mariadb

数据库授权

db1:

[root@db1 ~]# mysql -uroot -p   #root用户登录数据库
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)]>
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"@'db2' identified by "000000";   #赋予从节点同步主节点数据库的权限
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> Ctrl-C -- exit!
Aborted
[root@db1 ~]#

db2:

[root@db2 ~]# 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)]>
MariaDB [(none)]> change master to master_host='db1',master_user='user',master_password='000000';   #配置从节点连接主节点
Query OK, 0 rows affected (0.14 sec)
MariaDB [(none)]> start slave;   #开启从节点服务
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G  #检查Slave_IO_Running、Slave_SQL_Running字段是否为Yes,如果是那么主从数据库搭建成功
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: db1
                  Master_User: user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 526
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 810
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes   #检查字段是否为Yes
            Slave_SQL_Running: Yes   #检查字段是否为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: 526
              Relay_Log_Space: 1106
              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: 88
1 row in set (0.00 sec)
MariaDB [(none)]>

至此mariadb主从数据库搭建完毕!!!!

总结

  • 配置环境:修改主机名
    添加hosts映射
    配置yum源
    关闭防火墙
    selinux
  • 安装mariadb:启动mariadb
    初始化mariadb
    修改配置文件/etc/my.cnf.d/server.cnf
    重启mariadb
  • 登录授权:开启root用户远程访问所有数据库
    开启赋予从节点同步主节点数据库的权限
    配置从节点连接主节点
    开启从节点服务
    检查Slave_IO_Running、Slave_SQL_Running字段状态
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
网络协议 关系型数据库 MySQL
如何搭建MariaDB并实现无公网ip环境远程连接本地数据库
如何搭建MariaDB并实现无公网ip环境远程连接本地数据库
93 0
|
16天前
|
Oracle 关系型数据库 MySQL
centos6安装mariadb失败及解决办法
centos6安装mariadb失败及解决办法
37 4
|
25天前
|
Prometheus 监控 关系型数据库
数据库同步革命:MySQL GTID模式下主从配置的全面解析
数据库同步革命:MySQL GTID模式下主从配置的全面解析
91 0
|
1月前
|
关系型数据库 MySQL Linux
服务器Linux系统配置mysql数据库主从自动备份
这是一个基本的配置主从复制和设置自动备份的指南。具体的配置细节和命令可能因您的环境和需求而有所不同,因此建议在操作前详细阅读MySQL文档和相关资源,并谨慎操作以避免数据丢失或不一致。
65 3
|
1月前
|
运维 负载均衡 监控
软件体系结构 - 关系数据库(3)主从架构
【4月更文挑战第26天】软件体系结构 - 关系数据库(3)主从架构
41 0
|
1月前
|
关系型数据库 MySQL Linux
Centos7安装mariadb数据库
该文介绍了如何在CentOS7上安装MariaDB数据库。MariaDB是MySQL的开源分支,旨在保持与MySQL的兼容性。安装过程包括配置yum源,执行安装命令,启动数据库服务,并设置开机启动。首次启动时,数据库未设置密码,用户可直接回车。
155 0
|
1月前
|
网络协议 关系型数据库 MySQL
如何实现在公网下使用navicat图形化工具远程连接本地内网的MariaDB数据库
如何实现在公网下使用navicat图形化工具远程连接本地内网的MariaDB数据库
|
1月前
|
网络协议 关系型数据库 MySQL
安卓手机termux上安装MariaDB数据库并实现公网环境下的远程连接
安卓手机termux上安装MariaDB数据库并实现公网环境下的远程连接
|
1月前
|
网络协议 关系型数据库 MySQL
通过内网穿透本地MariaDB数据库,实现在公网环境下使用navicat图形化工具
本篇教程将使用cpolar内网穿透本地MariaDB数据库,并实现在外公网环境下使用navicat图形化工具远程连接本地内网的MariaDB数据库。
|
8月前
|
NoSQL 关系型数据库 MySQL
阿里云RDS关系型数据库大全_MySQL版、PolarDB、PostgreSQL、SQL Server和MariaDB等
阿里云RDS关系型数据库如MySQL版、PolarDB、PostgreSQL、SQL Server和MariaDB等,NoSQL数据库如Redis、Tair、Lindorm和MongoDB
279 0

热门文章

最新文章