安装MySQL
注意:安装MySQL需要root权限
在企业中MySQL 5.7和MySQL 8.x版本使用都较为广泛,因此本教程使用yum命令安装MySQL 5.7和MySQL 8.x到CentOS系统。
MySQL 5.7和MySQL 8.x版本大部分安装过程相同,仅有少部分配置不同,因此放在一起说明,请注意MySQL 5.7和MySQL 8.x的不同之处。
配置yum仓库
由于MySQL并不在CentOS的官方仓库中,所以进行两个操作,如下:
- 导入MySQL仓库的密钥
- 配置MySQL的yum仓库
注意:MySQL 5.7版本和MySQL 8.x版本操作不同
MySQL 5.7版本命令说明:
# 更新密钥 rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 # 安装MySQL 5.7 yum库 rpm -Uvh http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
MySQL 5.7版本操作示例:
[root@cxypa ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 [root@cxypa ~]# rpm -Uvh http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm 获取http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm 警告:/var/tmp/rpm-tmp.z15a2i: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql57-community-release-el7-7 ################################# [100%] [root@cxypa ~]#
MySQL 8.x版本命令说明:
# 更新密钥 rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 # 安装MySQL 8.x版本 yum库 rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm
MySQL 8.x版本操作示例:
[root@cxypa ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 [root@cxypa ~]# rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm 获取https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm 警告:/var/tmp/rpm-tmp.TSu4K0: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql80-community-release-el7-2 ################################# [100%] [root@cxypa ~]#
使用yum安装MySQL
命令说明:
# yum安装MySQL yum -y install mysql-community-server
操作示例:
[root@cxypa ~]# yum -y install mysql-community-server 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.sjtu.edu.cn * extras: ftp.sjtu.edu.cn * updates: ftp.sjtu.edu.cn # 省略其他 总下载量:213 M Downloading packages: Delta RPMs disabled because /usr/bin/applydeltarpm not installed. (1/6): mysql-community-common-5.7.40-1.el7.x86_64.rpm | 311 kB 00:00:00 (2/6): mysql-community-libs-5.7.40-1.el7.x86_64.rpm | 2.6 MB 00:00:00 (3/6): mysql-community-libs-compat-5.7.40-1.el7.x86_64.rpm | 1.2 MB 00:00:00 (4/6): mysql-community-client-5.7.40-1.el7.x86_64.rpm | 28 MB 00:00:01 (5/6): postfix-2.10.1-9.el7.x86_64.rpm | 2.4 MB 00:00:00 (6/6): mysql-community-server-5.7.40-1.el7.x86_64.rpm | 178 MB 00:00:07 -------------------------------------------------------------------------------------- 总计 24 MB/s | 213 MB Running transaction 警告:RPM 数据库已被非 yum 程序修改。 正在安装 : mysql-community-common-5.7.40-1.el7.x86_64 1/8 正在安装 : mysql-community-libs-5.7.40-1.el7.x86_64 2/8 正在安装 : mysql-community-client-5.7.40-1.el7.x86_64 3/8 已安装: mysql-community-libs.x86_64 0:5.7.40-1.el7 mysql-community-libs-compat.x86_64 0:5.7.40-1.el7 mysql-community-server.x86_64 0:5.7.40-1.el7 作为依赖被安装: mysql-community-client.x86_64 0:5.7.40-1.el7 mysql-community-common.x86_64 0:5.7.40-1.el7 作为依赖被升级: postfix.x86_64 2:2.10.1-9.el7 替代: mariadb-libs.x86_64 1:5.5.60-1.el7_5 完毕! [root@cxypa ~]#
到此完成了MySQL的安装,接下来需要启动MySQL并配置开机自启动等一系列MySQL软件自身的配置。
启动MySQL
安装好MySQL后,MySQL会自动配置一个名为mysqld
的服务,需要手动启动这个服务。
命令说明:
systemctl start mysqld # 启动MySQL服务 systemctl status mysqld # 查看服务状态
操作示例:
[root@cxypa ~]# systemctl start mysqld [root@cxypa ~]# systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since 六 2022-12-24 13:51:14 CST; 24s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 21026 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 20911 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 21029 (mysqld) CGroup: /system.slice/mysqld.service └─21029 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid 12月 24 13:51:10 cxypa systemd[1]: Starting MySQL Server... 12月 24 13:51:14 cxypa systemd[1]: Started MySQL Server. [root@cxypa ~]#
配置MySQL开机自启动
默认情况需要我们每次开机手动启动MySQL服务,这样操作比较麻烦,给MySQL配置开机自动启动服务。
命令说明:
systemctl enable mysqld # 开机自动启动MySQL服务
操作示例:
[root@cxypa ~]# systemctl enable mysqld [root@cxypa ~]#
没有显示任何错误,说明设置开机自动启动MySQL服务成功。
获取MySQL的初始密码
MySQL首次安装后会把初始的随机密码
保存在/var/log/mysqld.log
文件中。我们需要去这个文件中得到默认密码登录MySQL数据库。
命令说明:
# 使用grep命令在/var/log/mysqld.log文件中找到初始密码 grep 'temporary password' /var/log/mysqld.log
操作示例:
[root@cxypa ~]# grep 'temporary password' /var/log/mysqld.log 2022-12-24T05:51:11.620251Z 1 [Note] A temporary password is generated for root@localhost: eN(2DN4%tbwd [root@cxypa ~]#
效果说明:
root@localhost:
后面的内容就是初始密码,说明默认密码为eN(2DN4%tbwd
。
登陆MySQL数据库系统
通过mysql -uroot -p密码
就可以登录MySQL数据库了。
命令说明:
mysql -uroot -p密码 # -u:表示登陆的用户,MySQL数据库的管理员用户默认是root # -p:表示使用密码登陆
操作示例:
[root@cxypa ~]# 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.40 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>
效果说明:
看到mysql>
就说明登录MySQL成功了!
修改root用户密码
注意:MySQL 5.7版本和MySQL 8.x版本操作不同
MySQL 5.7版本命令说明:
# 在MySQL控制台内执行 ALTER USER 'root'@'localhost' IDENTIFIED BY '密码'; # MySQL5.7对密码的强度要求比较高,需要包含大写字母和特殊符号长度8位以上,使用简单的密码会出错。
MySQL 5.7版本操作示例:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'CXYhao123$'; Query OK, 0 rows affected (0.00 sec) mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql>
MySQL 8.x版本命令说明:
# 在MySQL控制台内执行 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码'; # MySQL8.x对密码的强度要求比较高,需要包含大写字母和特殊符号长度8位以上,使用简单的密码会出错。
MySQL 8.x版本操作示例:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'CXYhao123$'; Query OK, 0 rows affected (0.00 sec) mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
配置root允许简单密码
为了学习方便我们可以需要给root设置简单密码如root
。
注意:MySQL 5.7版本和MySQL 8.x版本操作不同
MySQL 5.7版本安装命令说明:
set global validate_password_policy=LOW; # 设置密码安全级别为低 set global validate_password_length=4; # 设置密码长度最低4位
MySQL 5.7版本安装操作示例:
mysql> set global validate_password_policy=LOW; Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_length=4; Query OK, 0 rows affected (0.01 sec) mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root'; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye [root@cxypa ~]# mysql -uroot -proot mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.40 MySQL Community Server (GPL) 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>
效果说明:
上面设置密码安全级别为低和设置密码长度最低4位,把MySQL的密码设置为root
,并退出MySQL,使用修改后的root
密码登录。
MySQL 8.x版本命令说明:
set global validate_password.policy=0; # 设置密码安全级别为低 set global validate_password.length=4; # 设置密码长度最低4位 set global validate_password.check_user_name=0; # 设置允许账号密码相同
MySQL 8.x版本操作示例:
mysql> set global validate_password.policy=0; Query OK, 0 rows affected (0.01 sec) mysql> set global validate_password.length=4; Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password.check_user_name=0; Query OK, 0 rows affected (0.00 sec) mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; Query OK, 0 rows affected (0.00 sec) mysql>
效果说明:
上面设置密码安全级别为低和设置密码长度最低4位,把MySQL的密码设置为root
,并退出MySQL,使用修改后的root
密码登录。
查看密码安全
命令说明:
show variables Like 'validate_password.%';
操作示例:
mysql> Show Variables Like 'validate_password.%'; +--------------------------------------+-------+ | Variable_name | Value | +--------------------------------------+-------+ | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 4 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | LOW | | validate_password.special_char_count | 1 | +--------------------------------------+-------+ 7 rows in set (0.01 sec)
效果说明:
validate_password.length
:需要密码最小字符数,默认为8validate_password.number_count
:需要密码的最小数字字符数,默认为1validate_password.mixed_case_count
:需要密码的小写和大写的最小字符数,默认为1validate_password.special_char_count
:需要密码的特殊字符的最小字符数,默认为1validate_password.dictionary_file
:用于检查密码的字典文件的路径名,默认没有
配置root远程登录
MySQL默认提供的root账号只能进行本地登录,也就是使用root账户只能在安装MySQL的系统上才能登录。如果要从其他电脑登录MySQL需要配置root账户允许远程登录。
注意:MySQL 5.7版本和MySQL 8.x版本操作不同
MySQL 5.7版本安装命令说明:
grant all privileges on *.* to root@"IP地址" identified by '密码' with grant option; # 授权root远程登录 # IP地址即允许用户登陆的主机IP地址,也可以填写%,表示允许任何主机远程登录 # 密码表示给远程登录独立设置密码,可以和本地登陆的密码不同 flush privileges; # 刷新权限
MySQL 5.7版本安装操作示例:
mysql> grant all privileges on *.* to root@"%" identified by 'root' 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 8.x版本命令说明:
# 第一次设置root远程登录并配置远程密码 create user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码'; # 后续修改密码使用如下SQL命令 ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
MySQL 8.x版本操作示例:
mysql> create user 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root'; Query OK, 0 rows affected (0.00 sec) mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root'; Query OK, 0 rows affected (0.01 sec)
配置防火墙允许远程访问3306端口
由于CentOS7默认开启了防火墙,远程连接被拦截了,需要打开3306的端口号,才能进行MySQL远程连接。
命令说明:
# 显示现有的规则 firewall-cmd --list-all # 开放的端口永久保存到防火墙 firewall-cmd --zone=public --add-port=3306/tcp --permanent # 重启防火墙 systemctl restart firewalld
操作示例:
[root@cxypa ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@cxypa ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent success [root@cxypa ~]# systemctl restart firewalld [root@cxypa ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh dhcpv6-client ports: 3306/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@cxypa ~]#
卸载MySQL5.7
- 停止MySQL服务
systemctl stop mysqld
- 查看MySQL安装了哪些东西
rpm -qa | grep -i mysql
操作示例:
[root@cxypa ~]# rpm -qa |grep -i mysql mysql-community-client-5.7.40-1.el7.x86_64 mysql57-community-release-el7-7.noarch mysql-community-libs-5.7.40-1.el7.x86_64 mysql-community-libs-compat-5.7.40-1.el7.x86_64 mysql-community-server-5.7.40-1.el7.x86_64 mysql-community-common-5.7.40-1.el7.x86_64 [root@cxypa ~]#
- 开始卸载
操作示例:
[root@cxypa ~]# yum remove mysql-community-client-5.7.40-1.el7.x86_64 [root@cxypa ~]# yum remove mysql57-community-release-el7-7.noarch [root@cxypa ~]# yum remove mysql-community-libs-5.7.40-1.el7.x86_64 [root@cxypa ~]# yum remove mysql-community-libs-compat-5.7.40-1.el7.x86_64 [root@cxypa ~]# yum remove mysql-community-server-5.7.40-1.el7.x86_64 [root@cxypa ~]# yum remove mysql-community-common-5.7.40-1.el7.x86_64
- 查看是否卸载完成
[root@cxypa ~]# rpm -qa |grep -i mysql [root@cxypa ~]#
- 查找MySQL相关目录
find / -name mysql
操作示例:
[root@cxypa ~]# find / -name mysql /etc/selinux/targeted/active/modules/100/mysql /var/lib/mysql /var/lib/mysql/mysql /usr/share/mysql [root@cxypa ~]#
- 删除相关目录
操作示例:
[root@cxypa ~]# find / -name mysql /etc/selinux/targeted/active/modules/100/mysql /var/lib/mysql /var/lib/mysql/mysql /usr/share/mysql [root@cxypa ~]# rm -fr /etc/selinux/targeted/active/modules/100/mysql [root@cxypa ~]# rm -fr /var/lib/mysql [root@cxypa ~]# rm -fr /var/lib/mysql/mysql [root@cxypa ~]# rm -fr /usr/share/mysql [root@cxypa ~]#
- 删除/etc/my.cnf
操作示例:
[root@cxypa ~]# rm -rf /etc/my.cnf
- 删除/var/log/mysqld.log(如果不删除这个文件,会导致新安装的MySQL无法生成新密码,导致无法登陆)
操作示例:
rm -rf /var/log/mysqld.log
到此就完成了MySQL的卸载。