Kylinv10 系统 mysql8.4安装

✅ 1.环境

[root@zabbix71 soft]# hostnamectl
   Static hostname: zabbix71
         Icon name: computer-vm
           Chassis: vm
        Machine ID: d90e35d6b0ab46ba9a297436a8be50e7
           Boot ID: 32462e22996d487491c4ebd0060db4e4
    Virtualization: vmware
  Operating System: Kylin Linux Advanced Server V10 (Halberd)
            Kernel: Linux 4.19.90-89.11.v2401.ky10.x86_64
      Architecture: x86-64

mysql-8.4.2-linux-glibc2.17-x86_64.tar.xz

✅ 2.部署

设置mysql 的密码

echo 'mysqlpasswd'|passwd --stdin mysql

## 设置mysql 可以登录
usermod -s /bin/bash mysql
## 创建目录
mkdir /mysql /mysqldata /var/log/mariadb 
touch /var/log/mariadb/mariadb.log
## 授权
chown mysql:  /mysql /mysqldata /var/log/mariadb 
chown mysql: /var/log/mariadb/mariadb.log

chmod 755 /var/log/mariadb


## 解压
cd /mysql
cp  /soft/mysql-8.4.2-linux-glibc2.17-x86_64.tar.xz .
tar -xvf mysql-8.4.2-linux-glibc2.17-x86_64.tar.xz
mv mysql-8.4.2-linux-glibc2.17-x86_64 mysql8
## 授权
chown mysql:mysql -R  /mysql/mysql8

✅ 3.初始化配置文件

vi /etc/my.cnf



[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[mysqld]
bind-address=0.0.0.0
port=3306                       # 端口
user=mysql                      # 用户
basedir=/mysql/mysql8           # 基础安装目录
datadir=/mysqldata              # 数据存放地址
socket=/var/lib/mysql/mysql.sock         # 套接字文件路径,默认无需修改
log-error=/mysqldata/mysql.err  # MySQL 错误日志的文件路径
pid-file=/mysqldata/mysql.pid   # MySQL 进程的 PID(进程ID)文件路径
# character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
# 关闭MySQL的only_full_group_by模式
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
# 忽略表名大小写
lower_case_table_names=1
# 采用系统时间
log_timestamps=SYSTEM

✅ 4.初始化数据库

cd /mysql/mysql8/bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/mysql/mysql8/ --datadir=/mysqldata/ --user=mysql --initialize

## 查看初始化后的密码 文件
cat /mysqldata/mysql.err

2025-07-15T06:15:25.697031Z 0 [System] [MY-015017] [Server] MySQL Server Initialization - start.
2025-07-15T06:15:25.698692Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2025-07-15T06:15:25.698796Z 0 [System] [MY-013169] [Server] /mysql/mysql8/bin/mysqld (mysqld 8.4.2) initializing of server in progress as process 1269891
2025-07-15T06:15:25.711514Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2025-07-15T06:15:26.187424Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2025-07-15T06:15:28.419000Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: gsiklrBtN9?#
2025-07-15T06:15:31.012931Z 0 [System] [MY-015018] [Server] MySQL Server Initialization - end.



✅ 5.配置环境变量

cp /etc/profile /etc/profile.bak`date +%Y%m%d`

## 这里一定要用单引号,否则,里面的变量会自动转换
echo 'export PATH=$PATH:/mysql/mysql8/bin' >>  /etc/profile
source /etc/profile

✅ 6.注册为系统服务

cp /mysql/mysql8/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
vi /etc/init.d/mysqld
## 修改以下两行为你自己的路径:大概在46,47行
basedir=/mysql/mysql8
datadir=/mysqldata


ln -s /etc/init.d/mysqld /usr/bin/mysqld
systemctl daemon-reload
systemctl enable mysqld

✅ 7.启动mysql

systemctl start mysqld
## 查看状态
systemctl status mysqld

✅ 8.修改密码

遇到这个就需要强制重置密码

ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.

查看临时密码
cat /mysqldata/mysql.err
## 大概在最后类似这样的root@localhost: 后面就是密码
Note] [MY-010454] [Server] A temporary password is generated for root@localhost: uPS<*su!q4*R



[root@zabbix71 bin]# mysql -uroot -p
Enter password:
ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.





✅ 9.强制重置mysql 密码

## 如果无法登录,如密码过期,采取如下操作

systemctl stop mysqld

[root@zabbix71 bin]#  /mysql/mysql8/bin/mysqld_safe --skip-grant-tables --user=mysql --datadir=/mysqldata &

[1] 1272159
[root@zabbix71 bin]# 2025-07-15T06:29:04.509003Z mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
2025-07-15T06:29:04.557758Z mysqld_safe Starting mysqld daemon with databases from /mysqldata


[root@zabbix71 bin]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.4.2 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# 必须执行,否则会有错误:ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
MySQL [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.013 sec)

MySQL [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysqlpasswd';
Query OK, 0 rows affected (0.008 sec)

MySQL [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.003 sec)
## 创建root用户远程登录 
MySQL [(none)]> create user 'root'@'%' identified by 'mysqlpasswd';
Query OK, 0 rows affected (0.015 sec)

MySQL [(none)]>  grant all privileges on *.* to 'root'@'%' with grant option;
MySQL [(none)]>  FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.003 sec)

MySQL [(none)]> exit
Bye



[root@zabbix71 bin]# ps -ef|grep mysql
root     1272159  334220  0 14:29 pts/2    00:00:00 /bin/sh /mysql/mysql8/bin/mysqld_safe --skip-grant-tables --user=mysql --datadir=/mysqldata
mysql    1272455 1272159  0 14:29 pts/2    00:00:01 /mysql/mysql8/bin/mysqld --basedir=/mysql/mysql8 --datadir=/mysqldata --plugin-dir=/mysql/mysql8/lib/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock --port=3306

✅ 10.重启 MySQL 服务

# 杀掉临时启动的 mysqld_safe
pkill mysql 


# 启动正式服务
systemctl start mysqld

✅ 11. 验证新密码登录是否成功

[root@zabbix71 bin]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.4.2 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.004 sec)
## 创建数据库
MySQL [(none)]> create database drldb;
Query OK, 1 row affected (0.003 sec)

✅ 12.导入数据

cd /soft
mysql -u root -p drldb < dump.sql
Enter password: 
[root@u4adropdp01 soft]# mysql -u root -p drldb < data.sql
Enter password: 

错误解决方案

在这里插入图片描述

错误原因详解

MySQL 8.0 引入了新的默认认证方式:

  • 默认身份验证插件是:caching_sha2_password
  • 如果用户使用了这种认证方式,并且:
    • 没有在连接字符串中配置 allowPublicKeyRetrieval=true
    • 或者没有提供 SSL 连接
    • 或者没有使用 serverRSAPublicKeyFile 指定公钥文件

那么就会出现这个错误。

mysql -uroot -p

MySQL [(none)]> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.006 sec)

MySQL [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.003 sec)


Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐