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

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS AI 助手,专业版
简介: 【主从数据库?】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字段状态
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
9月前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
870 87
|
5月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
979 152
|
5月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
801 156
|
5月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
7月前
|
存储 Linux Apache
在CentOS上配置SVN至Web目录的自动同步
通过上述配置,每次当SVN仓库中提交新的更改时,`post-commit`钩子将被触发,SVN仓库的内容会自动同步到指定的Web目录,从而实现代码的连续部署。
212 16
|
7月前
|
NoSQL 安全 Linux
设置Redis在CentOS7上的自启动配置
这些步骤总结了在CentOS 7系统上设置Redis服务自启动的过程。这些命令提供了一个直接且明了的方式,确保Redis作为关键组件在系统启动时能自动运行,保障了依赖于Redis服务的应用的稳定性和可用性。
570 9
|
9月前
|
Linux
Centos6配置阿里云yum源报错
在CentOS 6配置阿里云Yum源时,可能出现EPEL仓库访问报错(404 Not Found)。解决方法:编辑`/etc/yum.repos.d/epel.repo`文件,将`enabled`和`gpgcheck`参数设为0 ``` 此设置可解决仓库无法访问的问题。
1938 29
|
9月前
|
Ubuntu 安全 Linux
CentOS与Ubuntu中防火墙配置命令集汇
有了这些,你就能遨游在 CentOS 和 Ubuntu 的海洋中,频繁地改变你的防火墙设置,快速地应对各种安全威胁,同时也能保证你的系统可以正常工作。出发吧,勇敢的编程者,随着这些命令集的涌动,扬帆起航,走向安全的网络世界!
306 5
|
10月前
|
关系型数据库 MySQL Linux
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
2504 26