【MySQL】01.MySQL环境安装

注意:在MYSQL的安装与卸载中,需要使用root用户进行。

一、卸载不必要的环境

查看是否有运行的服务

bash 复制代码
[root@VM-24-10-centos etc]# ps axj |grep mysql
    1 22030 22029 22029 ?           -1 Sl      27   0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
 3096 22531 22530  3096 pts/0    22530 R+       0   0:00 grep --color=auto mysql
[root@VM-24-10-centos etc]# ps axj |grep maridb
 3096 22672 22671  3096 pts/0    22671 S+       0   0:00 grep --color=auto maridb

我们可以看到当前的机器上是存在着mysql的,如果没有则可直接跳过这步操作,下面的操作对应maridb也是一样的。

关闭运行的服务

bash 复制代码
[root@VM-24-10-centos etc]# systemctl stop mysqld
[root@VM-24-10-centos etc]# ps axj |grep mysql
 3096 23210 23209  3096 pts/0    23209 S+       0   0:00 grep --color=auto mysql

查找服务器上的mysql安装包

bash 复制代码
[root@VM-24-10-centos etc]# rpm -qa|grep mysql
mysql-community-client-5.7.44-1.el7.x86_64
mysql-community-server-5.7.44-1.el7.x86_64
mysql-community-common-5.7.44-1.el7.x86_64
mysql-community-libs-compat-5.7.44-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-libs-5.7.44-1.el7.x86_64

卸载全部的安装包

bash 复制代码
[root@VM-24-10-centos etc]# rpm -qa|grep mysql|xargs yum -y remove
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
......
......
Complete!

再次确认

bash 复制代码
[root@VM-24-10-centos etc]# ls /etc/my.cnf
ls: cannot access /etc/my.cnf: No such file or directory
[root@VM-24-10-centos etc]# rpm -qa|grep mysql
[root@VM-24-10-centos etc]# 

这就说明我们卸载干净了。

二、安装MySQL

获取mysql官方的yum源

这是网址:http://repo.mysql.com/ ,对于下载的yum源我们需要与当前系统相结合下载。

bash 复制代码
[root@VM-24-10-centos etc]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

因此我们最好能下载CentOS7.6版本的,但是我们查看后发现并没有这一版本,因此我们下载的是http://repo.mysql.com/mysql57-community-release-el7.rpm

我们将其下载到windows端,然后通过rzsz小工具传输到Linux端。

bash 复制代码
[root@VM-24-10-centos ~]# rz

[root@VM-24-10-centos ~]# ll
total 1
-rw-r--r-- 1 root   root   25680 Dec 28 16:53 mysql57-community-release-el7.rpm

安装mysql源

bash 复制代码
[root@VM-24-10-centos ~]# rpm -ivh mysql57-community-release-el7.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-11 ################################# [100%]

我们可以看到在/etc/yum.repos.d/目录下多了与mysql相关的两个源。

bash 复制代码
root@VM-24-10-centos ~]# ll /etc/yum.repos.d/
total 24
-rw-r--r-- 1 root root  614 Apr 14  2024 CentOS-Base.repo
-rw-r--r-- 1 root root  230 Apr 14  2024 CentOS-Epel.repo
-rw-r--r-- 1 root root 1358 Sep  5  2021 epel.repo
-rw-r--r-- 1 root root 1457 Sep  5  2021 epel-testing.repo
-rw-r--r-- 1 root root 1838 Apr 27  2017 mysql-community.repo
-rw-r--r-- 1 root root 1885 Apr 27  2017 mysql-community-source.repo

确认MySQL源安装成功

bash 复制代码
[root@VM-24-10-centos ~]# yum list|grep mysql
Repository epel is listed more than once in the configuration
mysql57-community-release.noarch         el7-11                        installed
akonadi-mysql.x86_64                     1.9.2-4.el7                   os       
anope-mysql.x86_64                       2.1.4-1.el7                   epel     
apr-util-mysql.x86_64                    1.5.2-6.el7_9.1               updates  
calligra-kexi-driver-mysql.x86_64        2.9.10-2.el7                  epel     
collectd-mysql.x86_64                    5.8.1-2.el7                   epel     
dmlite-plugins-mysql.x86_64              1.15.2-15.el7                 epel     
dovecot-mysql.x86_64                     1:2.2.36-8.el7                os       
dpm-copy-server-mysql.x86_64             1.13.0-1.el7                  epel     
dpm-name-server-mysql.x86_64             1.13.0-1.el7                  epel     
dpm-server-mysql.x86_64                  1.13.0-1.el7                  epel  

安装MySQL

bash 复制代码
[root@VM-24-10-centos ~]# yum install -y mysql-community-server
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
......
Complete!

可能会碰到安装遇到秘钥过期的问题:

Failing package is: mysql-community-client-5.7.39-1.el7.x86_64 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解决方案:

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

确认MySQL安装成功

bash 复制代码
[root@VM-24-10-centos ~]# ls /etc/my.cnf
/etc/my.cnf
[root@VM-24-10-centos ~]# which mysql
/usr/bin/mysql
[root@VM-24-10-centos ~]# which mysqld
/usr/sbin/mysqld

启动MySQL

bash 复制代码
[root@VM-24-10-centos ~]# systemctl start mysqld
[root@VM-24-10-centos ~]# ps axj |grep mysql
    1 16034 16033 16033 ?           -1 Sl      27   0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
 3096 16076 16075  3096 pts/0    16075 S+       0   0:00 grep --color=auto mysql

三、登录MySQL

bash 复制代码
[root@VM-24-10-centos ~]# mysql -uroot -p
Enter password: 

这里我们看到登录MySQL需要密码,但是我们不知道密码。最直接的方法是修改MySQL的配置文件,我们只需要在配置文件末尾加上skip-grant-tables

bash 复制代码
[root@VM-24-10-centos ~]# vim /etc/my.cnf
[root@VM-24-10-centos ~]# systemctl restart mysqld

不要忘记配置完文件需要重新启动MySQL。

bash 复制代码
[root@VM-24-10-centos ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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了。

四、设置配置文件

继续配置

bash 复制代码
# 端口号设为默认值
port=3306
# 编码方式使用utf-8
character-set-server=utf8
# 选择存储引擎
default-storage-engine=innodb

配置完成后再次重启即可。

bash 复制代码
#开启开机⾃启动
systemctl enable mysqld
systemctl daemon-reload

至于自启动配置,笔者使用的是云服务器就不配置了。

相关推荐
HackTwoHub7 分钟前
AI大模型网关存在SQL注入、附 POC 复现、影响版本LiteLLM 1.81.16~1.83.7(CVE-2026-42208)
数据库·人工智能·sql·网络安全·系统安全·网络攻击模型·安全架构
l1t13 分钟前
DeepSeek总结的DuckLake构建基于 SQL 原生表格式的下一代数据湖仓
数据库·sql
KmSH8umpK21 分钟前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案进阶第八篇
数据库·redis·分布式
其实防守也摸鱼35 分钟前
CTF密码学综合教学指南--第五章
开发语言·网络·笔记·python·安全·网络安全·密码学
TDengine (老段)1 小时前
从施工监测到运营预警,桥科院用 TDengine 提升桥梁数据管理能力
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
S1998_1997111609•X2 小时前
论mysql国盾shell-sfa犯罪行为集团下的分项工程及反向注入原理尐深度纳米算法下的鐌檵鄐鉎行为
网络·数据库·网络协议·百度·开闭原则
Yeh2020583 小时前
Filter与Listener笔记
笔记
KmSH8umpK3 小时前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案进阶第七篇
数据库·redis·分布式
九成宫3 小时前
Git 与远程仓库实操记录:克隆、配置、分支推送与问题排查
笔记·git·ssh
BU摆烂会噶4 小时前
【LangGraph】持久化实现的三大能力——时间旅行
数据库·人工智能·python·postgresql·langchain