Centos下安装mysql8.0详细步骤
本次安装mysql的centos版本是8
[root@hhzz ~]# cat /etc/centos-release
CentOS Stream release 8
目录
1. 下载Mysql
首先去Mysql官网下载安装包,网址https://dev.mysql.com/downloads/mysql/
推荐大家下载Linux通用版本的,便于管理安装位置,也方便一台服务器安装多个版本的mysql,下载后将Mysql安装包上传至服务器/opt/soft目录下
2. 创建Mysql用户和组
[root@hhzz ~]# groupadd mysql[root@hhzz ~]# useradd -r -g mysql mysql[root@hhzz ~]# passwd mysql
更改用户 mysql 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@hhzz ~]#
3. 上传mysql安装包并解压
[root@hhzz ~]# mkdir -p /opt/soft 创建工具包文件目录并将mysql8.2安装包上传至该路径下[root@hhzz ~]# cd /opt/soft/[root@hhzz soft]# ll
总用量 461528
-rw-r--r-- 1 root root 47260121612月 9 00:06 mysql-8.2.0-linux-glibc2.28-x86_64.tar.xz
[root@hhzz soft]# tar -xf mysql-8.2.0-linux-glibc2.28-x86_64.tar.xz 解压mysql[root@hhzz soft]# ll
总用量 461528
drwxr-xr-x 9 root root 12912月 9 00:07 mysql-8.2.0-linux-glibc2.28-x86_64
-rw-r--r-- 1 root root 47260121612月 9 00:06 mysql-8.2.0-linux-glibc2.28-x86_64.tar.xz
[root@hhzz soft]# mkdir /usr/local/mysql8.0 -p 创建mysql安装路径,并将soft目录下解压之后的mysql文件包移至该路径下并重命名为mysql8.2[root@hhzz local]# mv mysql-8.2.0-linux-glibc2.28-x86_64 mysql8.2
4. 创建Mysql数据目录并更改目前权限
############# 更改mysql目录下所有文件夹所属的组和用户以及权限[root@hhzz local]# chown -R mysql:mysql mysql8.2[root@hhzz local]# [root@hhzz local]# chmod 755 mysql8.2############# 创建mysql相关目录[root@hhzz local]# mkdir -p /data/mysql/{data,logs,tmp}[root@hhzz local]# chown -R mysql:mysql /data/mysql/
5. 配置my.cnf文件
创建mysql配置文件my.cnf
[root@hhzz local]# vim /etc/my.cnf
# 简单模板[mysqld]port=3306basedir=/usr/local/mysql8.2
datadir=/data/mysql/data
log-error=/data/mysql/logs/error.log
socket=/data/mysql/tmp/mysql.sock
pid-file=/data/mysql/tmp/mysql.pid
character-set-server = utf8
lower_case_table_names=1innodb_log_file_size=1G
default-storage-engine=INNODB
# default_authentication_plugin=mysql_native_password[client]port=3306
default-character-set=utf8
配置mysql.server
[root@hhzz local]# vim mysql8.2/support-files/mysql.server ########## 修改目录位置basedir=/usr/local/mysql8.2
datadir=/data/mysql/data
6. 初始化Mysql
######## 初始化mysql[root@hhzz mysql8.2]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql8.2 --datadir=/data/mysql/data######## 获取临时密码[root@hhzz mysql8.2]# more /data/mysql/logs/error.log | grep password2023-12-11T11:18:11.973152Z 6[Note][MY-010454][Server] A temporary password is generated for root@localhost: rdaudLd/e44U
7. 启动&登录Mysql&修改root密码
[root@hhzz mysql8.2]# ./support-files/mysql.server start
Starting MySQL.. SUCCESS! `
[root@hhzz mysql8.2]# ./bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.2.0
Copyright (c)2000, 2023, 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 'root';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
8. 配置Mysql开机自启
8.1 设置mysql环境变量
root@hhzz mysql8.2]# vim /etc/profile###### 添加如下内容:# mysql-envexportMYSQL_HOME=/usr/local/mysql8.2
exportPATH=$MYSQL_HOME/bin:$PATH###### 使增加内容生效[root@hhzz mysql8.2]# source /etc/profile
8.2 设置mysql服务为系统服务
[root@hhzz mysql8.2]# cp support-files/mysql.server /etc/init.d/mysql##### 然后重启服务即可使用如下命令查看mysql服务[root@hhzz ~]# systemctl status mysql
● mysql.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysql; generated)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)[root@hhzz ~]# [root@hhzz ~]# [root@hhzz ~]# [root@hhzz ~]# systemctl start mysql^C[root@hhzz ~]# [root@hhzz ~]# systemctl stop mysql
8.3 设置mysql服务开机自启
将MYSQL8服务加入开机自启动,创建文件/etc/systemd/system/mysqld.service 示例如下:
[root@hhzz ~]# cat /etc/systemd/system/mysql.service [Unit]Description=Mysql Server
Documentation=man:mysqld(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Service]User=mysql
Group=mysql
PIDFile=/run/nginx.pid
#修改成mysql的启动文件及配置文件路径ExecStart=/usr/local/mysql8.2/bin/mysqld --defaults-file=/etc/my.cnf
[Install]WantedBy=multi-user.target
######### 查看mysql服务是否设置了开机启动[root@luxl ~]# systemctl is-enabled mysql
enabled
9. 通过工具连接mysql
10. 后续补充及问题解决记录
10.1 后续补充
**1. mysql8创建用户及赋权 **
# 创建一个不限制IP登录的用户hhzz
mysql> create user 'hhzz'@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
使用工具连接该数据库用户后发现只有information_schema与performance_schema两个个数据库
给用户赋权
### 给用户hhzz赋予数据库mysql中所有表的权限### with grant option: 表示该用户可以给其他用户赋权,但是不能超过该用户的权限
mysql> grant all privileges on mysql.* to 'hhzz'@'%' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
查看、撤销用户权限
##### 查看用户权限
mysql> show grants for'hhzz'@'%';
+-------------------------------------------------------------------+
| Grants for hhzz@% |
+-------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `hhzz`@`%`|| GRANT ALL PRIVILEGES ON `mysql`.* TO `hhzz`@`%` WITH GRANT OPTION || GRANT ALL PRIVILEGES ON `root`.* TO `hhzz`@`%` WITH GRANT OPTION |
+-------------------------------------------------------------------+
3 rows inset(0.00 sec)##### 撤销用户hhzz对mysql数据库的操作权限,不过这样撤销连接数据库仍然可以看到Mysql数据库,
但是无法操作;原因是之前赋权时用到了with grant option,因为all privileges 是除了with grant option 的所有权限。
mysql> revoke all privileges on mysql.* from 'hhzz'@'%';
Query OK, 0 rows affected (0.00 sec)##### 可执行如下语句,回收用户所有权限
mysql> revoke all privileges,grant option from 'hhzz'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> show grants for'hhzz'@'%';
+----------------------------------+
| Grants for hhzz@% |
+----------------------------------+
| GRANT USAGE ON *.* TO `hhzz`@`%`|
+----------------------------------+
1 row inset(0.00 sec)
删除用户
mysql> drop user 'hhzz'@'%';
Query OK, 0 rows affected (0.02 sec)
mysql>select user,host from user;
+------------------+-----------+
| user |host|
+------------------+-----------+
| root | % || mysql.infoschema | localhost || mysql.session | localhost || mysql.sys | localhost |
+------------------+-----------+
4 rows inset(0.00 sec)
2. 【MySQL密码策略查询为空】validate_password 插件安装
查看mysql密码策略返回为空,原因是没有安装validate_password插件。
mysql> show variables like 'vadidate_password%';
Empty set(0.00 sec)###### 笔者这边使用的是 8.2.0 版本,其不具备密码策略查看的插件,具体代码如下所示。
mysql>select version();
+-----------+
| version()|
+-----------+
|8.2.0 |
+-----------+
1 row inset(0.00 sec)###### 安装validate_password插件
mysql> INSTALL COMPONENT 'file://component_validate_password';
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> show variables like 'validate_password%';
+-------------------------------------------------+--------+
| Variable_name | Value |
+-------------------------------------------------+--------+
| validate_password.changed_characters_percentage |0|| validate_password.check_user_name | ON || 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|
+-------------------------------------------------+--------+
8 rows inset(0.01 sec)
**3. mysql8 修改密码策略 **
临时修改,重新失效
mysql>
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.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> show variables like 'validate_password%';
+-------------------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------------------+-------+
| validate_password.changed_characters_percentage |0|| 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|
+-------------------------------------------------+-------+
8 rows inset(0.01 sec)
永久修改
[root@luxl ~]# vim /etc/my.cnf # 添加如下两个配置,重启永久生效# 修改密码策略
validate_password.policy = LOW
validate_password.length =4
** 注意: 如果一开始未安装validate_password插件时未受到mysql8密码策略限制,可以随意使用密码,一旦安装后就必须根据密码策略使用密码。**
10.2 问题解决记录
1. 【mysql报错】ERROR 2002 (HY000) Can‘t connect to local MySQL server through socket ‘tmpmysql
明白mysql.sock文件的作用
mysql 支持 socket 和 TCP/IP 连接。那么 mysql.sock 这个文件有什么用呢?连接localhost通常通过一个Unix域套接字文件进行,一般是/tmp/mysql.sock。如果套接字文件被删除了,本地客户就不能连接。/tmp 文件夹属于临时文件,随时可能被删除。
解决问题
# 可以看到我的mysql已经声明了mysql.sock的文件位置,而错误提示为/tmp/mysql.sock; 也就是说虽然我的mysql声明了sock文件的位置,但是客户端在连接的时候, linux系统总是去/tmp目录下去找,找到了原因,就可以对症下药[root@hhzz ~]# vim /etc/my.cnf socket=/data/mysql/tmp/mysql.sock
# 为mysql.sock创建链接文件,然后再次连接mysql,成功连接[root@luxl mysql]# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
2. 远程连接mysql报错:1130 - Host XXX is not allowed to connect to this MySQL server
问题分析
根据描述提示,在用Navicat配置远程连接Mysql数据库时遇到如下报错信息,这是由于Mysql配置了不支持远程连接引起的。为了安全性, 默认情况下,mysql帐号不允许从远程登陆,只能在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
# 修改root用户不限制IP登录即可。
mysql> update user sethost='%' where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
11. 参考博客
[ Centos7使用systemctl管理mysql8服务开机启动
Mysql8怎么创建用户及赋权
MySQL密码策略查询为空 validate_password 插件安装
mysql忘记密码修改密码(mysql8)
mysql报错 ERROR 2002 (HY000) Can‘t connect to local MySQL server through socket ‘tmpmysql
远程连接mysql报错:1130 - Host XXX is not allowed to connect to this MySQL server
版权归原作者 慌慌张张的小路 所有, 如有侵权,请联系我们删除。