Code前端首页关于Code前端联系我们

CentOS 7 配置 MariaDB 允许指定 IP 远程连接数据库

terry 2年前 (2023-09-26) 阅读数 44 #数据库

防火墙

CentOS 7 之前的防火墙有所不同。例如添加端口3306:

## 全部
 iptables -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT

 ## 部分ipiptables
 iptables -A INPUT -p tcp -s 138.111.21.11 -dport 3306 -j ACCEP

 service iptables save
 
 service iptables restart

 ## 查看 iptables
 iptables -L -n 

但这在CentOS 7中不起作用,你可以通过查看文档来了解。 CentOS 7 使用改进版的防火墙

firewall-cmd --zone=public --permanent --add-port=3306/tcp
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
  2、--permanent:表示设置为持久;
  3、--add-port:标识添加的端口;
  4、--zone=public:指定的zone为public;

。当然,如果你不习惯使用命令,我们可以直接将配置文件

CentOS 7配置MariaDB允许指定IP远程连接数据库

更改为etc/firewalld/zone并更改public。 xml

<?xml version="" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas.</description>
  <rule family="ipv4">
    <source address=""/>
    <port protocol="udp" port="514"/>
    <accept/>
  </rule>
  <rule family="ipv4">
    <source address=""/>
    <port protocol="tcp" port="10050-10051"/>
    <accept/>
  </rule>
 <rule family="ipv4">
    <source address=""/> 放通指定ip,指定端口、协议
    <port protocol="tcp" port="80"/>
    <accept/>
  </rule>
<rule family="ipv4"> 放通任意ip访问服务器的9527端口
    <port protocol="tcp" port="9527"/>
    <accept/>
  </rule>
</zone>

以上配置文件可见:

1、添加需要的规则,开放通源ip为122.10.70.234,端口514,协议tcp;
2、开放通源ip为123.60.255.14,端口10050-10051,协议tcp;/3、开放通源ip为任意,端口9527,协议
firewall 常用命令

# 重启
service firewalld restart 

# 开启
service firewalld start 

# 关闭
service firewalld stop

# 查看firewall 服务状态
 systemctl status firewall

# 查看防火墙
 firewall-cmd  --list-all

CentOS 7配置MariaDB允许指定IP远程连接数据库

对外开放服务器3306后,还需要配置数据库用户授权

MariaDB开启远程连接

数据库中mysql 您可以在 user 表中看到,默认情况下仅允许本地连接。可以添加所有用户。

# 针对ip
create user 'root'@'' identified by 'password';

#全部
 create user 'root'@'%' identified by 'password';

建议只对IP开放,而不是所有

授权用户

 # 给用户最大权限
  grant all privileges on *.* to 'root'@'%' identified by 'password';

 # 给部分权限(test 数据库)

  grant all privileges on test.* to 'root'@'%' identified by 'password' with grant option;

# 刷新权限表
 
 flush privileges;

# show grants for 'root'@'localhost';

CentOS 7配置MariaDB允许指定IP远程连接数据库

下一步就可以本地连接了。

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门