1 创建mysql用户

1
$ CREATE USER myuser IDENTIFIED BY 'password';

2 分配权限

1
$ grant all privileges on mydb.* to myuser@'%' identified by 'mypasswd';

mydb:DB name myuser: mysql user name mypassword: password

eg. assign all DB privileges to all remote user

1
$ grant all PRIVILEGES on *.* to root@'%' identified by '123456';

不要忘记flush修改的内容

1
$ FLUSH PRIVILEGES;

3 ubuntu 开放 3306端口,远程访问

  • 使用nestat命令查看3306端口状态
1
$ netstat -an | grep 3306

tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN

如果是上面的结果,说明 3306端口只是在IP 127.0.0.1上监听,所以拒绝了其他IP的访问。

  • 解决方法:修改/etc/mysql/my.cnf文件。打开文件,找到下面内容:
1
2
3
# 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

把上面这一行注释掉或者把127.0.0.1换成合适的IP,建议注释掉。

  • 重新启动
1
$ sudo /etc/init.d/mysql restart

c 重新使用netstat检测:

1
$ netstat -an | grep 3306

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN