Centos7下MySQL的环境卸载、安装和登录

1.卸载MySQL or MariaDB

MariaDB本质上就是MySQL,MariaDB是MySQL的一个分支。
首先检查我们的系统中MySQL或者MariaDB是否运行了。

[usrname@VM-16-2-centos ~]$ ps -axj | grep mysql
  750 28252 28251   750 pts/0    28251 S+    1001   0:00 grep --color=auto mysql
  19010 19187 19010 19010 ? -1 Sl 27 16:55 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pidfile=/var/run/mariadb/mariadb.pid 
--socket=/var/lib/mysql/mysql.sock
[usrname@VM-16-2-centos ~]$ ps -axj | grep mariadb
  750 28377 28376   750 pts/0    28376 S+    1001   0:00 grep --color=auto mariadb

如果运行了就需要关闭再卸载,就和Windows下卸载应用一样的。下面是关闭MySQL服务,注意需要求换成超级用户:

systemctl stop mariadb # 停⽌mariadb 服务
systemctl stop mysql # 停⽌mysql 服务

最后检查是否关闭成功:

# 停⽌完成
[usrname@VM-16-2-centos ~]$ ps axj |grep mysql 
13134 14976 14975 13134 pts/0 14975 S+ 1005 0:00 grep --color=auto 
mysql

2.检查系统安装包

我们用yum下载的安装包一般都是.rpm形式的安装包。
查看所有的安装包:rpm -qa
查看mysql安装包:rpm -qa | grep mysql或者rpm -qa | grep mariadb

[root@VM-16-2-centos usrname]# rpm -qa | grep mysql
mysql-community-common-5.7.41-1.el7.x86_64
11mysql-community-server-5.7.41-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-client-5.7.41-1.el7.x86_64
mysql-community-libs-5.7.41-1.el7.x86_64

3.卸载安装包

因为我们曾经使用yum安装的:yum install
所以现在我们用yum进行卸载:yum remove

  • 我们可以一个一个的卸载:
sudo yum remove mysql*** # 将上⾯的包都⼀个⼀个卸载
  • 也可以使用-xargs,将显示的压缩包以命令行参数的形式一个一个的拼接到yum remove后面进行批量化卸载:
rpm -qa | grep mysql | -xargs yum -y remove

卸载的时候可以备份一下配置文件和数据,但是默认mysql卸载时是不会删除数据的
配置文件所在路径:/etc/my.cnf,建议备份!
数据所在路径:/var/lib/mysql,它是上一个mysql残余的数据,建议备份!
备份的时候重命名即可。

4.获取MySQL官方yum源

首先获取mysql官方yum源:http://repo.mysql.com/

  • 最好安装和系统一致的mysql版本,否则可能会存在软件兼容性问题
    我的系统版本如下:
[root@VM-16-2-centos usrname]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

在Windows下打开上述网址,找到和自己版本匹配的资源,点击下载
在这里插入图片描述
打开我们的云服务器,新建一个目录~/mysql/,输入rz回车,将本地安装包上传至云服务器。
在这里插入图片描述
在这里插入图片描述
至此yum源安装包下载完毕:

[usrname@VM-16-2-centos MySQL]$ ls
mysql57-community-release-el7.rpm

5.更新本地yum源仓库

更新本地机器的yum源仓库,让它具有mysql的yum源.
首先查看一下我们本地yum源:

[usrname@VM-16-2-centos MySQL]$ ls /etc/yum.repos.d -l
total 24
-rw-r--r-- 1 root root  614 May  5 12:41 CentOS-Base.repo
-rw-r--r-- 1 root root  230 May  5 12:41 CentOS-Epel.repo
-rw-r--r-- 1 root root  998 Dec 11  2018 CentOS-SCLo-scl.repo
-rw-r--r-- 1 root root  971 Oct 29  2018 CentOS-SCLo-scl-rh.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

发现并没有mysql的yum源,如果我们直接安装会失败!
所以要进行yun源安装:

[usrname@VM-16-2-centos MySQL]$ sudo rpm -ivh mysql57-community-release-el7.rpm
[sudo] password for Pau: 
warning: 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%]

安装好之后我们的本地yum源仓库就有了mysql yum源。

[usrname@VM-16-2-centos MySQL]$ ls /etc/yum.repos.d/ -l
total 32
-rw-r--r-- 1 root root  614 May  5 12:41 CentOS-Base.repo
-rw-r--r-- 1 root root  230 May  5 12:41 CentOS-Epel.repo
-rw-r--r-- 1 root root  998 Dec 11  2018 CentOS-SCLo-scl.repo
-rw-r--r-- 1 root root  971 Oct 29  2018 CentOS-SCLo-scl-rh.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-community.repo,里面有各种版本。
看一下yum能不能正常工作(安装mysql):
在这里插入图片描述
安装yum源之后,yum源安装包就可以删除了。

6.安装mysql服务

直接使用yum安装

sudo yum install -y mysql-community-server

安装时可能会出现下面问题,这是因为密钥过期导致的

Failing package is: mysql-community-libs-5.7.44-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

再一次安装就成功了!
在这里插入图片描述
检查一下我们是否安装成功,查看配置文件件和可执行程序安装位置:

[usrname@VM-16-2-centos MySQL]$ ls /etc/my.cnf
/etc/my.cnf
[usrname@VM-16-2-centos MySQL]$ which mysqld # mysql服务端
/usr/sbin/mysqld
[usrname@VM-16-2-centos MySQL]$ which mysql # mysql客户端
/usr/bin/mysql

启动mysql服务端,查看后台mysql是否启动,发现后台有mysql守护进程:

[usrname@VM-16-2-centos MySQL]$ sudo systemctl start mysqld
[usrname@VM-16-2-centos MySQL]$ ps axj | grep mysqld
    1  5573  5572  5572 ?           -1 Sl      27   0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
  750 18708 18707   750 pts/0    18707 S+    1001   0:00 grep --color=auto mysqld

7.登录mysql服务

(1)方案一:获取临时密码 [没有用的就用下一个方法]

[usrname@VM-16-2-centos ~]$ sudo grep 'temporary password' /var/log/mysqld.log # 获取临时密码
[sudo] password for usrname: 
2024-07-14T04:28:02.524992Z 1 [Note] A temporary password is generated for root@localhost: ItLEjpURi2/9
[usrname@VM-16-2-centos ~]$ mysql -uroot -p # -u后面带哪个用户 -p表示密码
Enter password: 
# 登录MySQL客户端成功
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44

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> 

(2)方案二:最新的MySQL可能没有临时密码,root用户没有默认密码,遇到输入密码窗口时直接回车 [没有用的就用下一个方法]
(3)方案三:修改配置文件
vi /etc/my.cnf打开配置文件(注意:如果不是root用户将无法修改配置文件,建议切换成root用户或者在命令前加sudo),在[mysqld]最后⼀栏配置加入skip-grant-tables 选项保存退出.
在这里插入图片描述
重新登陆MySQL,发现还是不行,这是因为配置文件在MySQL服务器启动的时候才加载,而我们的MySQL服务器之前已经启动过了。

[usrname@VM-16-2-centos ~]$ mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

重启MySQL服务器:

[root@VM-16-2-centos usrname]# systemctl restart mysqld # 重启MySQL服务器
[root@VM-16-2-centos usrname]# mysql -uroot # 不用输入密码直接登录
# MySQL客户端登陆成功
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> 

8.配置my.cnf

配置服务端的端口号(默认3306),配置字符编码格式,配置存储引擎为InnoDB。
打开配置文件vi /etc/my.cnf,将下面指令写入配置文件,保存退出

port=3306
character-set-server=utf8
default-storage-engine=innodb

最后同样的重启服务器。

9.设置开机启动

对于云服务器而言可以不设,因为云服务器是不会关机的。具体的操作输入下面两条指令即可:

#开启开机⾃启动
systemctl enable mysqld
systemctl daemon-reload
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值