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

2023年 7月 12日 72.7k 0

防火墙

 CentOS7 之前的防火墙是不一样的,比如你要添加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

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



  Public
  For use in public areas.
  
    
    
    
  
  
    
    
    
  
 
     放通指定ip,指定端口、协议
    
    
  
 放通任意ip访问服务器的9527端口
    
    
  

上述配置文件可以看出:

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远程连接数据库

接下来就是可以本地连接了。

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论