当前位置:首页 > 技术 > 正文内容

在CentOS 7上通过RPM安装MySQL 5.7

访客 技术 2026年7月9日 1

首先,访问MySQL官方网站下载所需版本的RPM包。

例如,MySQL 5.7的RPM包可以从这里获取:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar

下载完成后将其上传到CentOS系统中。

[root@server ~]# mkdir mysql_dir //创建一个专门的MySQL目录
[root@server ~]# tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar -C mysql_dir/ //解压文件到mysql_dir目录
[root@server ~]# yum install -y make gcc-c++ cmake bison-devel ncurses-devel libaio libaio-devel net-tools //安装必要的依赖包

由于CentOS 7默认安装了MariaDB,因此需要先卸载系统中的MariaDB组件才能安装MySQL。

[root@server ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@server ~]# yum remove -y mariadb-libs

现在可以开始安装MySQL。因为存在依赖关系,所以安装顺序是固定的。

[root@server ~]# rpm -ivh mysql_dir/mysql-community-common-5.7.26-1.el7.x86_64.rpm
[root@server ~]# rpm -ivh mysql_dir/mysql-community-libs-5.7.26-1.el7.x86_64.rpm
[root@server ~]# rpm -ivh mysql_dir/mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm
[root@server ~]# rpm -ivh mysql_dir/mysql-community-client-5.7.26-1.el7.x86_64.rpm
[root@server ~]# rpm -ivh mysql_dir/mysql-community-server-5.7.26-1.el7.x86_64.rpm //完成MySQL服务器的安装

下一步,启动MySQL服务并设置开机自启。

[root@server ~]# systemctl start mysqld
[root@server ~]# systemctl enable mysqld
[root@server ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2023-09-01 10:00:00 CST; 1min 30s ago
 Main PID: 12345 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─12345 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

9月 01 10:00:00 server systemd[1]: Starting MySQL Server...
9月 01 10:00:00 server systemd[1]: Started MySQL Server.

第四步,获取MySQL临时密码,并为root用户设置新密码。

[root@server ~]# grep "temporary password" /var/log/mysqld.log //查找日志中的临时密码
2023-09-01T02:00:00.123456Z 1 [Note] A temporary password is generated for root@localhost: abc!@#123
[root@server ~]# mysql -uroot -p"abc!@#123"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
//尝试使用简单密码会失败
mysql> alter user 'root'@'localhost' identified by 'simple';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user 'root'@'localhost' identified by 'ComplexPass123!';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye

除了使用"alter user 'root'@'localhost' identified by 'ComplexPass123!';",还可以用"set password for root@localhost = password('ComplexPass123!');"。

最后一步,测试连接。

如果密码包含特殊字符,必须用引号包裹。

[root@server ~]# mysql -u root -p'ComplexPass123!'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

扩展说明:

若想设置简单的密码,可以通过以下两种方法之一来实现:修改MySQL命令或直接编辑配置文件。

使用MySQL命令的方法如下:

[root@server ~]# mysql -uroot -p'ComplexPass123!'
mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.02 sec)
mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)
mysql> set password for root@localhost=password('easy');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye

直接修改配置文件的方法:

[root@server ~]# vi /etc/my.cnf
# 在[mysqld]部分添加validate-password=OFF以禁用密码验证插件
[root@server ~]# systemctl restart mysqld
[root@server ~]# mysql -uroot -peasy
mysql> set password for root@localhost=password('veryeasy');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
标签: MySQLRPM

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。