CentOS 7 安装与卸载MySQL
2018-04-11 本文已影响111人
阳光的技术小栈
卸载
第一步
分为yum方式和rpm方式
- yum方式
- 首先查看yum是否安装过mysql
yum list installed mysql*
如果显示了列表,说明系统中有MySql。
- yum卸载
根据列表上的名字
yum remove mysql-community-client mysql-community-common mysql-community-libs mysql-community-libs-compat mysql-community-server mysql57-community-release
rm -rf /var/lib/mysql
rm /etc/my.cnf
- rpm方式
- 首先查看rpm是否安装过mysql
rpm -qa | grep -i mysql
- rpm卸载
rpm -e mysql57-community-release-el7-9.noarch
rpm -e mysql-community-server-5.7.17-1.el7.x86_64
rpm -e mysql-community-libs-5.7.17-1.el7.x86_64
rpm -e mysql-community-libs-compat-5.7.17-1.el7.x86_64
rpm -e mysql-community-common-5.7.17-1.el7.x86_64
rpm -e mysql-community-client-5.7.17-1.el7.x86_64
cd /var/lib/
rm -rf mysql/
第二步
清除余项
whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/local/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
#删除上面的文件夹
rm -rf /usr/bin/mysql
rm -rf /usr/lib64/mysql
rm -rf /usr/local/mysql
rm -rf /usr/share/mysql
rm -rf /usr/share/man/man1/mysql.1.gz
第三步
删除配置
rm –rf /usr/my.cnf
rm -rf /root/.mysql_sercret
第四步
剩余配置检查
chkconfig --list | grep -i mysql
chkconfig --del mysqld
根据上面的列表删除 ,如:mysqldf
安装(mysql5.6)
centos自带的repo是不会自动更新每个软件的最新版本,所以无法通过yum方式安装MySQL的高级版本。
即使用yum -y install mysql mysql-server mysql-devel,也是没用的。
所以,正确的安装mysql5姿势是要先安装带有可用的mysql5系列社区版资源的rpm包
使用以下命令
rpm -Uvh http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
结果显示
获取http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-release-el7-5 ################################# [100%]
这个时候查看当前可用的mysql安装资源:
# yum repolist enabled | grep "mysql.*-community.*"
结果显示
mysql-connectors-community/x86_64 MySQL Connectors Community 45
mysql-tools-community/x86_64 MySQL Tools Community 59
mysql56-community/x86_64 MySQL 5.6 Community Server 378
然后我们直接使用yum的方式安装MySQL
[root@VM_0_17_centos ~]# yum -y install mysql-community-server
已加载插件:fastestmirror, langpacks
epel | 4.7 kB 00:00
extras | 3.4 kB 00:00
os | 3.6 kB 00:00
updates | 3.4 kB 00:00
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.5.6.39-2.el7 将被 安装
--> 正在处理依赖关系 mysql-community-common(x86-64) = 5.6.39-2.el7,它被软件包 mysql-community-server-5.6.39-2.el7.x86_64 需要
--> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.6.10,它被软件包 mysql-community-server-5.6.39-2.el7.x86_64 需要
--> 正在处理依赖关系 perl(Data::Dumper),它被软件包 mysql-community-server-5.6.39-2.el7.x86_64 需要
--> 正在处理依赖关系 perl(DBI),它被软件包 mysql-community-server-5.6.39-2.el7.x86_64 需要
--> 正在处理依赖关系 libnuma.so.1(libnuma_1.2)(64bit),它被软件包 mysql-community-server-5.6.39-2.el7.x86_64 需要
--> 正在处理依赖关系 libnuma.so.1(libnuma_1.1)(64bit),它被软件包 mysql-community-server-5.6.39-2.el7.x86_64 需要
--> 正在处理依赖关系 libnuma.so.1()(64bit),它被软件包 mysql-community-server-5.6.39-2.el7.x86_64 需要
--> 正在检查事务
---> 软件包 mysql-community-client.x86_64.0.5.6.39-2.el7 将被 安装
--> 正在处理依赖关系 mysql-community-libs(x86-64) >= 5.6.10,它被软件包 mysql-community-client-5.6.39-2.el7.x86_64 需要
---> 软件包 mysql-community-common.x86_64.0.5.6.39-2.el7 将被 安装
---> 软件包 numactl-libs.x86_64.0.2.0.9-6.el7_2 将被 安装
---> 软件包 perl-DBI.x86_64.0.1.627-4.el7 将被 安装
--> 正在处理依赖关系 perl(RPC::PlServer) >= 0.2001,它被软件包 perl-DBI-1.627-4.el7.x86_64 需要
--> 正在处理依赖关系 perl(RPC::PlClient) >= 0.2000,它被软件包 perl-DBI-1.627-4.el7.x86_64 需要
---> 软件包 perl-Data-Dumper.x86_64.0.2.145-3.el7 将被 安装
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.52-1.el7 将被 取代
---> 软件包 mysql-community-libs.x86_64.0.5.6.39-2.el7 将被 舍弃
---> 软件包 perl-PlRPC.noarch.0.0.2020-14.el7 将被 安装
--> 正在处理依赖关系 perl(Net::Daemon) >= 0.13,它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在处理依赖关系 perl(Net::Daemon::Test),它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在处理依赖关系 perl(Net::Daemon::Log),它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在处理依赖关系 perl(Compress::Zlib),它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在检查事务
---> 软件包 perl-IO-Compress.noarch.0.2.061-2.el7 将被 安装
--> 正在处理依赖关系 perl(Compress::Raw::Zlib) >= 2.061,它被软件包 perl-IO-Compress-2.061-2.el7.noarch 需要
--> 正在处理依赖关系 perl(Compress::Raw::Bzip2) >= 2.061,它被软件包 perl-IO-Compress-2.061-2.el7.noarch 需要
---> 软件包 perl-Net-Daemon.noarch.0.0.48-5.el7 将被 安装
--> 正在检查事务
---> 软件包 perl-Compress-Raw-Bzip2.x86_64.0.2.061-3.el7 将被 安装
---> 软件包 perl-Compress-Raw-Zlib.x86_64.1.2.061-4.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在安装:
mysql-community-libs x86_64 5.6.39-2.el7 mysql56-community 2.0 M
替换 mariadb-libs.x86_64 1:5.5.52-1.el7
mysql-community-server x86_64 5.6.39-2.el7 mysql56-community 59 M
为依赖而安装:
mysql-community-client x86_64 5.6.39-2.el7 mysql56-community 19 M
mysql-community-common x86_64 5.6.39-2.el7 mysql56-community 257 k
numactl-libs x86_64 2.0.9-6.el7_2 os 29 k
perl-Compress-Raw-Bzip2 x86_64 2.061-3.el7 os 32 k
perl-Compress-Raw-Zlib x86_64 1:2.061-4.el7 os 57 k
perl-DBI x86_64 1.627-4.el7 os 802 k
perl-Data-Dumper x86_64 2.145-3.el7 os 47 k
perl-IO-Compress noarch 2.061-2.el7 os 260 k
perl-Net-Daemon noarch 0.48-5.el7 os 51 k
perl-PlRPC noarch 0.2020-14.el7 os 36 k
事务概要
================================================================================
安装 2 软件包 (+10 依赖软件包)
总下载量:82 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/mysql56-community/packages/mysql-community-common-5.6.39-2.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
mysql-community-common-5.6.39-2.el7.x86_64.rpm 的公钥尚未安装
(1/12): mysql-community-common-5.6.39-2.el7.x86_64.rpm | 257 kB 00:01
(2/12): mysql-community-client-5.6.39-2.el7.x86_64.rpm | 19 MB 00:19
(3/12): numactl-libs-2.0.9-6.el7_2.x86_64.rpm | 29 kB 00:00
(4/12): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm | 32 kB 00:00
(5/12): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm | 57 kB 00:00
(6/12): perl-Data-Dumper-2.145-3.el7.x86_64.rpm | 47 kB 00:00
(7/12): perl-IO-Compress-2.061-2.el7.noarch.rpm | 260 kB 00:00
(8/12): perl-Net-Daemon-0.48-5.el7.noarch.rpm | 51 kB 00:00
(9/12): perl-DBI-1.627-4.el7.x86_64.rpm | 802 kB 00:00
(10/12): perl-PlRPC-0.2020-14.el7.noarch.rpm | 36 kB 00:00
(11/12): mysql-community-server-5.6.39-2.el7.x86_64.rpm | 59 MB 00:50
(12/12): mysql-community-libs-5.6.39-2.el7.x86_64.rpm | 2.0 MB 01:55
--------------------------------------------------------------------------------
总计 721 kB/s | 82 MB 01:56
从 file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
用户ID : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
指纹 : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
软件包 : mysql-community-release-el7-5.noarch (installed)
来自 : file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
正在安装 : perl-Data-Dumper-2.145-3.el7.x86_64 1/13
正在安装 : mysql-community-common-5.6.39-2.el7.x86_64 2/13
正在安装 : mysql-community-libs-5.6.39-2.el7.x86_64 3/13
正在安装 : mysql-community-client-5.6.39-2.el7.x86_64 4/13
正在安装 : numactl-libs-2.0.9-6.el7_2.x86_64 5/13
正在安装 : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 6/13
正在安装 : perl-Net-Daemon-0.48-5.el7.noarch 7/13
正在安装 : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 8/13
正在安装 : perl-IO-Compress-2.061-2.el7.noarch 9/13
正在安装 : perl-PlRPC-0.2020-14.el7.noarch 10/13
正在安装 : perl-DBI-1.627-4.el7.x86_64 11/13
正在安装 : mysql-community-server-5.6.39-2.el7.x86_64 12/13
正在删除 : 1:mariadb-libs-5.5.52-1.el7.x86_64 13/13
warning: file /usr/lib64/mysql/plugin/mysql_clear_password.so: remove failed: No such file or directory
warning: file /usr/lib64/mysql/plugin/dialog.so: remove failed: No such file or directory
warning: file /usr/lib64/mysql/libmysqlclient.so.18.0.0: remove failed: No such file or directory
验证中 : mysql-community-server-5.6.39-2.el7.x86_64 1/13
验证中 : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 2/13
验证中 : perl-Net-Daemon-0.48-5.el7.noarch 3/13
验证中 : mysql-community-common-5.6.39-2.el7.x86_64 4/13
验证中 : perl-Data-Dumper-2.145-3.el7.x86_64 5/13
验证中 : mysql-community-libs-5.6.39-2.el7.x86_64 6/13
验证中 : perl-PlRPC-0.2020-14.el7.noarch 7/13
验证中 : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 8/13
验证中 : numactl-libs-2.0.9-6.el7_2.x86_64 9/13
验证中 : perl-DBI-1.627-4.el7.x86_64 10/13
验证中 : perl-IO-Compress-2.061-2.el7.noarch 11/13
验证中 : mysql-community-client-5.6.39-2.el7.x86_64 12/13
验证中 : 1:mariadb-libs-5.5.52-1.el7.x86_64 13/13
已安装:
mysql-community-libs.x86_64 0:5.6.39-2.el7
mysql-community-server.x86_64 0:5.6.39-2.el7
作为依赖被安装:
mysql-community-client.x86_64 0:5.6.39-2.el7
mysql-community-common.x86_64 0:5.6.39-2.el7
numactl-libs.x86_64 0:2.0.9-6.el7_2
perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7
perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7
perl-DBI.x86_64 0:1.627-4.el7
perl-Data-Dumper.x86_64 0:2.145-3.el7
perl-IO-Compress.noarch 0:2.061-2.el7
perl-Net-Daemon.noarch 0:0.48-5.el7
perl-PlRPC.noarch 0:0.2020-14.el7
替代:
mariadb-libs.x86_64 1:5.5.52-1.el7
完毕!
加入开机启动
systemctl enable mysqld
启动mysql服务进程
systemctl start mysqld
配置用户密码
可以先尝试使用空密码登录
mysql -u root -p
登录成功后显示
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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 statement.
mysql>
如果显示如下类似的错误
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
则需要查看root用户临时随机密码。查找后,原来为了增加数据库的安全性,在安装时会为root用户生成一个临时的随机密码,存放在/var/log/mysqld.log 中。root用户的临时密码查看方法如下(“ XqWlxlV3p.a8”就是其临时密码):
[root@123 mysql]# grep 'password' /var/log/mysqld.log |head -n 1
2 2017-06-17T02:01:01.026946Z 1 [Note] A temporary password is generated for root@localhost: XqWlxlV3p.a8
然后可以修改root密码。
如果使用临时密码登录数据库的话,mysql会提示你修改临时密码,否则无法使用,如下:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
修改密码命令为
mysql> update user set password=PASSWORD("123456") where user='root';
然后退出mysql并重启
systemctl restart mysqld
再次使用新密码就能登录了。
远程访问
需要先登录mysql后,授权
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。
然后刷新(或者重启mysql)
FLUSH PRIVILEGES;
然后再远程访问(使用默认3306端口)
参考文章:
https://blog.csdn.net/huyangg/article/details/71308147