目录
[1. 卸载不要的环境](#1. 卸载不要的环境)
[2. 配置Mysql yum源](#2. 配置Mysql yum源)
[2.1 获取mysql官方yum源](#2.1 获取mysql官方yum源)
[2.2 结合自己的系统来下载:](#2.2 结合自己的系统来下载:)
[3. 开始安装](#3. 开始安装)
[4. 进行登录](#4. 进行登录)
[4.1 登陆方法一:临时密码](#4.1 登陆方法一:临时密码)
[4.2 登陆方法二:](#4.2 登陆方法二:)
[4.3 登陆方法三:](#4.3 登陆方法三:)
[5. 设置配置文件](#5. 设置配置文件)
[6. 设置开机自启动(根据自己的需求)](#6. 设置开机自启动(根据自己的需求))
1. 卸载不要的环境
环境:centos7
root@iZ2vc2ummd1vkuxyf2vgvkZ \~# ps axj | grep mysql
1527 1559 1558 1527 pts/0 1558 S+ 0 0:00 grep --color=auto mysql
root@iZ2vc2ummd1vkuxyf2vgvkZ \~# ps axj | grep mariadb
1527 1561 1560 1527 pts/0 1560 S+ 0 0:00 grep --color=auto mariadb
可以看到我们新换的环境中是没有 mysql 和 mariadb 对应的进程,查看对应的安装包:

没有的话,就不需要进行操作了,有的话,输入命令:
rpm -qa | grep mysql | xargs yum -y remove (对应的是当时mysql是通过yum方式下载的的写在操作,可根据自己实际的情况来进行更改指令)
至此环境就被卸载干净了,为了确认是否卸载好,命令:
root@iZ2vc2ummd1vkuxyf2vgvkZ \~# ls /etc/my.cnf
ls: cannot access /etc/my.cnf: No such file or directory
出现以上的结果,卸载干净
ls /var/lib/mysql:查看上一个数据库残留下的数据,mysql卸载时没有将对应的数据删掉,有数据的话,也是不影响后续的操作
- 没有rpm包。rpm -qa | grep mysql
- 没有对应的进程。 ps axj | grep mysql
- 没有配置文件。 ps axj | grep mysql
2. 配置Mysql yum源
2.1 获取mysql官方yum源
2.2 结合自己的系统来下载:
root@iZ2vc2ummd1vkuxyf2vgvkZ \~# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
el:代表的是centos

查看源代码:

我们对应的版本是7.6的,没有与此对应的版本,选择这个连接进行下载:

rpm 就是在Linux的一个安装包,里面包含的是mysql的yum源,将刚刚下载好的文件上传到Linux服务器中:
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# rz
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# ll
total 28
-rw-r--r-- 1 root root 25680 Sep 7 09:05 mysql57-community-release-el7.rpm
安装命令:
查看系统当中默认支持的yum源:
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# ls /etc/yum.repos.d/ -l
total 8
-rw-r--r-- 1 root root 675 Sep 7 08:16 CentOS-Base.repo
-rw-r--r-- 1 root root 230 Sep 7 08:16 epel.repo
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# rpm -ivh mysql57-community-release-el7.rpmwarning: mysql57-community-release-el7.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# 100%
Updating / installing...
1:mysql57-community-release-el7-11 ################################# 100%
再看一下安装后的结果:
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# ls /etc/yum.repos.d/ -l
total 16
-rw-r--r-- 1 root root 675 Sep 7 08:16 CentOS-Base.repo
-rw-r--r-- 1 root root 230 Sep 7 08:16 epel.repo
-rw-r--r-- 1 root root 1838 Apr 27 2017 mysql-community.repo(安装MySQL,我们只需要看这个就可以了)
-rw-r--r-- 1 root root 1885 Apr 27 2017 mysql-community-source.repo(支持安装MySQL周边的资源和内容)
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# vim /etc/yum.repos.d/mysql-community
到底能不能工作呢??查到我想查到的内容呢?
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# yum list | grep mysql

说明,yum源已经生效了。可以将对应的安装包删除:rm mysql57-community-release-el7.rpm ,不删也行
3. 开始安装
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# yum install -y mysql-community-server
密钥过期的问题:

解决方案:
输入命令:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
再次安装MySQL就没有问题了:

安装成功后存在此文件:
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# ls /etc/my.cnf
/etc/my.cnf
必须的存在MySQL对应的服务端的应用程序:
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# which mysqld
/usr/sbin/mysqld
sbin:需要超级用户去执行它
必须存在MySQL对应的客户端:
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# which mysql
/usr/bin/mysql
以上三个命令都有对应的文件存在,至此,证明MySQL安装完毕
启动命令:
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# systemctl start mysqld
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# ps ajx | grep mysqld
1 14874 14873 14873 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
1527 14935 14934 1527 pts/0 14934 S+ 0 0:00 grep --color=auto mysqld

4. 进行登录
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
4.1 登陆方法一:临时密码
看看是否有临时密码,获取临时root密码:
sudo grep 'temporary password' /var/log/mysqld.log


后续操作:
#判断修改密码时候新密码是否符合当前的策略,不满足报错,不让修改,关闭它
#安全强度,默认为中,即1,要求必须包含 数字、符号、大小写字母,长度至少为8位
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
#密码最小长度
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
#修改本地登录密码,暂不授权远程登录
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'qwer@wu.888';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
#如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码
4.2 登陆方法二:
如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码
试着直接client登陆⼀下
4.3 登陆方法三:
root@bite-alicloud mysql# vim /etc/my.cnf # 打开mysql配置文件
在mysqld最后⼀栏配置(不知道是什么,就放在配置文件最后) 加入: skip-grant-tables 选项,
并保存退出
root@bite-alicloud mysql# systemctl restart mysqld # 重启mysql服务
登陆成功
root@bite-alicloud mysql# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.41 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服务,此时才能起效
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# systemctl stop mysqld
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# systemctl start mysqld
root@iZ2vc2ummd1vkuxyf2vgvkZ MySQL# ps axj | grep mysql
1 16269 16268 16268 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
1527 16317 16316 1527 pts/0 16316 S+ 0 0:00 grep --color=auto mysql


5. 设置配置文件
vim /etc/my.cnf
datadir=/var/lib/mysql:mysql建表建库时,对应数据所在的路径log-error=/var/log/mysqld.log:错误日志所在的地方
添加的配置:
端口:port = 3366;(根据自己的需求,建议不修改)
server端对应的编码格式:character-set-server=utf8
MySQL用的默认的存储引擎:default-storage-engine=innodb

重启之后端口号就被重新设置好了。

恢复为之前的端口号。
6. 设置开机自启动(根据自己的需求)
systemctl enable mysqld
systemctl daemon-reload
设不设置都无所谓,因为云服务器是不关闭的。