The issue of MySQL not having a root user

2022年1月13日 5034点热度 2人点赞 1条评论
内容目录

Installed Mysql/MariaDB, but I cannot log in as root; I can only use the mysql account to get in.

[root@192-168-0-241 ~]# mysql -u mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.32-MariaDB-log MariaDB Server

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

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

MariaDB [(none)]> show tables;
ERROR 1046 (3D000): No database selected
MariaDB [(none)]> SHOW DATABASES
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+

MariaDB [(none)]> select user ();
+-----------------+
| user ()         |
+-----------------+
| mysql@localhost |
+-----------------+
1 row in set (0.000 sec)

MariaDB [(none)]> show grants;
+--------------------------------------+
| Grants for @localhost                |
+--------------------------------------+
| GRANT USAGE ON *.* TO ``@`localhost` |
+--------------------------------------+

As you can see, there are no mysql system tables, indicating that the installation is not correct.

If it's mysql, try to locate the my.ini file; if it's MariaDB, it can be found in /etc/my.cnf.d/server.cnf. Locate [mysqld] and change it to the following:

[mysqld]
skip-grant-tables
skip-networking

Then restart the database.

Execute the command to enter the database:

mysql

Enter the following commands to create tables and add users:

UPDATE mysql.user SET password=password('123456')
WHERE user='root' AND host='localhost';
exit

Then find the configuration file [mysqld] and delete the two lines you added: skip*.
Restart the database.

Use mysql -u root -p to log in to the database, successfully!
Modify or set a new password:

set password for root@localhost = password('123456');

PS:
Allow remote access:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;

痴者工良

高级程序员劝退师

文章评论