很多时候,我们的数据库服务和应用并不在一台服务器上,所以要开启mysql远程登陆。网上这种教程很多,但是质量参差不齐,讲的不透彻。
mysql -V: mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
图中所用连接工具为navicat。navicat能登录的话,shell也一样可以。
1. 新建非root、可远程登陆的用户
mysql> create user 'username'@'%' identified by 'password';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on database.* to 'username'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
复制代码
有部分资料到这一步就已经结束了,此时尝试远程登陆,依然失败。
2. 修改mysqld.cnf中的bind-address
# 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
==>>
bind-address = 0.0.0.0
复制代码
官方文档对于bind address的描述:
If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces. If the option specifies a list of multiple values, this value is not permitted.
查看详细文档
由此可见,此步的操作决定着第一步是否生效
此时就可以远程连接成功啦!
3. 安全加固
此时所有IPV4的主机都可以连接该SQL server,安全性相对较差。但是bind address只能指定一个IP,因此我们只能用防火墙来达到这个目的了,具体看mysql实现绑定多个ip
参考文章: