阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

203次阅读
没有评论

共计 2612 个字符,预计需要花费 7 分钟才能阅读完成。

前提

公司数据库没有版本控制,这个就相当坑爹了,问以前的技术,就是在本地建一个表,然后导入到线上,WTF。目前就只能临时开启远程连接数据了,后面在使用版本控制

防火墙

 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

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas.</description>
  <rule family="ipv4">
    <source address="122.10.70.234"/>
    <port protocol="udp" port="514"/>
    <accept/>
  </rule>
  <rule family="ipv4">
    <source address="123.60.255.14"/>
    <port protocol="tcp" port="10050-10051"/>
    <accept/>
  </rule>
 <rule family="ipv4">
    <source address="192.249.87.114"/> 放通指定 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'@'192.168.10.10' 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 远程连接数据库

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

Ubuntu 16.04 LTS 上安装 Nginx、MariaDB 和 HHVM 运行 WordPress  http://www.linuxidc.com/Linux/2016-10/136435.htm

Ubuntu 16.04 Dockerfile 安装 MariaDB  http://www.linuxidc.com/Linux/2016-09/135260.htm

Linux 系统教程:如何检查 MariaDB 服务端版本  http://www.linuxidc.com/Linux/2015-08/122382.htm

Ubuntu 16.04 下如何安装 MariaDB  http://www.linuxidc.com/Linux/2017-04/142915.htm

CentOS 7.3 二进制安装 MariaDB10.2.8 步骤  http://www.linuxidc.com/Linux/2017-10/147904.htm

CentOS 7 编译安装 MariaDB-10.1.22  http://www.linuxidc.com/Linux/2017-05/143291.htm

Ubuntu 上如何将 MySQL 5.5 数据库迁移到 MariaDB 10  http://www.linuxidc.com/Linux/2014-11/109471.htm

[翻译]Ubuntu 14.04 (Trusty) Server 安装 MariaDB  http://www.linuxidc.com/Linux/2014-12/110048htm

Ubuntu 14.04(Trusty)安装 MariaDB 10 数据库  http://www.linuxidc.com/Linux/2016-11/136833.htm

MariaDB 的详细介绍:请点这里
MariaDB 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-10/148068.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2612字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中