MYSQL默认只能本机访问,为了在其他主机上访问MYSQL需要手动配置。
首先需要为非本地(localhost)主机配置权限,然后修改MYSQL配置文件打开mysql的远程访问端口。
step1. 以root身份登入mysql
pi ~ $mysql -u root -p
step2.
##root使用123456从任何主机连接到mysql服务器
mysql> grant all privileges on *.* to root@”%” identified by “123456″;
step3.
mysql> FLUSH PRIVILEGES; ##使修改生效
##验证是否修改成功。
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 from user;
+————-+———————+
| host | user |
+————-+———————+
| % | root |
| 127.0.0.1 | root |
| localhost | |
| localhost | debian-sys-maint |
| localhost | root |
| raspberrypi | |
| raspberrypi | root |
+————-+———————+
7 rows in set (0.00 sec)
step4.打开mysql的远程访问端口
pi ~ $ netstat -ant | grep 3306 tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
从上面可以看出,mysql的3306端口只是监听本地的连接,这样就阻碍了外部IP对该数据库的访问,修改的办法其实很简单,进入到mysql的配置文件所在目录(/etc/mysql/my.cnf)下,找到文件中的如下内容:
# Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. bind-address = 127.0.0.1 ##注释掉这一行 重启mysql
pi ~ $ sudo /etc/init.d/mysql restart