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

本文涉及的产品
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
简介: 【主从数据库?】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字段状态
相关实践学习
自建数据库迁移到云数据库
本场景将引导您将网站的自建数据库平滑迁移至云数据库RDS。通过使用RDS,您可以获得稳定、可靠和安全的企业级数据库服务,可以更加专注于发展核心业务,无需过多担心数据库的管理和维护。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
7月前
|
Oracle 关系型数据库 Linux
【赵渝强老师】Oracle数据库配置助手:DBCA
Oracle数据库配置助手(DBCA)是用于创建和配置Oracle数据库的工具,支持图形界面和静默执行模式。本文介绍了使用DBCA在Linux环境下创建数据库的完整步骤,包括选择数据库操作类型、配置存储与网络选项、设置管理密码等,并提供了界面截图与视频讲解,帮助用户快速掌握数据库创建流程。
588 93
|
7月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1204 152
|
7月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
902 156
|
10月前
|
关系型数据库 MySQL 数据库连接
Django数据库配置避坑指南:从初始化到生产环境的实战优化
本文介绍了Django数据库配置与初始化实战,涵盖MySQL等主流数据库的配置方法及常见问题处理。内容包括数据库连接设置、驱动安装、配置检查、数据表生成、初始数据导入导出,并提供真实项目部署场景的操作步骤与示例代码,适用于开发、测试及生产环境搭建。
456 1
|
关系型数据库 MySQL Java
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
|
数据库
【YashanDB知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
【YashanDB知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
【YashanDB知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
|
7月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
7月前
|
Ubuntu 安全 关系型数据库
安装与配置MySQL 8 on Ubuntu,包括权限授予、数据库备份及远程连接指南
以上步骤提供了在Ubuntu上从头开始设置、配置、授权、备份及恢复一个基础但完整的MySQL环境所需知识点。
759 7
|
7月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
1226 5
|
12月前
|
安全 Linux 网络安全
YashanDB数据库服务端SSL连接配置
YashanDB支持通过SSL连接确保数据传输安全,需在服务端生成根证书、服务器证书及DH文件,并将根证书提供给客户端以完成身份验证。服务端配置包括使用OpenSSL工具生成证书、设置SSL参数并重启数据库;客户端则需下载根证书并正确配置环境变量与`yasc_env.ini`文件。注意:启用SSL后,所有客户端必须持有根证书才能连接,且SSL与密码认证独立运行。