注意:在MYSQL的安装与卸载中,需要使用root用户进行。
一、卸载不必要的环境
• 查看是否有运行的服务
[root@VM-24-10-centos etc]# ps axj |grep mysql1 22030 22029 22029 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid3096 22531 22530 3096 pts/0 22530 R+ 0 0:00 grep --color=auto mysql
[root@VM-24-10-centos etc]# ps axj |grep maridb3096 22672 22671 3096 pts/0 22671 S+ 0 0:00 grep --color=auto maridb
我们可以看到当前的机器上是存在着mysql的,如果没有则可直接跳过这步操作,下面的操作对应maridb也是一样的。
• 关闭运行的服务
[root@VM-24-10-centos etc]# systemctl stop mysqld
[root@VM-24-10-centos etc]# ps axj |grep mysql3096 23210 23209 3096 pts/0 23209 S+ 0 0:00 grep --color=auto mysql
• 查找服务器上的mysql安装包
[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
• 卸载全部的安装包
[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!
• 再次确认
[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源我们需要与当前系统相结合下载。
[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端。
[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源
[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相关的两个源。
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源安装成功
[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
[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安装成功
[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
[root@VM-24-10-centos ~]# systemctl start mysqld
[root@VM-24-10-centos ~]# ps axj |grep mysql1 16034 16033 16033 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid3096 16076 16075 3096 pts/0 16075 S+ 0 0:00 grep --color=auto mysql
三、登录MySQL
[root@VM-24-10-centos ~]# mysql -uroot -p
Enter password:
这里我们看到登录MySQL需要密码,但是我们不知道密码。最直接的方法是修改MySQL的配置文件,我们只需要在配置文件末尾加上skip-grant-tables
。
[root@VM-24-10-centos ~]# vim /etc/my.cnf
[root@VM-24-10-centos ~]# systemctl restart mysqld
不要忘记配置完文件需要重新启动MySQL。
[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了。
四、设置配置文件
继续配置
# 端口号设为默认值
port=3306
# 编码方式使用utf-8
character-set-server=utf8
# 选择存储引擎
default-storage-engine=innodb
配置完成后再次重启即可。
#开启开机⾃启动
systemctl enable mysqld
systemctl daemon-reload
至于自启动配置,笔者使用的是云服务器就不配置了。