RedHat7.9安装mysql8.0.32 ↝ 二进制方式
- 一、rpm方式安装
- 1、检查是否安装了mariadb
- 2、下载mysqlmysql8.0.32
- 3、上传解压
- 4、创建安装目录,拷贝解压后的文件至安装目录/usr/local/mysql8.0/
- 5、创建相关目录,开始安装
- 6、创建mysql组和用户
- 7、更改安装目录归属
- 8、初始化MySQL
- 9、配置MySQL
- 10、启动MySQL
- 11、配置环境变量
- 12、修改root密码
- 13、安装、开启密码校验插件
- 14、修改密码规则(根据情况设置)
- 15、创建一个用户用于远程登录
- 16、设置mysql服务开机自启
- 二、总结—温故知新
一、rpm方式安装
1、检查是否安装了mariadb
检查linux中是否安装了mariadb,先卸载mariadb, 避免与安装mysql发生冲突。
[root-server ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root-server ~]#
卸载清除: rpm -e 文件名 或者 yum -y remove 文件名
[root-server ~]# rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps
[root-server ~]#
[root-server ~]# rpm -qa|grep mariadb
或者使用:
yum -y remove mariadb-libs-5.5.68-1.el7.x86_64
2、下载mysqlmysql8.0.32
官网地址:https://downloads.mysql.com/archives/community/
3、上传解压
[root-server ~]# ll
total 982092
-rw-r--r--. 1 root root 1005660160 Jul 29 14:05 mysql-8.0.32-linux-glibc2.12-x86_64.tar
[root-server ~]# tar -zxvf mysql-8.0.32-linux-glibc2.12-x86_64.tar
[root-server ~]# ll
total 1964176
-rw-r--r--. 1 root root 1005660160 Jul 29 14:05 mysql-8.0.32-linux-glibc2.12-x86_64.tar
-rw-r--r--. 1 7155 31415 610315332 Dec 17 2022 mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
-rw-r--r--. 1 7155 31415 17127660 Dec 17 2022 mysql-router-8.0.32-linux-glibc2.12-x86_64.tar.xz
-rw-r--r--. 1 7155 31415 378207988 Dec 17 2022 mysql-test-8.0.32-linux-glibc2.12-x86_64.tar.xz
###继续解压tar.xz文件
[root-server ~]# tar -xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
4、创建安装目录,拷贝解压后的文件至安装目录/usr/local/mysql8.0/
[root-server ~]# mkdir /usr/local/mysql8.0
[root-server ~]# mv mysql-8.0.32-linux-glibc2.12-x86_64/* /usr/local/mysql8.0/
[root@db-server ~]# cd /usr/local/mysql8.0/
[root@db-server mysql8.0]# ll
total 296
drwxr-xr-x. 2 7161 31415 4096 Dec 17 2022 bin
drwxr-xr-x. 2 7161 31415 56 Dec 17 2022 docs
drwxr-xr-x. 3 7161 31415 4096 Dec 17 2022 include
drwxr-xr-x. 6 7161 31415 201 Dec 17 2022 lib
-rw-r--r--. 1 7161 31415 283374 Dec 16 2022 LICENSE
drwxr-xr-x. 4 7161 31415 30 Dec 17 2022 man
-rw-r--r--. 1 7161 31415 666 Dec 16 2022 README
drwxr-xr-x. 28 7161 31415 4096 Dec 17 2022 share
drwxr-xr-x. 2 7161 31415 77 Dec 17 2022 support-files
[root@db-server mysql8.0]#
5、创建相关目录,开始安装
可以根据自己安装规划进行创建,初始化MySQL时指定你创建的有关目录即可。
[root-server mysql8.0]# mkdir /usr/local/mysql8.0/data
[root-server mysql8.0]#
[root-server mysql8.0]# mkdir /usr/local/mysql8.0/log
[root-server mysql8.0]#
[root-server mysql8.0]# touch /usr/local/mysql8.0/log/mysqld.log
[root-server mysql8.0]#
[root-server mysql8.0]#
6、创建mysql组和用户
[root-server mysql8.0]# groupadd mysql
[root-server mysql8.0]#
[root-server mysql8.0]# useradd -r -g mysql mysql
[root-server mysql8.0]#
7、更改安装目录归属
chown -R mysql /usr/local/mysql8.0/
chgrp -R mysql /usr/local/mysql8.0/
或者使用:
chown -R mysql:mysql /usr/local/mysql8.0
[root-server mysql8.0]# chown -R mysql:mysql /usr/local/mysql8.0
[root-server mysql8.0]# ll
total 296
drwxr-xr-x. 2 mysql mysql 4096 Dec 17 2022 bin
drwxr-xr-x. 2 mysql mysql 6 Jul 29 14:18 data
drwxr-xr-x. 2 mysql mysql 56 Dec 17 2022 docs
drwxr-xr-x. 3 mysql mysql 4096 Dec 17 2022 include
drwxr-xr-x. 6 mysql mysql 201 Dec 17 2022 lib
-rw-r--r--. 1 mysql mysql 283374 Dec 16 2022 LICENSE
drwxr-xr-x. 2 mysql mysql 24 Jul 29 14:18 log
drwxr-xr-x. 4 mysql mysql 30 Dec 17 2022 man
-rw-r--r--. 1 mysql mysql 666 Dec 16 2022 README
drwxr-xr-x. 28 mysql mysql 4096 Dec 17 2022 share
drwxr-xr-x. 2 mysql mysql 77 Dec 17 2022 support-files
[root-server mysql8.0]#
8、初始化MySQL
[root-server mysql8.0]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql8.0/ --datadir=/usr/local/mysql8.0/data --initialize
2023-07-29T06:26:23.438791Z 0 [System] [MY-013169] [Server] /usr/local/mysql8.0/bin/mysqld (mysqld 8.0.32) initializing of server in progress as process 1858
2023-07-29T06:26:23.446856Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-07-29T06:26:23.935723Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-07-29T06:26:24.856531Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root: Rr8mh_%ysuqZ
[root-server mysql8.0]#
9、配置MySQL
解决---mysql初始化后未找到配置文件:
使用指令./mysql --help | grep my.cnf 查看配置文件的读取顺序,一般默认优先读取/etc/my.cnf,若/etc/my.cnf文件不存在则需要新建该文件。
#查询相关配置文件读取顺序
[root-server mysql8.0]# ./bin/mysql --help | grep my.cnf
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
[root-server mysql8.0]#
#直接在/etc下新建my.cnf文件,写入配置内容
[mysqld]
datadir=/usr/local/mysql8.0/data
basedir=/usr/local/mysql8.0
socket=/tmp/mysqld.sock
user=mysql
port=3306
character-set-server=utf8
[mysqld_safe]
log-error=/usr/local/mysql8.0/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/tmp/mysqld.sock
10、启动MySQL
[root-server ~]# cd /usr/local/mysql8.0/
[root-server mysql8.0]# ll
total 300
drwxr-xr-x. 2 mysql mysql 4096 Dec 17 2022 bin
drwxr-xr-x. 7 mysql mysql 4096 Jul 29 14:26 data
drwxr-xr-x. 2 mysql mysql 56 Dec 17 2022 docs
drwxr-xr-x. 3 mysql mysql 4096 Dec 17 2022 include
drwxr-xr-x. 6 mysql mysql 201 Dec 17 2022 lib
-rw-r--r--. 1 mysql mysql 283374 Dec 16 2022 LICENSE
drwxr-xr-x. 2 mysql mysql 24 Jul 29 14:18 log
drwxr-xr-x. 4 mysql mysql 30 Dec 17 2022 man
-rw-r--r--. 1 mysql mysql 666 Dec 16 2022 README
drwxr-xr-x. 28 mysql mysql 4096 Dec 17 2022 share
drwxr-xr-x. 2 mysql mysql 77 Dec 17 2022 support-files
[root-server mysql8.0]# cd support-files/
[root-server support-files]# ll
total 20
-rwxr-xr-x. 1 mysql mysql 1061 Dec 16 2022 mysqld_multi.server
-rw-r--r--. 1 mysql mysql 2027 Dec 17 2022 mysql-log-rotate
-rwxr-xr-x. 1 mysql mysql 10576 Dec 17 2022 mysql.server
[root-server support-files]# sh mysql.server start
Starting MySQL. SUCCESS!
[root-server support-files]#
查看MySQL服务是否启动成功:
[root-server support-files]# sh mysql.server status
SUCCESS! MySQL running (2604)
[root-server support-files]#
11、配置环境变量
[root-server support-files]# vi /etc/profile
添加内容:
export PATH=$PATH:/usr/local/mysql/bin
使配置生效:
[root-server support-files]# source /etc/profile
12、修改root密码
使用步骤8、初始化MySQL时产生的临时密码登录:Rr8mh_%ysuqZ
[root-server ~]# mysql -u root -p
修改mysql数据库密码报错:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement.
解决:先刷新权限表
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>alter user 'root'@'localhost' identified with mysql_native_password by 'Zyl##123456';
Query OK, 0 rows affected (0.01 sec)
授权root远程登录会报错:原因:因为 MySQL8.0 以上将创建账户和赋予权限分开了,需要用两个语句创建账号,赋予远程权限;root用户禁止远程登录了。
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'Zyl##123456' WITH GRANT OPTION' at line 1
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'Zyl##123456' WITH GRANT OPTION;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'Zyl##123456' WITH GRANT OPTION' at line 1
mysql>
13、安装、开启密码校验插件
未安装前、返回空集合:
mysql> SHOW VARIABLES LIKE 'validate_password%';
Empty set (0.00 sec)
mysql> select plugin_name, plugin_status from information_schema.plugins where plugin_name like 'validate%';
Empty set (0.00 sec)
mysql> install plugin validate_password soname 'validate_password.so';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql>
安装密码校验插件之后:
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| 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 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
mysql>
14、修改密码规则(根据情况设置)
###有关参数详解
1、validate_password_policy 这个参数用于控制validate_password的验证策略 0-->low 1-->MEDIUM 2-->strong。
2、validate_password_length密码长度的最小值(这个值最小要是4)。
3、validate_password_number_count 密码中数字的最小个数。
4、validate_password_mixed_case_count大小写的最小个数。
5、validate_password_special_char_count 特殊字符的最小个数。
6、validate_password_dictionary_file 字典文件
本次环境设置:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 8.0.32 |
+-----------+
1 row in set (0.00 sec)
mysql>
##验证策略强
mysql> set global validate_password_policy=strong;
Query OK, 0 rows affected (0.00 sec)
##密码长度的最小值8
mysql> set global validate_password_length=8;
Query OK, 0 rows affected (0.00 sec)
##大小写的最小个数1
mysql> set global validate_password_mixed_case_count=1;
Query OK, 0 rows affected (0.00 sec)
##密码中数字的最小个数1
mysql> set global validate_password_number_count=1;
Query OK, 0 rows affected (0.00 sec)
##特殊字符的最小个数1
mysql> set global validate_password_special_char_count=1;
Query OK, 0 rows affected (0.00 sec)
mysql>
密码规则检查:
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| 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 | STRONG |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
mysql>
15、创建一个用户用于远程登录
MySQL8.0 以上将创建账户和赋予权限分开了,需要用两个语句创建账号,赋予远程权限;
###1、创建zyl用户用于远程登录
create user 'zyl'@'%' identified by 'Zyl##2023';
##2、授权远程登录
#仅授权某一个ip访问
grant all privileges on *.* to 'zyl'@'191.168.181.10' with grant option;
# 允许所有 ip 远程访问(不建议设置),这里测试环境,允许所有ip访问
grant all privileges on *.* to 'zyl'@'%' with grant option;
###3、刷新(刷新后才生效)
FLUSH PRIVILEGES;
查看监听端口:
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)
mysql>
远程访问测试:
16、设置mysql服务开机自启
将/usr/local/mysql8.0/support-files/mysql.server
拷贝到/etc/init.d/mysqld目录。
[root-server ~]# cp /usr/local/mysql8.0/support-files/mysql.server /etc/init.d/mysqld
[root-server ~]# systemctl enable mysqld
##启动
[root-server ~]# systemctl start mysqld
##状态查看
[root-server ~]# systemctl status mysqld
##停止
[root-server ~]# systemctl stop mysqld
##设置开机自启
[root-server ~]# systemctl enable mysqld
##取消开机自启
[root-server ~]# systemctl disable mysqld
二、总结—温故知新
1、mysql8.0 详细安装步骤
2、环境变量配置
3、root用户密码修改
4、远程登录账户创建、授权
5、服务开机自启等设置