Mysql 没有 root 用户的问题

2022年1月13日 3800点热度 2人点赞 1条评论
内容纲要

安装了 Mysql/MariaDB ,但是 root 死活进不去,只能用 mysql 这个账号进去。

[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 `<code>@</code>localhost` |
+--------------------------------------+

可以看到,根本没有 mysql 这些系统表,说明安装根本不正常。

如果是 mysql,想办法找到 my.ini 文件,如果是 MariaDB ,在 /etc/my.cnf.d/server.cnf 文件里面。找到 [mysqld],改成如下:

[mysqld]
skip-grant-tables
skip-networking

然后重启数据库。

执行命令进入数据库:

mysql

输入命令创建表以及添加用户:

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

然后再找到 配置文件的 [mysqld] ,删除添加的那两行 skip*
重启数据库。

使用 mysql -u root -p 登录数据库,成功!
修改或设置新的密码:

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

PS:
允许远程:

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

痴者工良

高级程序员劝退师

文章评论

  • 痴者工良

    如果是 TIDB - MYSQL,则使用 SET PASSWORD FOR 'root'@'%' = 'xxx';,然后刷新:flush privileges;
    同样,TIDB-MYSQL 也可以开启远程:

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

    2022年1月18日