centos7 yum下mariadb10的安装

mariadb的官网地址
https://mariadb.com/
centos6的操作系统模式mysql,centos7以后默认的mariadb了,两个是软件是分支版本的,基本会一个另外一个就会了,很通用。
在这里插入图片描述

CentOS 6 或早期的版本中提供的是 MySQL 的服务器/客户端安装包,但 CentOS 7 已使用了 MariaDB 替代了默认的 MySQL。MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

全部删除MySQL/MariaDB
MySQL 已经不再包含在 CentOS 7 的源中,而改用了 MariaDB;
1.使用rpm -qa | grep mariadb搜索 MariaDB 现有的包:
在这里插入图片描述
显示原有系统自带有mariadb的包

2.使用yum remove mariadb-libs移除现有的包:
在这里插入图片描述
3.删除完成后再次查看,显示已经没有
在这里插入图片描述

4 开始安装新的yum仓库的rpm文件
第一步:
添加 MariaDB yum 仓库
首先在CentOS操作系统中/etc/yum.repos.d/目录下添加 MariaDB 的YUM配置文件MariaDB.repo文件。

vi /etc/yum.repos.d/MariaDB.repo
在该文件中添加以下内容保存:
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

如果想定制自己的版本,可以去官网查看,查看地址为
系统及版本选择:https://downloads.mariadb.org/mariadb/repositories/#mirror=tuna
选择相应的版本或者系统
在这里插入图片描述
DO NOT USE RC RELEASES IN PRODUCTION
Here is your custom MariaDB DNF/YUM repository entry for RedHatEnterpriseLinux. Copy and paste it into a file under /etc/yum.repos.d (we suggest naming the file MariaDB.repo or something similar).

MariaDB 11.8 RedHatEnterpriseLinux repository list - created 2025-03-14 12:21 UTC

https://mariadb.org/download/

[mariadb]
name = MariaDB

rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.

baseurl = https://rpm.mariadb.org/11.rc/rhel/ r e l e a s e v e r / releasever/ releasever/basearch

baseurl = https://mirrors.xtom.com/mariadb/yum/11.8/rhel/ r e l e a s e v e r / releasever/ releasever/basearch
module_hotfixes = 1

gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB

gpgkey = https://mirrors.xtom.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
EPEL repository may be required to satisfy the pv dependency of galera.

After the file is in place, install and start MariaDB with:

sudo yum install MariaDB-server MariaDB-client
If you haven’t already accepted the MariaDB GPG key, you will be prompted to do so during the install. See Installing MariaDB with yum for detailed information. Note the GPG was changed in January 2023 with more secure key. For more details see our blog article New GPG Release Key for RPMs.

第二步:安装 MariaDB
通过yum命令轻松安装 MariaDB。
yum install MariaDB-server MariaDB-client -y
MariaDB 安装完毕后,立即启动数据库服务守护进程。
systemctl start mariadb
设置 MariaDB 在操作系统重启后自动启动服务。
systemctl enable mariadb
查看 MariaDB 服务当前状态。
systemctl status mariadb

第三步:对 MariaDB 进行安全配置
通过以下命令进行安全配置,根据实际情况用Y/N回复以下问题:设置 MariaDB 的 root 账户密码,删除匿名用户,禁用 root 远程登录,删除测试数据库,重新加载权限表。
mysql_secure_installation
本人全都是选择了Y,然后按回车。

2.配置MariaDB

1mysql_secure_installation
2.1 Enter current password for root (enter for none):   //输入当前密码,初次安装没有密码,直接回车
2.2 Switch to unix_socket authentication [Y/n] n     //询问是否使用’unix_socket’进行身份验证:n
2.3 Change the root password? [Y/n] y         //为root设置密码:y
New password:                   //输入密码
Re-enter new password:               //再次输入密码
2.4 Remove anonymous users? [Y/n] y        //是否移除匿名用户,这个随意,建议删除:y
2.5 Disallow root login remotely? [Y/n] n        //拒绝用户远程登录,这个建议开启:n
2.6 Remove test database and access to it? [Y/n] n   //删除test库,可以保留:n
2.7 Reload privilege tables now? [Y/n] y        //重新加载权限表:y
在配置完数据库的安全配置后,可以通过
以下命令查看版本,确认 MariaDB已安装成功。
mysql --version
可以通过 MariaDB 命令行登录,然后对数据库进行sql查询操作。
mysql -u root -p

第四步:为 MariaDB 配置远程访问权限
在第三步中如果禁用 root 远程登录选择 Y 的话就不能在别的电脑通过navicat等工具连接到数据库,这时就需要给对应的 MariaDB 账户分配权限,允许使用该账户远程连接到MariaDB。可以输入以下命令查看账号信息:
select User, host from mysql.user;
root账户中的host项是localhost表示该账号只能进行本地登录,我们需要修改权限,输入命令:
GRANT ALL PRIVILEGES ON . TO ‘root’@‘%’ IDENTIFIED BY ‘password’ WITH GRANT OPTION;
修改权限。%表示针对所有IP,password表示将用这个密码登录root用户,如果想只让某个IP段的主机连接,可以修改为:
GRANT ALL PRIVILEGES ON . TO ‘root’@‘192.168.71.%’ IDENTIFIED BY ‘my-new-password’ WITH GRANT OPTION;
最后别忘了:
FLUSH PRIVILEGES;
保存更改后,再看看用户账号信息:
这个时候发现相比之前多了一项,它的host项是%,这个时候说明配置成功了,我们可以用该账号进行远程访问了。

第五步:CentOS 7 开放防火墙端口
在第四步后如果还是不能远程连上数据库的话应该就是3306端口被防火墙拦截了,这时我们就需要关闭防火墙或者开放防火墙端口。
关闭防火墙:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
开放防火墙端口,开启后要重启防火墙:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

第六步:设置数据库字母大小写不敏感
vi /etc/my.cnf.d/server.cnf
在[mysqld]下加上】
lower_case_table_names=1
默认是等于0的,即大小写敏感。改成1就OK了。如果之前已经建了数据库要把之前建立的数据库删除,重建才生效。

第七步:设置MariaDB数据库默认编码
MariaDB的默认编码是latin1,插入中文会乱码,因此需要将编码改为utf8。
1.登录,使用以下命令查看当前使用的字符集,应该有好几个不是utf8格式。
SHOW VARIABLES LIKE ‘character%’;
2.修改的配置文件
vi /etc/my.cnf.d/client.cnf
在[client]字段里加入
default-character-set=utf8
vi /etc/my.cnf.d/server.cnf
在[mysqld]字段里加入
character-set-server=utf8
3.重启 MariaDB 配置生效。
systemctl restart mariadb

### 编写 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、付费专栏及课程。

余额充值