关于远程访问ubuntu服务器中mysql和memcache的一些总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16885135/article/details/52524552

领导分配一个任务,需要远程访问一台ubuntu服务器上的mysql和memcache。

首先需要配置mysql,使其允许远程访问:

登录ubuntu服务器,我发现mysql的user是这样配置的

select host,user from user\G

Host:% 
User:root 

当时我就震惊了,这样配置不是代表只要知道root账户密码,任何IP都可以登录这台服务器的mysql了吗??这样不是想当危险吗?
于是我尝试在个人电脑上登录这台服务器上的mysql

%sudo mysql -hxx.xx.xx.xx -P3306 -uroot -p

结果却是失败了。

我又尝试远程登录memcache:

修改memcache的配置文件,使其允许远程访问。

%sudo vim /etc/memcached.conf

将 -l 127.0.0.1注释掉。这样就可以使用任何IP登录memcache (注:这样非常危险,只适合测试)

重启memcache

%sudo services memcached restart

尝试在个人电脑登录memcache
打开cmd命令行

telnet xx.xx.xx.xx 11211

结果再次失败了。

经过查阅资料发现原来linux有个iptables(防火墙)可以限制外网ip访问。

使用 sudo iptables -L -n 查看iptables设置

%sudo iptables -L -n

Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:21
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 8
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state ESTABLISHED

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

发现服务器仅开放了21(ftp),22(ssh),和80(http)端口

于是在网上查找开对某些ip开放11211端口的方法。

接着,我做了一件特别傻逼的事儿,按照网上的教程,首先执行了这一条命令:

%sudo iptables -F #(请勿模仿)

于是就悲剧了:这个命令清空了iptables中的所有规则,关闭了所有外界访问的端口。服务器上的网站瞬间就无法访问了。不仅如此,ssh远程连接也断开了,我竟然什么也做不了了。

我意识到了问题的严重性,于是马上联系领导重启了一下服务器,由于没有修改iptables启动时读取的文件,所以重启服务器之后一切有都恢复正常了。

经过一番学习,对iptables有了一定的了解之后。

#将iptables配置存储到一个文件
%sudo iptables-save > ~/iptables.save
# 编辑这个文件
%sudo vim ~/iptables.save

# Generated by iptables-save v1.4.21 on Tue Sep 13 09:00:12 2016
*filter
:INPUT DROP [31186:1828159]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [73190648:74353549865]
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -m state --state ESTABLISHED -j ACCEPT
COMMIT
# Completed on Tue Sep 13 09:00:12 2016

做出如下修改:

# Generated by iptables-save v1.4.21 on Tue Sep 13 09:00:12 2016
*filter
:INPUT DROP [31186:1828159]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [73190648:74353549865]
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
#-s 后为允许访问的IP 
-A INPUT -p tcp -m tcp -s 123.57.20.21 --dport 11211 -j ACCEPT
-A INPUT -p tcp -m tcp -s 123.57.20.21 --dport 11211 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -m state --state ESTABLISHED -j ACCEPT
COMMIT
# Completed on Tue Sep 13 09:00:12 2016

保存

将iptables.save中的配置应用到服务器的iptables中

%sudo cat ~/iptables.save | sudo iptables-restore
#(注意:这样修改iptables只能临时生效,重启服务器后会失效,要想永久生效,需要修改开机启动时iptables默认读取的文件)

此时使用 sudo iptables -L -n 查看iptables设置

%sudo iptables -L -n 

Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:21
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
ACCEPT     tcp  --  123.57.20.21         0.0.0.0/0            tcp dpt:11211
ACCEPT     tcp  --  123.57.20.21         0.0.0.0/0            tcp dpt:11211
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 8
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state ESTABLISHED

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

发现服务器对ip123.57.20.21开放了 11211 端口,于是使用telnet尝试连接memcache,成功!

依样添加mysql的3306端口:

%sudo vim ~/iptables.save

# Generated by iptables-save v1.4.21 on Tue Sep 13 09:00:12 2016
*filter
:INPUT DROP [31186:1828159]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [73190648:74353549865]
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp -s 123.57.20.21 --dport 11211 -j ACCEPT
-A INPUT -p tcp -m tcp -s 123.57.20.21 --dport 11211 -j ACCEPT
-A INPUT -p tcp -m tcp -s 123.57.20.21 --dport 3306 -j ACCEPT
-A INPUT -p tcp -m tcp -s 123.57.20.21 --dport 3306 -j ACCEPT

-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -m state --state ESTABLISHED -j ACCEPT
COMMIT
# Completed on Tue Sep 13 09:00:12 2016
%sudo cat ~/iptables.save | sudo iptables-restore

此时使用 sudo iptables -L -n 查看iptables设置

%sudo iptables -L -n

Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:21
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
ACCEPT     tcp  --  123.57.20.21         0.0.0.0/0            tcp dpt:11211
ACCEPT     tcp  --  123.57.20.21         0.0.0.0/0            tcp dpt:11211
ACCEPT     tcp  --  123.57.20.21         0.0.0.0/0            tcp dpt:3306
ACCEPT     tcp  --  123.57.20.21         0.0.0.0/0            tcp dpt:3306
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 8
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state ESTABLISHED

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

尝试连接mysql,却发现还是无法访问成功。

在服务器上查看监听的端口

%sudo ss -tlnp

State      Recv-Q Send-Q                  Local Address:Port                    Peer Address:Port
LISTEN     0      128                                 *:80                                 *:*      users:(("nginx",1696,10),("nginx",1695,10),("nginx",1694,10),("nginx",1693,10),("nginx",1692,10),("nginx",1691,10),("nginx",1690,10),("nginx",1689,10),("nginx",1688,10))
LISTEN     0      128                                 *:22                                 *:*      users:(("sshd",853,3))
LISTEN     0      100                                 *:25                                 *:*      users:(("master",1869,12))
LISTEN     0      128                                 *:11211                              *:*      users:(("memcached",2965,26))
LISTEN     0      100                                :::25                                :::*      users:(("master",1869,13))
LISTEN     0      128                                :::11211                             :::*      users:(("memcached",2965,27))

却发现并没有在监听3306端口。

于是查看mysql的配置文件:

%sudo vim /etc/mysql/my.cnf

发现mysql的监听端口并没有修改。
于是我发现了这一行

skip-networking

skip-networking : 开启 skip-networking 选项可以彻底关闭MySQL的TCP/IP连接方式,在一些文档中也提到在单机运行的 MySQL 推荐开启该选项,现在看,不太靠谱。

把Skip-networking注释掉后,重启mysql

%sudo services mysql restart
%sudo ss -tlnp

State      Recv-Q Send-Q                  Local Address:Port                    Peer Address:Port
LISTEN     0      128                                 *:80                                 *:*      users:(("nginx",1696,10),("nginx",1695,10),("nginx",1694,10),("nginx",1693,10),("nginx",1692,10),("nginx",1691,10),("nginx",1690,10),("nginx",1689,10),("nginx",1688,10))
LISTEN     0      128                                 *:22                                 *:*      users:(("sshd",853,3))
LISTEN     0      100                                 *:25                                 *:*      users:(("master",1869,12))
LISTEN     0      50                                  *:3306                               *:*      users:(("mysqld",8501,16))
LISTEN     0      128                                 *:11211                              *:*      users:(("memcached",2965,26))
LISTEN     0      100                                :::25                                :::*      users:(("master",1869,13))
LISTEN     0      128                                :::11211                             :::*      users:(("memcached",2965,27))

发现mysql的3306端口在监听了

于是再次尝试连接mysql,终于成功了。

猜你喜欢

转载自blog.csdn.net/qq_16885135/article/details/52524552
今日推荐