Linux MySQL 修改root密码和设置权限

Linux MySQL 修改root密码和设置权限_第1张图片

Linux MySQL 修改root密码和设置权限

如果遇到以下问题,或者使用 Navicat 连接不上Mysql(修改权限),请尝试一下操作。

Access denied for user 'root'@'localhost' (using password: YES)
修改密码
[root@localhost ~]# vim /etc/my.cnf

在[mysqld]下添加

skip-grant-tables

跳过密码登录mysql。
使用

[root@localhost ~]# mysql -u root -p

登录,然后回车,进入mysql命令行。

mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

进入user库:

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

修改密码:

mysql> select host,user,password from user;
+-----------------------+------+-------------------------------------------+
| host                  | user | password                                  |
+-----------------------+------+-------------------------------------------+
| 192.168.5.104         | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| localhost.localdomain | root |                                           |
| 127.0.0.1             | root |                                           |
| ::1                   | root |                                           |
| %                     | root | *FABE5482D5AADF36D028AC443D117BE1180B9725 |
+-----------------------+------+-------------------------------------------+
5 rows in set (0.00 sec)

mysql> UPDATE user SET Password = PASSWORD('root') WHERE user = 'root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 5  Changed: 4  Warnings: 0

然后flush privileges使生效

mysql> select user, host,password  from user;
+------+-----------------------+-------------------------------------------+
| user | host                  | password                                  |
+------+-----------------------+-------------------------------------------+
| root | 192.168.5.104         | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | localhost.localdomain | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | 127.0.0.1             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | ::1                   | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | %                     | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+------+-----------------------+-------------------------------------------+
5 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

然后ctrl + c退出mysql命令行,将/etc/my.cnf 之前添加的skip-grant-tables 去除。
然后重启mysq服务。

[root@localhost ~]# systemctl restart mysqld.service 
修改权限:

root默认是不支持远程登录的,用外网连接你必须给权限,
或者root不给权限,可以创建一个其他的用户,然后赋权限,
以便安全控制。

允许用户root使用密码从任何主机连接到mysql服务器

mysql>  GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; 
Query OK, 0 rows affected (0.00 sec)

或者

允许用户root从ip为192.168.5.104的主机连接到mysql服务器

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.5.104'  IDENTIFIED BY 'root'WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

然后flush privileges

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

重启

[root@localhost ~]# systemctl restart mysqld.service 

完。

你可能感兴趣的:(MySQL)