1、MySQL配置文件mysqld配置块写入如下配置
[mysqld]# 2025/02/04skip-grant-tables
2、启动MySQL进入,执行如下语句修改即可
--进入MySQ数据库服务器[root@DevOps83 login_project]# opt/mysql/bin/mysql -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 8Server version: 8.4.0 MySQL Community Server - GPLCopyright (c) 2000, 2024, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.-- 使用use语句进入MySQL数据库mysql> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changed--刷新权限啥意思?!mysql> flush privileges;Query OK, 0 rows affected (0.01 sec)--还刷新权限,啥意思?!mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)-- 此处应是查询数据库的基本信息,和刷权限没关系
3、修改已经存在的用户密码
--使用alte语修改用户密码,前提是必须是存在的用户mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456.com';ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'%'mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456.com';ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'mysql>mysql>--查修改后的用户基本信息mysql> select user,host,authentication_string from mysql.user;+------------------+-----------+------------------------------------------------------------------------+| user | host | authentication_string |+------------------+-----------+------------------------------------------------------------------------+| mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || mysql.session | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || mysql.sys | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || root | localhost | 123456.com |+------------------+-----------+------------------------------------------------------------------------+4 rows in set (0.00 sec)
4、修改用户密码为空密码
mysql> update user set authentication_string='' where user='root';Query OK, 1 row affected (0.01 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql>-> flush privileges;Query OK, 0 rows affected (0.00 sec)
5、再次查询用户信息
mysql> select user,host,authentication_string from mysql.user;+------------------+-----------+------------------------------------------------------------------------+| user | host | authentication_string |+------------------+-----------+------------------------------------------------------------------------+| mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || mysql.session | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || mysql.sys | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || root | localhost | |+------------------+-----------+------------------------------------------------------------------------+4 rows in set (0.00 sec)
6、修改用户密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456.com';Query OK, 0 rows affected (0.00 sec)mysql> select user,host,authentication_string from mysql.user;+------------------+-----------+------------------------------------------------------------------------+| user | host | authentication_string |+------------------+-----------+------------------------------------------------------------------------+| mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || mysql.session | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || mysql.sys | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED || root | localhost | $A$005$C8Rywt z @NnrDYrhVszGCOvCQocBoe/VxWzUDsELkGblq06yHXC9yC |+------------------+-----------+------------------------------------------------------------------------+4 rows in set (0.01 sec)
7、退出系统
mysql> exitBye
8、注释MySQL中的配置文件中mysqld段的skip语句,重新启动MySQL,确认密码是否修改成功
[root@DevOps83 login_project]# opt/mysql/bin/mysql -uroot -p'123456.com'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 9Server version: 8.4.0 MySQL Community Server - GPLCopyright (c) 2000, 2024, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>mysql> \s--------------/opt/mysql/bin/mysql Ver 8.4.0 for Linux on x86_64 (MySQL Community Server - GPL)Connection id: 9Current database:Current user: root@localhostSSL: Not in useCurrent pager: stdoutUsing outfile: ''Using delimiter: ;Server version: 8.4.0 MySQL Community Server - GPLProtocol version: 10Connection: Localhost via UNIX socketServer characterset: utf8mb4Db characterset: utf8mb4Client characterset: utf8mb4Conn. characterset: utf8mb4UNIX socket: /data/mysql/3306/data/mysql.sockBinary data as: HexadecimalUptime: 8 secThreads: 2 Questions: 7 Slow queries: 0 Opens: 136 Flush tables: 3 Open tables: 55 Queries per second avg: 0.875
9、登录MySQL服务器,建库建表
-- 2025/02/04 Web登录页面的测试,建库建表mysql> create user 'hanyw'@'%' identified by '123456.com';Query OK, 0 rows affected (0.03 sec)mysql> create database php_demo01;Query OK, 1 row affected (0.00 sec)mysql> grant all privileges on php_demo01.* to 'hanyw'@'%' all privileges;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 'all privileges' at line 1mysql>mysql> grant all privileges on php_demo01.* to 'hanyw'@'%';Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)
10、输入修改后的用户密码信息登录MySQL数据库
[root@DevOps83 login_project]# /opt/mysql/bin/mysql -uhanyw -p'123456.com'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 11Server version: 8.4.0 MySQL Community Server - GPLCopyright (c) 2000, 2024, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> ^DBye[root@DevOps83 login_project]# /opt/mysql/bin/mysql -h 10.16.5.128 -uhanyw -p'123456.com'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 12Server version: 8.4.0 MySQL Community Server - GPLCopyright (c) 2000, 2024, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> select user();+-------------------+| user() |+-------------------+| hanyw@10.16.5.128 |+-------------------+1 row in set (0.00 sec)mysql> exitBye
11、 思考【授权引发的小问题】
授权的是hanyw用户,为何hanyw@localhost 和hanyw@%都可以登录MySQL数据库呢?
12、附送:建库建用户表的语句
CREATE DATABASE db01;USE db01;CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(50) NOT NULL,password VARCHAR(255) NOT NULL);INSERT INTO users (username, password) VALUES ('testuser001', '123456.com');
文章转载自韩公子的Linux大集市,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




