CentOS7和CentOS6怎样开启MySQL远程访问

CentOS7和CentOS6怎样开启MySQL远程访问,第1张

CentOS6开启MySQL远程访问

1.开放mysql访问端口3306

修改防火墙配置文件

vi /etc/sysconfig/iptables

加入端口配置

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

重新加载规则

service iptables restart

2.修改mysql库里的host

登录mysql;

use mysql

update user set host='%' where user='root' and host='localhost'

记得一定还得修改密码,因为这时密码已失效,虽然本地还可以原密码登录,可远程改了host后还是没法访问

UPDATE user SET password=password("root") WHERE user='root'

flush privileges

3.重启mysql,远程就可以访问了

service mysqld restart

CentOS7开启MySQL远程访问

CentOS7这个版本的防火墙默认使用的是firewall,与之前的版本使用iptables不一样。按如下方便配置防火墙:

1、关闭防火墙:sudo systemctl stop firewalld.service

2、关闭开机启动:sudo systemctl disable firewalld.service

3、安装iptables防火墙

执行以下命令安装iptables防火墙:sudo yum install iptables-services

?

4、配置iptables防火墙,打开指定端口(CentOS6一样)

5、设置iptables防火墙开机启动:sudo systemctl enable iptables

6、之后的和CentOS6一样

设置mysql服务允许外网访问,修改mysql的配置文件,有的是my.ini,有的是my.cnf【linux】.

1:设置mysql的配置文件

/etc/mysql/my.cnf

找到 bind-address =127.0.0.1 将其注释掉;//作用是使得不再只允许本地访问;

重启mysql:/etc/init.d/mysql restart

2:登录mysql数据库:mysql -u root -p

mysql>use mysql

查询host值:

mysql>select user,host from user

如果没有"%"这个host值,就执行下面这两句:

mysql>update user set host='%' where user='root'

mysql>flush privileges

或者也可以执行:

mysql>grand all privileges on *.* to root@'%' identifies by ' xxxx'

其中 第一个*表示数据库名;第二个*表示该数据库的表名;如果像上面那样 *.*的话表示所有到数据库下到所有表都允许访问;

‘%':表示允许访问到mysql的ip地址;当然你也可以配置为具体到ip名称;%表示所有ip均可以访问;

后面到‘xxxx'为root 用户的password;

MySQL远程访问权限,允许远程连接的开启

1、登陆mysql数据库

mysql -u root -p

查看user表 www.2cto.com

mysql>use mysql

Database changed

mysql>select host,user,password from user

+--------------+------+-------------------------------------------+

| host | user | password |

+--------------+------+-------------------------------------------+

| localhost| root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |

| 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |

+--------------+------+-------------------------------------------+

2 rows in set (0.00 sec)

可以看到在user表中已创建的root用户。host字段表示登录的主机,其值可以用IP,也可用主机名,

(1)有时想用本地IP登录,那么可以将以上的Host值改为自己的Ip即可。

2、实现远程连接(授权法)

将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。

update user set host = ’%’ where user = ’root’

将权限改为ALL PRIVILEGES

mysql>use mysql

Database changed

mysql>grant all privileges on *.* to root@'%' identified by "root"

Query OK, 0 rows affected (0.00 sec) www.2cto.com

mysql>select host,user,password from user

+--------------+------+-------------------------------------------+

| host | user | password |

+--------------+------+-------------------------------------------+

| localhost| root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |

| 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |

| %| root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |

+--------------+------+-------------------------------------------+

3 rows in set (0.00 sec)

这样机器就可以以用户名root密码root远程访问该机器上的MySql.

3、实现远程连接(改表法)

use mysql

update user set host = '%' where user = 'root'

这样在远端就可以通过root用户访问Mysql.


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7466148.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存