CentOS7下彻底删除MySQL和重新安装MySQL
CentOS7下彻底删除MySQL和重新安装MySQL
删除MySQL
-
清空之前的数据库日志(可以直接
rm /var/log/mysqld.log
)[root@localhost ~]# rm /var/log/mysqld.log rm: remove regular file ‘/var/log/mysqld.log’? y
-
yum remove mysql mysql-server mysql-libs mysql-server;
[root@localhost ~]# yum remove mysql mysql-server mysql-libs mysql-server; Loaded plugins: fastestmirror Resolving Dependencies --> Running transaction check ---> Package mysql-community-client.x86_64 0:8.0.20-1.el7 will be erased ---> Package mysql-community-libs.x86_64 0:8.0.20-1.el7 will be erased ---> Package mysql-community-server.x86_64 0:8.0.20-1.el7 will be erased --> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================== Package Arch Version Repository Size ===================================================================================================================================== Removing: mysql-community-client x86_64 8.0.20-1.el7 @mysql80-community 225 M mysql-community-libs x86_64 8.0.20-1.el7 @mysql80-community 22 M mysql-community-server x86_64 8.0.20-1.el7 @mysql80-community 2.2 G Transaction Summary ===================================================================================================================================== Remove 3 Packages Installed size: 2.5 G Is this ok [y/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Erasing : mysql-community-server-8.0.20-1.el7.x86_64 1/3 Erasing : mysql-community-client-8.0.20-1.el7.x86_64 2/3 Erasing : mysql-community-libs-8.0.20-1.el7.x86_64 3/3 Verifying : mysql-community-client-8.0.20-1.el7.x86_64 1/3 Verifying : mysql-community-libs-8.0.20-1.el7.x86_64 2/3 Verifying : mysql-community-server-8.0.20-1.el7.x86_64 3/3 Removed: mysql-community-client.x86_64 0:8.0.20-1.el7 mysql-community-libs.x86_64 0:8.0.20-1.el7 mysql-community-server.x86_64 0:8.0.20-1.el7 Complete!
-
find / -name mysql
将找到的相关东西全部remove掉(如rm -rf /var/lib/mysql
,有多少删多少就行);[root@localhost ~]# find / -name mysql /var/lib/mysql /var/lib/mysql/mysql [root@localhost ~]# rm -rf /var/lib/mysql [root@localhost ~]# rm -rf /var/lib/mysql/mysql
-
检查mysql
rpm -qa|grep mysql
(查询出来的东西用yum remove
掉);
[root@localhost ~]# rpm -qa|grep mysql
mysql-community-common-8.0.20-1.el7.x86_64
mysql80-community-release-el7-3.noarch
[root@localhost ~]# yum remove mysql-community-common-8.0.20-1.el7.x86_64
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-common.x86_64 0:8.0.20-1.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
=====================================================================================================================================
Package Arch Version Repository Size
=====================================================================================================================================
Removing:
mysql-community-common x86_64 8.0.20-1.el7 @mysql80-community 8.6 M
Transaction Summary
=====================================================================================================================================
Remove 1 Package
Installed size: 8.6 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : mysql-community-common-8.0.20-1.el7.x86_64 1/1
Verifying : mysql-community-common-8.0.20-1.el7.x86_64 1/1
Removed:
mysql-community-common.x86_64 0:8.0.20-1.el7
Complete!
[root@localhost ~]# yum remove mysql80-community-release-el7-3.noarch
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package mysql80-community-release.noarch 0:el7-3 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
=====================================================================================================================================
Package Arch Version Repository Size
=====================================================================================================================================
Removing:
mysql80-community-release noarch el7-3 installed 31 k
Transaction Summary
=====================================================================================================================================
Remove 1 Package
Installed size: 31 k
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : mysql80-community-release-el7-3.noarch 1/1
Verifying : mysql80-community-release-el7-3.noarch 1/1
Removed:
mysql80-community-release.noarch 0:el7-3
Complete!
- 删除旧的配置文件rm /etc/my.cnf
[root@localhost ~]# rm /etc/my.cnf
rm: cannot remove ‘/etc/my.cnf’: No such file or directory
-
查看是否还有mysql软件:
rpm -qa|grep mysql
,如果存在的话,继续删除即可。 -
如果之前的MySQL是用rpm方式安装的
a)查看系统中是否以rpm包安装的mysql:
[root@localhost opt]# rpm -qa | grep -i mysql MySQL-server-5.6.17-1.el6.i686 MySQL-client-5.6.17-1.el6.i686
b)卸载mysql
[root@localhost local]# rpm -e MySQL-server-5.6.17-1.el6.i686 [root@localhost local]# rpm -e MySQL-client-5.6.17-1.el6.i686
c)删除mysql服务
[root@localhost local]# chkconfig --list | grep -i mysql [root@localhost local]# chkconfig --del mysql
d)删除分散mysql文件夹
[root@localhost local]# whereis mysql 或者 find / -name mysql mysql: /usr/lib/mysql /usr/share/mysql 清空相关mysql的所有目录以及文件 rm -rf /usr/lib/mysql rm -rf /usr/share/mysql rm -rf /usr/my.cnf
通过以上几步,mysql应该已经完全卸载干净了。
安装MySQL
在这里安装的是MySQL8.0版本,安装MySQL5.7版本的操作大致差不多。
- 配置安装源
[linkai@localhost ~]# sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
Retrieving https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:mysql80-community-release-el7-3 ################################# [100%]
5.7的话:
[linkai@localhost ~]# sudo wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[linkai@localhost ~]# sudo yum -y install mysql57-community-release-el7-10.noarch.rpm
-
安装
[linkai@localhost ~]# sudo yum --enablerepo=mysql80-community install mysql-community-server Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package mysql-community-server.x86_64 0:8.0.20-1.el7 will be installed --> Processing Dependency: mysql-community-common(x86-64) = 8.0.20-1.el7 for package: mysql-community-server-8.0.20-1.el7.x86_64 --> Processing Dependency: mysql-community-client(x86-64) >= 8.0.11 for package: mysql-community-server-8.0.20-1.el7.x86_64 --> Running transaction check ---> Package mysql-community-client.x86_64 0:8.0.20-1.el7 will be installed --> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.20-1.el7.x86_64 ---> Package mysql-community-common.x86_64 0:8.0.20-1.el7 will be installed --> Running transaction check ---> Package mysql-community-libs.x86_64 0:8.0.20-1.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================== Package Arch Version Repository Size ===================================================================================================================================== Installing: mysql-community-server x86_64 8.0.20-1.el7 mysql80-community 488 M Installing for dependencies: mysql-community-client x86_64 8.0.20-1.el7 mysql80-community 47 M mysql-community-common x86_64 8.0.20-1.el7 mysql80-community 609 k mysql-community-libs x86_64 8.0.20-1.el7 mysql80-community 4.5 M Transaction Summary ===================================================================================================================================== Install 1 Package (+3 Dependent packages) Total download size: 540 M Installed size: 2.5 G Is this ok [y/d/N]: y Downloading packages: (1/4): mysql-community-common-8.0.20-1.el7.x86_64.rpm | 609 kB 00:00:01 (2/4): mysql-community-libs-8.0.20-1.el7.x86_64.rpm | 4.5 MB 00:00:01 (3/4): mysql-community-client-8.0.20-1.el7.x86_64.rpm | 47 MB 00:00:15 (4/4): mysql-community-server-8.0.20-1.el7.x86_64.rpm | 488 MB 00:02:32 ------------------------------------------------------------------------------------------------------------------------------------- Total 3.5 MB/s | 540 MB 00:02:35 Running transaction check Running transaction test Transaction test succeeded Running transaction Warning: RPMDB altered outside of yum. Installing : mysql-community-common-8.0.20-1.el7.x86_64 1/4 Installing : mysql-community-libs-8.0.20-1.el7.x86_64 2/4 Installing : mysql-community-client-8.0.20-1.el7.x86_64 3/4 Installing : mysql-community-server-8.0.20-1.el7.x86_64 4/4 Verifying : mysql-community-client-8.0.20-1.el7.x86_64 1/4 Verifying : mysql-community-libs-8.0.20-1.el7.x86_64 2/4 Verifying : mysql-community-common-8.0.20-1.el7.x86_64 3/4 Verifying : mysql-community-server-8.0.20-1.el7.x86_64 4/4 Installed: mysql-community-server.x86_64 0:8.0.20-1.el7 Dependency Installed: mysql-community-client.x86_64 0:8.0.20-1.el7 mysql-community-common.x86_64 0:8.0.20-1.el7 mysql-community-libs.x86_64 0:8.0.20-1.el7 Complete!
5.7的话:
[linkai@localhost ~]# sudo yum -y install mysql-community-server
-
启动mysql:
systemctl start mysqld
-
查看运行状态:
systemctl status mysqld
[linkai@localhost ~]# systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2020-06-11 00:00:27 CST; 3s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 28633 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 28713 (mysqld) Status: "Server is operational" CGroup: /system.slice/mysqld.service └─28713 /usr/sbin/mysqld Jun 11 00:00:18 iZ2zehlypfpbchjd5bsvxjZ systemd[1]: Starting MySQL Server... Jun 11 00:00:27 iZ2zehlypfpbchjd5bsvxjZ systemd[1]: Started MySQL Server.
-
停止:
systemctl stop mysqld
-
重启:
systemctl restart mysqld
-
查看并修改密码
-
mysql安装后会生成一个默认密码,此密码登录后要修改密码,不能用于其他操作,通过这个命令可以查看初始密码:
grep "password" /var/log/mysqld.log
,比方说结果是:2020-06-10T16:00:21.662964Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: dEDf./DgA6tE
,初始密码就是最后的dEDf./DgA6tE
。 -
登录root账号修改初始密码,复制上面的密码登录
[linkai@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1430
Server version: 8.0.20
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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 statementmysql -u root -p
修改密码,注意密码要稍微复杂点(大小写+符号+数字),不然会提示 Your password does not satisfy the current policy requirements
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yz+Lk12.26';
Query OK, 0 rows affected (0.01 sec)
本人在8.0的时候无法修改密码复杂度,但是5.7可以修改:
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=0;
这样可以把密码设置最简单的模式。
新建MySQL账号并授权
-
创建一个可以叫’app’@’%'的用户,然后授予可以增删改查以及创建数据库和删除数据库的功能
mysql> create user 'app'@'%' identified by 'App@2020'; Query OK, 0 rows affected (0.02 sec) mysql> grant select,insert,delete,update,drop,create on *.* to 'app'@'%'; Query OK, 0 rows affected (0.01 sec)
-
创建一个可以叫’linkai’@’%'的用户,然后授予管理者的功能(即除了创建MySQL账号并授权的权限外的所有权限)
mysql> create user linkai@'%' identified by 'Linkai@2020'; Query OK, 0 rows affected (0.01 sec) mysql> grant all on *.* to linkai@'%' with grant option; Query OK, 0 rows affected (0.01 sec)
-
刷新权限
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
常见问题和处理
- 如果客户端连接报错"Authentication plugin ‘caching_sha2_password’ cannot be loaded",是以为mysql 8的加密方式改了。有两种选择:1、升级客户端 2、把加密方式改为传统的方式
推荐用新的加密方式(一般升级客户端就可以了),老项目代码的话可能有老的jar包等不支持新加密,下面是改为传统加密方式
mysql> alter user 'linkai'@'%' identified with mysql_native_password by 'Linkai@2020';
Query OK, 0 rows affected (0.01 sec)
mysql> alter user 'app'@'%' identified with mysql_native_password by 'App@2020';
Query OK, 0 rows affected (0.00 sec)
-
查找mysql的安装路径
[root@localhost ~]# whereis mysql mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/man/man1/mysql.1.gz
-
创建数据库,字符集有两种
方式 字符集 排序规则 不支持emoji utf8 utf8_general_ci 支持emoji utf8mb4 utf8mb4_general_ci -
查看mysql版本
[root@localhost ~]# mysql -V mysql Ver 8.0.20 for Linux on x86_64 (MySQL Community Server - GPL)
-
因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把它卸载掉
[root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch