linux系统上的mysql开启远程链接


前言

今天接手一个项目,需要在本地用连接liunx服务器上的mysql数据库,就是开启远程链接,只知道服务器的root账号密码,不知道mysql的,记录一下吧

下面案例可供参考

1、使用root登陆mysql

由于不知道root密码,登陆不了,修改/etc/my.cnf文件, 在**[Mysqld]**配置段下添加:skip-grant-tables

可以跳过mysql登陆时密码验证(好用!) 改好后重启mysql服务:/etc/init.d/mysql restart

用这个登陆:mysql -u root -p 需要密码直接回车

2、更新root账号密码

1、使用mysql数据库:use mysql;

2、更改root密码:update user set authentication_string = password(‘root’), password_expired = ‘N’, password_last_changed = now() where user = ‘root’;

注:我的mysql版本未5.7,密码字段为【authentication_string】,不同版本mysql字段不一样,可以使用【select * from user;】查看密码字段

3、开启root账号远程登陆权限

1、使用mysql数据库:use mysql; 2、查看root账号权限:select user,host from user; 如果root不是【%】,使用语句更新:update user set host = ‘%’ where user = ‘root’;

4、开启3306端口访问权限

【exit;】退出mysql,查看3306端口状态:netstat -apn | grep 3306 前面为[0.0.0.0]就是允许所有ip访问 如果第一行中 3306 前面的ip为127.0.0.1 时,表示该端口只对本地打开; 那么我们就需要编辑 etc/mysql/my.cnf文件 使用vi编辑器打开my.cnf文件

将 bind-address = 127.0.0.1 改成 0.0.0.0

重启服务 service mysql stop service mysql start

经验分享 程序员 微信小程序 职场和发展