MySQL开启远程访问权限 允许远程连接

1、登陆mysql数据库    
mysql -u root -p
查看user表

C:\Users\Administrator>C:\mysql\bin\mysql.exe -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.40-ndb-7.4.20-cluster-gpl MySQL Cluster Community Server (GP
L)

mysql> use mysql;
Database changed
mysql> select host,user,password from user;
+-----------+------+----------+
| host      | user | password |
+-----------+------+----------+
| localhost | root |          |
| 127.0.0.1 | root |          |
| ::1       | root |          |
| localhost |      |          |
+-----------+------+----------+
4 rows in set (0.00 sec)

mysql>

可以看到在user表中已创建的root用户。host字段表示登录的主机,其值可以用IP,也可用有时想用本地IP登录,那么可以将以上的Host值改为自己的Ip即可。


2、实现远程连接(授权法)

将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。   
update user set host = ’%’ where user = ’root’;

将权限改为ALL PRIVILEGES

mysql> use mysql;
Database changed
mysql> grant all privileges  on *.* to root@'%' identified by "";
Query OK, 0 rows affected (0.03 sec)


mysql> select host,user,password from user;
+-----------+------+----------+
| host      | user | password |
+-----------+------+----------+
| localhost | root |          |
| 127.0.0.1 | root |          |
| ::1       | root |          |
| localhost |      |          |
| %         | root |          |
+-----------+------+----------+
5 rows in set (0.02 sec)

这样机器就可以以用户名root密码root远程访问该机器上的MySql.


3、实现远程连接(改表法)

use mysql;

update user set host = '%' where user = 'root';

这样在远端就可以通过root用户访问Mysql.

猜你喜欢

转载自blog.csdn.net/w892824196/article/details/80249327