1.默认情况下Mysql只允许本地登录,所以需要修改配置文件将地址绑定给注释掉:
vim /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 <---注释掉这一行就可以远程登录了
2.重启mysql服务:
sudo service mysql restart
3.设置可以从远程登录的用户,以用户tom为例
$ mysql -u root -p (以root用户登录,赋予tom用户权限)
mysql > update mysql.user set host= '%' where user=' tom'
4.授权用户(以tom为例)可以远程访问的数据库(以testdb为例)
mysql > GRANT ALL ON testdb.* TO 'tom'@'%';
mysql > FLUSH PRIVILEGES;
5.远程登录测试
mysql -h hostname -P protNum -u username -p
如:mysql -h 192.168.0.1 -P 3306 -u tom -p