最近接手个混合式的老项目,有些页面需要前端修改,所以需要连上我本地mysql取到数据看效果,这就需要他们能在局域网中通过ip访问我的数据库,在网上看了一下解决方案入下:
第一步:先用其他电脑在命令行ping本机,看能否ping通
C:\Windows\system32>ping 10.177.15.170
正在 Ping 10.177.15.170 具有 32 字节的数据:
来自 10.177.15.170 的回复: 字节=32 时间<1ms TTL=128
来自 10.177.15.170 的回复: 字节=32 时间<1ms TTL=128
来自 10.177.15.170 的回复: 字节=32 时间<1ms TTL=128
来自 10.177.15.170 的回复: 字节=32 时间<1ms TTL=128
10.177.15.170 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
如上则说明没有问题,也有ping不通的情况,图快就直接关闭自己电脑的防火墙,但是不安全,时间允许的话建议搜索合理的解决方案。
第二步:开放mysql的访问
打开MySQL 命令行链接mysql:mysql -hlocalhost -uroot -p
输入密码:Enter password: ******(自己的密码)
打开 mysql 数据库:use mysql(因为MySQL的权限存在这个里面)
将user=‘root’的用户访问权限为all:update user set host=’%’ where user=‘root’;(把host改为%,相当于任何用户都可以连接。)
让赋予的权限立即生效:flush privileges;
C:\Users\ut>mysql -hlocalhost -uroot -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.15 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.
mysql> use mysql
Database changed
mysql> update user set host='%' where host= 'localhost';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select host,user from user;
+------+------------------+
| host | user |
+------+------------------+
| % | mysql.infoschema |
| % | mysql.session |
| % | mysql.sys |
| % | root |
+------+------------------+
4 rows in set (0.00 sec)
mysql> flush privileges ;
Query OK, 0 rows affected (0.01 sec)
不出意外的话重启一下自己的mysql服务,同事电脑就能连上自己的库了,如果不行试下下面的命令
GRANT ALL PRIVILEGES ON *.* TO root @'%' IDENTIFIED BY "mypassword";
FLUSH PRIVILEGES;
% 表示所有的IP都能访问,也可以修改为专属的
mypassword 为连接密码 需要修改为你自己的
参考链接:https://www.jianshu.com/p/92e2204d04f5
来源:简书