【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

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

相关推荐
heartbeat..4 小时前
Spring AOP 全面详解(通俗易懂 + 核心知识点 + 完整案例)
java·数据库·spring·aop
晓幂5 小时前
【2025】HECTF
笔记·学习·web安全
麦聪聊数据6 小时前
MySQL并发与锁:从“防止超卖”到排查“死锁”
数据库·sql·mysql
做cv的小昊7 小时前
【TJU】信息检索与分析课程笔记和练习(8)(9)发现系统和全文获取、专利与知识产权基本知识
大数据·笔记·学习·全文检索·信息检索
AC赳赳老秦7 小时前
DeepSeek 私有化部署避坑指南:敏感数据本地化处理与合规性检测详解
大数据·开发语言·数据库·人工智能·自动化·php·deepseek
hkNaruto7 小时前
【AI】AI学习笔记:MCP协议与gRPC、OpenAPI的差异
人工智能·笔记·学习
myzshare7 小时前
实战分享:我是如何用SSM框架开发出一个完整项目的
java·mysql·spring cloud·微信小程序
YMatrix 官方技术社区8 小时前
YMatrix 存储引擎解密:MARS3 存储引擎如何超越传统行存、列存实现“时序+分析“场景性能大幅提升?
开发语言·数据库·时序数据库·数据库架构·智慧工厂·存储引擎·ymatrix
辞砚技术录8 小时前
MySQL面试题——索引2nd
数据库·mysql·面试
九成宫8 小时前
计算机网络期末复习——第2章:应用层 Part One
笔记·计算机网络·软件工程