2024-03-11
Databases
00
请注意,本文编写于 36 天前,最后修改于 35 天前,其中某些信息可能已经过时。

目录

一、安装MySQL
1.1 二进制
安装MySQL
设置开机启动
1.2 YUM安装
1.3 离线RPM安装
安装MySQL
配置MySQL
二、卸载MySQL
2.1 卸载二进制文件安装
2.2 卸载YUM安装

一、安装MySQL

1.1 二进制

安装MySQL

下载地址:https://downloads.mysql.com/archives/community/

相关信息

Product Version:5.7.37 (选择需要的安装的版本) Operating System:Linux - Generic OS Version:Linux - Generic (glibc 2.12) (x86, 64-bit)

1、安装依赖

yum -y install libaio yum -y install autoconf

2、上传到服务器并解压

tar -xvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

3、移动并重命名一下

mv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql

4、创建mysql用户组和用户并修改权限

groupadd mysql useradd -r -g mysql mysql

5、创建数据目录并赋予权限

mkdir -p /data/mysql #创建目录 chown mysql:mysql -R /data/mysql #赋予权限

6、修改环境变量:

vim /etc/profile # 文档末尾添加一行: export PATH=/usr/local/mysql/bin:$PATH # 使配置生效 source /etc/profile

7、修改配置文件:vi /etc/my.cnf

[mysqld] bind-address=0.0.0.0 port=3306 user=mysql basedir=/usr/local/mysql datadir=/data/mysql socket=/tmp/mysql.sock plugin-load-add=validate_password.so validate-password=FORCE_PLUS_PERMANENT [mysqld_safe] log-error=/data/mysql/mysql.err pid-file=/data/mysql/mysql.pid #character config character_set_server=utf8mb4 symbolic-links=0 explicit_defaults_for_timestamp=true lower_case_table_names=1

8、初始化数据库

cd /usr/local/mysql/bin/ ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize

9、将mysql.server放置到/etc/init.d/mysql中

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

10、启动

service mysql start

11、创建软连接

ln -s /usr/local/mysql/bin/mysql /usr/bin

12、登录mysql:mysql -u root -p密码为前面初始化提示的密码

13、设置复杂密码(由于MySQL密码策略,此密码必须足够复杂)

mysql> set password=password("Qs23=zs32");

14、更改MySQL密码策略

mysql> set global validate_password_length=4; mysql> set global validate_password_policy=0;

15、设置简单好记的密码

mysql> set password=password("000000");

16、进入MySQL库

mysql> use mysql

17、查询user表

mysql> select user, host from user;

18、修改user表,把Host表内容修改为%

mysql> update user set host="%" where user="root";

19、刷新

mysql> flush privileges;

20、退出

mysql> quit;

设置开机启动

1、将服务文件拷贝到init.d下,并重命名为mysql

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

2、赋予可执行权限

chmod +x /etc/init.d/mysqld

3、添加服务

chkconfig --add mysqld

4、显示服务列表

chkconfig --list

1.2 YUM安装

1、更新yum

yum update -y

这里会更新好多内容,需要等待一会。

2、安装wget工具

yum install -y wget

3、使用wget下载mysql yum源

wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

4、添加 mysql yum 源

yum localinstall mysql80-community-release-el7-3.noarch.rpm -y

5、安装 yum 工具 yum-utils

yum install -y yum-utils

6、查看可用的 mysql

yum repolist enabled | grep "mysql.*-community.*"
[root@hadoop101 mysql]# yum repolist enabled | grep "mysql.*-community.*" mysql-connectors-community/x86_64 MySQL Connectors Community 234 mysql-tools-community/x86_64 MySQL Tools Community 102 mysql80-community/x86_64 MySQL 8.0 Community Server 446

这里默认mysql8.0

7、查看所有的 mysql 版本

yum repolist all | grep mysql
[root@hadoop101 mysql]# yum repolist all | grep mysql mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community 禁用 mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - 禁用 mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community 禁用 mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - 禁用 mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community 禁用 mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - 禁用 mysql-connectors-community/x86_64 MySQL Connectors Community 启用: 234 mysql-connectors-community-source MySQL Connectors Community - S 禁用 mysql-tools-community/x86_64 MySQL Tools Community 启用: 102 mysql-tools-community-source MySQL Tools Community - Source 禁用 mysql-tools-preview/x86_64 MySQL Tools Preview 禁用 mysql-tools-preview-source MySQL Tools Preview - Source 禁用 mysql55-community/x86_64 MySQL 5.5 Community Server 禁用 mysql55-community-source MySQL 5.5 Community Server - S 禁用 mysql56-community/x86_64 MySQL 5.6 Community Server 禁用 mysql56-community-source MySQL 5.6 Community Server - S 禁用 mysql57-community/x86_64 MySQL 5.7 Community Server 禁用 mysql57-community-source MySQL 5.7 Community Server - S 禁用 mysql80-community/x86_64 MySQL 8.0 Community Server 禁用 mysql80-community-source MySQL 8.0 Community Server - S 禁用

8、使用指定版本MySQL
假如我想使用MySQL5.7,那么我就需要先关闭MySQL8.0
关闭MySQL8.0

yum-config-manager --disable mysql80-community

开启MySQL5.7

yum-config-manager --enable mysql57-community

9、查看当前启用的MySQL版本

yum repolist enabled | grep mysql
yum repolist enabled | grep mysql mysql-connectors-community/x86_64 MySQL Connectors Community 234 mysql-tools-community/x86_64 MySQL Tools Community 102 mysql57-community/x86_64 MySQL 5.7 Community Server 696

10、安装MySQL

yum install -y mysql-community-server

期间等待很长时间,等待安装完成。,如果下载速度只有几十kb,请更换网络,或者使用手机热点进行下载。更换热点后,再次执行此命令即可。

yum install -y mysql-community-server

11、启动MySQL

service mysqld start

12、查看MySQL服务状态

service mysqld status

13、初始化MySQL 查看初始化密码:

grep 'temporary password' /var/log/mysqld.log

使用初始密码进行登录:

mysql -u root -p

初始化密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'daasan7ujm^YHN';

14、设置MySQL密码策略 查询MySQL初始的密码策略

SHOW VARIABLES LIKE 'validate_password%';
mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | validate_password_check_user_name | OFF | | validate_password_dictionary_file | | | validate_password_length | 8 | | validate_password_mixed_case_count | 1 | | validate_password_number_count | 1 | | validate_password_policy | MEDIUM | | validate_password_special_char_count | 1 | +--------------------------------------+--------+ 7 rows in set (0.00 sec)

修改密码验证强度

set global validate_password_policy=LOW;

修改密码长度

set global validate_password_length=6;

此时密码已经可以设置简单密码了。

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

注意,密码长度最低为4位,设置的密码长度小于4时,默认改成4. 15、设置MySQL远程连接,在sql里面设置

sql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

这里的密码时你自己的设置的密码

刷新权限

FLUSH PRIVILEGES;

16、设置 MySQL开机启动,在linux里面设置 不在sql里面配置,在Linux命令行执行。!!!不是在sql里面

systemctl enable mysqld (这只是提示目录所在)MySQL的配置文件目录: /etc/my.cnf

17、配置防火墙
设置3306端口开放

firewall-cmd --zone=public --add-port=3306/tcp --permanent

看到success表示添加成功。

重启防火墙

firewall-cmd --reload

看到success表示添加成功。

验证3306是否开放成功

firewall-cmd --zone=public --query-port=3306/tcp

1.3 离线RPM安装

安装MySQL

1、安装包准备
将安装包上传到服务器并解压

[root@localhost ~]# ll 总用量 533104 -rw-r--r--. 1 root root 545894400 3月 11 19:15 mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar [root@localhost ~]# tar -xvf mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar mysql-community-client-5.7.37-1.el7.x86_64.rpm mysql-community-common-5.7.37-1.el7.x86_64.rpm mysql-community-devel-5.7.37-1.el7.x86_64.rpm mysql-community-embedded-5.7.37-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.37-1.el7.x86_64.rpm mysql-community-embedded-devel-5.7.37-1.el7.x86_64.rpm mysql-community-libs-5.7.37-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm mysql-community-server-5.7.37-1.el7.x86_64.rpm mysql-community-test-5.7.37-1.el7.x86_64.rpm

2、如果是虚拟机按照如下步骤执行
卸载自带的Mysql-libs(如果之前安装过MySQL,要全都卸载掉)

rpm -qa | grep -i -E mysql\|mariadb | xargs -n1 sudo rpm -e --nodeps

3、如果是阿里云服务器按照如下步骤执行
说明:由于阿里云服务器安装的是Linux最小系统版,没有如下工具,所以需要安装。 (1)卸载MySQL依赖,虽然机器上没有装MySQL,但是这一步不可少

yum remove mysql-libs

(2)下载依赖并安装

yum -y install libaio yum -y install autoconf

4、安装MySQL依赖

rpm -ivh mysql-community-common-5.7.37-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-5.7.37-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm 20 21 22

5、安装mysql-client

rpm -ivh mysql-community-client-5.7.37-1.el7.x86_64.rpm

6、安装mysql-server

rpm -ivh mysql-community-server-5.7.37-1.el7.x86_64.rpm

注意:如果报如下错误,这是由于yum安装了旧版本的GPG keys所造成,从rpm版本4.1后,在安装或升级软件包时会自动检查软件包的签名。

warning: 05_mysql-community-server-5.7.37-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY error: Failed dependencies: libaio.so.1()(64bit) is needed by mysql-community-server-5.7.37-1.el7.x86_64

解决办法

rpm -ivh mysql-community-server-5.7.37-1.el7.x86_64.rpm --force --nodeps

7、启动MySQL

systemctl start mysqld

8、查看MySQL密码

cat /var/log/mysqld.log | grep password

配置MySQL

配置只要是root用户 + 密码,在任何主机上都能登录MySQL数据库。 1、用刚刚查到的密码进入MySQL(如果报错,给密码加单引号)

mysql -uroot -p'password'

2、设置复杂密码(由于MySQL密码策略,此密码必须足够复杂)

mysql> set password=password("Qs23=zs32");

3、更改MySQL密码策略

mysql> set global validate_password_length=4; mysql> set global validate_password_policy=0;

4、设置简单好记的密码

mysql> set password=password("000000");

5、进入MySQL库

mysql> use mysql

6、查询user表

mysql> select user, host from user;

7、修改user表,把Host表内容修改为%

mysql> update user set host="%" where user="root";

8、刷新

mysql> flush privileges;

9、退出

mysql> quit;

二、卸载MySQL

2.1 卸载二进制文件安装

1、关闭MySQL服务

systemctl stop mysql.service

2、查找MySQL对应的所有文件夹

bash
find / -name mysql rm -rf [显示的文件夹路径] # 检查mysql是否删除干净 whereis mysql

3、删除mysql配置文件

rm -rf /etc/my.cnf

4、删除相关文件

rm -rf /etc/init.d/mysqld

2.2 卸载YUM安装

1、筛选过滤出mysql相关组件

rpm -qa | grep mysql

2、关闭MySQL服务

systemctl stop mysql.service

3、卸载已安装的所有mysql组件

bash
rpm -qa | grep mysql # 卸载对应组件命令如下: rpm -ev --nodeps [显示的组件名称]

4、查找MySQL对应的所有文件夹

bash
find / -name mysql rm -rf [显示的文件夹路径] # 检查mysql是否删除干净 whereis mysql

5、删除mysql用户及用户组

bash
# 使用命令查看指定文件夹下的mysql more /etc/passwd | grep mysql more /etc/shadow | grep mysql more /etc/group | grep mysql # 使用命令删除mysql用户和用户组 userdel mysql groupdel mysql

删除完成后再次检查文件夹mysql信息是否清空

如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:XiaoWang0777

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

本网站由 又拍云Logo 提供CDN加速/云存储服务 萌ICP备20240377号