mysql 远程链接

1.设置防火墙

1.安装 
sudo apt-get install ufw 


2.启用 
sudo ufw enable 
sudo ufw default deny 
运行以上两条命令后,开启了防火墙,并在系统启动时自动开启。 
关闭所有外部对本机的访问,但本机访问外部正常。 


3.开启/禁用 
sudo ufw allow|deny [service] 
打开或关闭某个端口,例如: 
sudo ufw allow smtp 允许所有的外部IP访问本机的25/tcp (smtp)端口 
sudo ufw allow 22/tcp 允许所有的外部IP访问本机的22/tcp (ssh)端口 
sudo ufw allow 53 允许外部访问53端口(tcp/udp) 
sudo ufw allow from 192.168.1.100 允许此IP访问所有的本机端口 
sudo ufw allow proto udp 192.168.0.1 port 53 to 192.168.0.2 port 53 
sudo ufw deny smtp 禁止外部访问smtp服务 
sudo ufw delete allow smtp 删除上面建立的某条规则 


4.查看防火墙状态 
sudo ufw status 


一般用户,只需如下设置: 
sudo apt-get install ufw 
sudo ufw enable 
sudo ufw default deny 
以上三条命令已经足够安全了,如果你需要开放某些服务,再使用sudo ufw allow开启。
补充:


开启/关闭防火墙 (默认设置是’disable’)


# ufw enable|disable


转换日志状态


# ufw logging on|off


设置默认策略 (比如 “mostly open” vs “mostly closed”)


# ufw default allow|deny


许 可或者屏蔽某些入埠的包 (可以在“status” 中查看到服务列表[见后文])。可以用“协议:端口”的方式指定一个存在于/etc/services中的服务名称,也可以通过包的meta-data。 ‘allow’ 参数将把条目加入 /etc/ufw/maps ,而 ‘deny’ 则相反。基本语法如下:


# ufw allow|deny [service]


显示防火墙和端口的侦听状态,参见 /var/lib/ufw/maps。括号中的数字将不会被显示出来。


# ufw status


[注意:上文中虽然没有使用 sudo,但是命令提示符号都是“#”。所以……你知道啥意思了哈。原文如此。──译者注]




UFW 使用范例:


允许 53 端口


$ sudo ufw allow 53


禁用 53 端口


$ sudo ufw delete allow 53


允许 80 端口


$ sudo ufw allow 80/tcp


禁用 80 端口


$ sudo ufw delete allow 80/tcp


允许 smtp 端口


$ sudo ufw allow smtp


删除 smtp 端口的许可


$ sudo ufw delete allow smtp


允许某特定 IP


$ sudo ufw allow from 192.168.254.254


删除上面的规则


$ sudo ufw delete allow from 192.168.254.254


2.设置mysql

use mysql;

select host,user from user;

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

GRANT ALL PRIVILEGES ON *.* TO user_name@HOST IDENTIFIED BY ‘pass_word’;

GRANT ALL PRIVILEGES ON *.* TO user_name@'%' IDENTIFIED BY ‘pass_word’; 

flush privileges;


Q: 当在另一台机器上登录MySQL时出现如下错误:

ERROR 2003 (HY000): Can't connect to MySQL server on 'x.x.x.x' (111)

A: 原因是MySQL考虑到安全因素,默认配置只让从本地登录

打开 /etc/mysql/my.cnf 文件,找到 bind-address = 127.0.0.1 修改为 bind-address = 0.0.0.0

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

可参考:

> I've already check my /etc/my.cnf file for a "binding"
> line.  Its not 
> there.  I also found an item online that indicated
> adding:
>

Look for the option "skip-networking". This disables TCP/IP so the 
server only accepts local connections via the Unix socket. This sounds 
like your situation.
Note that a "could not connect" error means just that. If the problem 
was related to user privileges you would get an "access denied" error.

Q: 还一种情况出现类似下面的错误:

ERROR 1045 (28000): Access denied for user 'test'@'x.x.x.x' (using password: NO)

A: 原因是没有给登录用户名设置远程主机登录的权限。

在本地用 root 登录: mysql -u root -p

修改 MySQL 数据库中 user 表中 对应用户名的 Host 字段,将 localhost 改为 %

use mysql;

update user set Host = '%' where User = 'username';


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值