CentOS 7 yum 安装 MariaDB

本文详细介绍如何在CentOS 7环境下自定义安装MariaDB 10.4版本,包括创建MariaDB.repo文件、使用yum安装、配置启动、初始化设置、修改配置及远程登录等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境

> cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

前言

原本寄希望于直接使用yum,安装MariaDByum search mariadb 也可以搜索到,可直接安装的版本比较低(使用的阿里的镜像):

Server version: 5.5.64-MariaDB MariaDB Server

翻阅官方说明,可以自定义yumMariaDB.repo安装。

1. 自定义MariaDB.repo

MariaDB官网提供了创建一个MariaDB.repo的工具页:

  1. 打开以下网址:
    https://downloads.mariadb.org/mariadb/repositories/

  2. 选择自己的服务器的版本和想要安装的版本: image

  3. 页面下会生成一个MariaDB.repo文件内容,如下:

# MariaDB 10.4 CentOS repository list - created 2019-11-19 02:02 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
  1. 按照官方提示,在/etc/yum.repos.d/目录下,创建MariaDB.repo文件。
  2. 官网的下载地址在国内访问比较慢,可以修改为国内镜像:
# MariaDB 10.4 CentOS repository list - created 2019-11-19 02:02 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://mirrors.ustc.edu.cn/mariadb/yum/10.4/centos7-amd64
gpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

2. yum 安装mariaDB

  • 安装
    • 客户端 mariadb-client
    • 服务端 mariadb-server
yum install mariadb-client mariadb-server
  • 如果需要开发环境(如C\C++等),安装开发环境
yum install mariadb-devel

2. 配置启动项

# 启动mariadb
systemctl start mariadb

# 设置为开机启动
systemctl enable mariadb

3. 初始化mariaDB

  • 输入命令,启动配置首次安装选项:
mysql_secure_installation
  • 根据提示配置mariaDB
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.

# 是否设置root的密码,设置一个密码
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.

# 是否禁止root远程登录
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.

# 是否移除默认安装时的测试DB
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.

# 是否重新加载权限表。即以上改变是否立即生效,重启mariaDB也可以
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!

4. 修改配置

4.1 配置 /etc/my.cnf

  • 根据需要增加以下配置:
[mysqld]

# 表名一律小写,即新建表和查询SQL时,表名转换为小写。
# 相当于表名忽略大小写,但如果之前大写创建的,该方式并不能解决SQL的表大小写问题。
lower_case_table_name = 1

# 允许上传的最大数据包大小
max_allowed_packet = 10M

# 跳过DNS反向解析
skip-name-resolve

# 开启慢查询日志
slow-query-log = 1
slow-query-log-file = /var/log/mysql/mysql-slow.log
# 超过多少秒的SQL记录为慢查询
long_query_time = 1

# 修改字符集为utf8mb4,主要为解决Emoji
init-connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
  • 修改后重启生效
systemctl restart mariadb

4.2 修改远程登录

如果需要远程登录的,需要修改防火墙和数据库权限表。

修改防火墙

1. 查看端口开放情况

firewall-cmd --query-port=3306/tcp

2. 开放端口

根据情况,选择合适的防火墙策略

  • 方式一: 开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
  • 方式二: 设置白名单
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="3306" accept"

3. 使防火墙设置生效

firewall-cmd --reload

4. 查看防火墙规则

firewall-cmd --list-all 

修改权限表

1. 本机通过命令登录mysql,输入密码登录

mysql -u root -p

2. 修改mysql数据库的user host

  • 切换到mysql数据库下
MariaDB [(none)]> use mysql;
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
  • 时间看user表
MariaDB [mysql]> select host,user from user;
+-----------------------------------------+------+
| host                                    | user |
+-----------------------------------------+------+
| 127.0.0.1                               | root |
| ::1                                     | root |
| localhost                               | root |
| vm-85972bf5-6119-48e8-b7ee-95f6d88312c2 | root |
+-----------------------------------------+------+
4 rows in set (0.00 sec)
  • 将host本机名的项改为%,任意IP连接
MariaDB [mysql]> update user set host='%' where host='vm-85972bf5-6119-48e8-b7ee-95f6d88312c2';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
  • 刷新权限退出
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> quit;
Bye
  • 以上,通过远程连接,测试是否成功。
### 编写 Shell 脚本以在 CentOS 7 上通过 Yum 安装 MariaDB 为了实现自动化部署,可以创建一个名为 `install_mariadb.sh` 的 Bash 脚本来完成此操作。该脚本会执行必要的预处理工作并调用 yum安装 MariaDB。 #### 关闭 SELinux 和防火墙设置 考虑到安全性和网络访问的需求,建议先调整 SELinux 设置以及清理 iptables 规则以便允许数据库服务正常运行: ```bash #!/bin/bash # 备份原始配置文件以防恢复 cp /etc/selinux/config /etc/selinux/config.bak # 修改SELinux状态为禁用 sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config # 清除现有iptables规则 iptables -F service iptables save ``` 上述命令修改了 `/etc/selinux/config` 文件中的 SELinux 配置选项,并清除了所有的 IP 表规则[^3]。 #### 使用 Yum 安装 MariaDB 及其开发工具包 接下来的部分负责实际的软件包安装过程,这里不仅限于服务器组件还包括客户端和其他可能需要用到的支持库: ```bash # 更新系统仓库缓存 yum makecache fast # 安装MariaDB服务器及相关依赖项 yum install -y mariadb-server mariadb-devel # 启动MariaDB服务并将它设为开机自启 systemctl start mariadb.service systemctl enable mariadb.service echo "MariaDB 已成功安装" ``` 这段代码片段利用 yum 命令来获取最新的元数据信息,接着指定要安装的具体软件包名称列表,最后启动新安装的服务程序并且将其加入到系统的初始化进程中去[^1][^2]。 完整的 shell 脚本如下所示: ```bash #!/bin/bash # Backup original config file before making changes cp /etc/selinux/config /etc/selinux/config.bak # Disable SELinux by modifying its configuration file sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config # Clear existing iptables rules to allow database service operation iptables -F service iptables save # Update system repository cache yum makecache fast # Install MariaDB server along with development packages yum install -y mariadb-server mariadb-devel # Start MariaDB service and set it up as a boot-starting service systemctl start mariadb.service systemctl enable mariadb.service echo "MariaDB has been successfully installed." ``` 保存以上内容至 `.sh` 文件后赋予可执行权限即可随时用来快速搭建环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值