演示环境说明
操作系统:CentOS7 【 192.168.65.116 】 mysql版本:V5.7
各个历史版本下载:https://downloads.mysql.com/archives/community/
最新版本下载地址:https://dev.mysql.com/downloads/mysql/
百度网盘下载地址:https://pan.baidu.com/s/1TugqavLcQoLc4miZEFE5rw 提取码:zhan
安装MySql的时候会默认创建一个mysql组和一个mysql用户,并把datadir和安装目录属主改为mysql,在MySQL启动的时候,单进程mysqld,该进程的属主就是mysql,这样就保证了mysql服务的独立性,
即便mysql服务被黑掉,得到了mysql用户权限,也不会影响整个系统的安全,mysql用户是不登陆的,所以默认没有密码,只是给MySQL服务 一个运行的用户,默认安装在/usr/bin/mysql目录。
一、yum安装MySql
1.1、下载rpm包
[root@CentOS7-0002 yum.repos.d]# mkdir -p /opt/software
[root@CentOS7-0002 yum.repos.d]# cd /opt/software/
[root@CentOS7-0002 software]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@CentOS7-0002 software]# du -sh mysql57-community-release-el7-10.noarch.rpm
28K mysql57-community-release-el7-10.noarch.rpm
1.2、安装rpm包
[root@CentOS7-0002 software]# sudo yum -y install mysql57-community-release-el7-10.noarch.rpm
[root@CentOS7-0002 software]# ll /etc/yum.repos.d
总用量 12
-rw-r--r--. 1 root root 1627 4月 5 2017 mysql-community.repo
-rw-r--r--. 1 root root 1663 4月 5 2017 mysql-community-source.repo
[root@CentOS7-0002 software]#
rpm安装后,/etc/yum.repos.d/目录会新增两个mysql的repo源:mysql-community.repo,mysql-community-source.repo。需要安装哪个版本将文件mysql-community.repo中版本对应的enabled改成1,其他的改成0。
1.3、安装mysql
[root@CentOS7-0002 software]# sudo yum -y install mysql-community-server
安装有点慢,耐心等待完成即可。
至此MySql安装完成,它包含了四个包[common、libs、client、server],并创建了mysql用户和组。
[root@CentOS7-0002 yum.repos.d]# rpm -qa | grep -i mysql
mysql57-community-release-el7-10.noarch
mysql-community-libs-5.7.37-1.el7.x86_64
mysql-community-server-5.7.37-1.el7.x86_64
mysql-community-common-5.7.37-1.el7.x86_64
mysql-community-client-5.7.37-1.el7.x86_64
[root@CentOS7-0002 yum.repos.d]# id mysql
uid=27(mysql) gid=27(mysql) 组=27(mysql)
[root@CentOS7-0002 yum.repos.d]#
1.4、启动MySql查看状态设置开机自启动
[root@CentOS7-0002 yum.repos.d]# systemctl start mysqld
[root@CentOS7-0002 yum.repos.d]# systemctl enable mysqld
[root@CentOS7-0002 yum.repos.d]# systemctl status mysqld
1.5、查初始密码,并重置密码
[root@CentOS7-0002 yum.repos.d]# cat /var/log/mysqld.log | grep password
2022-01-28T03:14:27.055571Z 1 [Note] A temporary password is generated for root@localhost: Bu*wbhz)u2vs
[root@CentOS7-0002 yum.repos.d]#
这里的Bu*wbhz)u2vs就是root的初始密码。
1.6、登录MySql重置密码
使用查询到的临时密码使用命令mysql -u root -p登录MySQL,修改密码
[root@CentOS7-0002 yum.repos.d]# 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.37
Copyright (c) 2000, 2022, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'zhj@AaZz123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
5.7增加了策略更改密码必须满足:数字、小写字母、大写字母 、特殊字符、长度至少8位,如果设设置太简单,会设置失败会提示密码不安全。如果想去掉可在/etc/my.cnf配置文件中[mysqld]节点下增加validate_password=off配置,重启数据库通过SHOW PLUGINS;即可看到密码验证的插件被停止了。
1.7、授权远程远程登录(两种都设置)
1、改表法。可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 “mysql” 数据库里的 “root” 表里的 “host” 项,从"localhost"改称"%"
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host, user from user;
2、授权法。例如,你想root使用password从任何主机连接到mysql服务器的话。
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'zhj@AaZz123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges ;
Query OK, 0 rows affected (0.00 sec)
mysql>
如果你想允许用户root从192.168.56.166的主机连接到mysql服务器
GRANT ALL PRIVILEGES ON . TO ‘root’@‘192.168.56.166’ IDENTIFIED BY ‘zhj@AaZz123456’ WITH GRANT OPTION;
1.8、通过远程工具验证:
如果需要修改MySql的目录,可以修改/etc/my.cnf配置文件,改成对应的目录,重启即可。
二、MySql数据库完全卸载
2.1、关闭MySql关闭开机自启动
[root@CentOS7-0002 lib]# systemctl stop mysqld
[root@CentOS7-0002 lib]# systemctl disable mysqld
2.2、查看rpm
[root@CentOS7-0002 lib]# rpm -qa | grep -i mysql
mysql-community-libs-5.7.37-1.el7.x86_64
mysql-community-server-5.7.37-1.el7.x86_64
mysql57-community-release-el7-10.noarch
mysql-community-common-5.7.37-1.el7.x86_64
mysql-community-client-5.7.37-1.el7.x86_64
2.3、卸载rpm
[root@CentOS7-0002 lib]# rpm -e --nodeps mysql-community-libs-5.7.37-1.el7.x86_64
[root@CentOS7-0002 lib]# rpm -e --nodeps mysql-community-server-5.7.37-1.el7.x86_64
[root@CentOS7-0002 lib]# rpm -e --nodeps mysql57-community-release-el7-10.noarch
警告:/etc/yum.repos.d/mysql-community.repo 已另存为 /etc/yum.repos.d/mysql-community.repo.rpmsave
[root@CentOS7-0002 lib]# rpm -e --nodeps mysql-community-common-5.7.37-1.el7.x86_64
[root@CentOS7-0002 lib]# rpm -e --nodeps mysql-community-client-5.7.37-1.el7.x86_64
[root@CentOS7-0002 lib]#
2.3、查看yum安装mysql
[root@CentOS7-0002 lib]# yum list install mysql*
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
错误:没有匹配的软件包可以列出
[root@CentOS7-0002 lib]#
2.4、卸载对应的包
[root@CentOS7-0002 lib]# yum remove mysql-community-release
2.5、查看并删除对应的目录
[root@CentOS7-0002 lib]# whereis mysql
mysql: /usr/lib64/mysql /usr/share/mysql
[root@CentOS7-0002 lib]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/lib64/mysql
/usr/share/mysql
/data/mysql
[root@CentOS7-0002 lib]# rm -rf /usr/lib64/mysql
[root@CentOS7-0002 lib]# rm -rf /usr/share/mysql
[root@CentOS7-0002 lib]# rm -rf /etc/selinux/targeted/active/modules/100/mysql
[root@CentOS7-0002 lib]# rm -rf /var/lib/mysql/mysql
[root@CentOS7-0002 lib]# rm -rf /var/lib/mysql
[root@CentOS7-0002 lib]# rm -rf /data/mysql
2.6、删除配置文件
[root@CentOS7-0002 lib]# rm -rf /root/.mysql_sercret
[root@CentOS7-0002 lib]# rm -rf /etc/my.cnf.rpmsave
[root@CentOS7-0002 lib]# rm -rf /var/log/mysqld.log
2.7、查看组和用户并删除
[root@CentOS7-0002 lib]# cat /etc/group
[root@CentOS7-0002 lib]# cat /etc/passwd
[root@CentOS7-0002 lib]# groupdel mysql
[root@CentOS7-0002 lib]# userdel mysql
至此MySql已完全卸载成功。
三、安装问题处理
3.1、在CentOS上使用yum安装MySQL时检索密钥错误的解决方法
编辑文件
/etc/yum.repos.d/mysql-community.repo
修改对应安装版本的gpgcheck值默认1改成0,再次重新安装。
3.2、登录时报这样的错:ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2)
原因是/var/lib/mysql的访问权限问题。下面的命令把/var/lib/mysql的拥有者改为当前用户:
[root@CentOS7-0002 software]# mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@CentOS7-0002 software]# sudo chown -R mysql:mysql /var/lib/mysql
[root@CentOS7-0002 software]#
[root@CentOS7-0002 software]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
[root@CentOS7-0002 software]# mysql -u root
然后,重启服务,重新进入
3.3、出现Delta RPMs disabled because /usr/bin/applydeltarpm not installed错误
解决方案:
yum provides ‘*/applydeltarpm’ #查看依赖包的位置
yum -y install deltarpm #安装命令