OneinStack如何配置MySQL远程连接?

问答中心分类: DatabaseOneinStack如何配置MySQL远程连接?
oneinstack asked 9年 ago
OneinStack如何配置是本地连接MySQL

7 Answers

Best Answer

oneinstack answered 9年 ago

为了安全考虑,OneinStack仅允许云主机本机(localhost)连接数据库,如果需要远程连接数据库,需要如下操作(缺一不可):

1. 云主机安全组端口开放3306端口

参考文档:《云主机安全组端口开放教程

2. 打开iptables 3306端口

PS: 只有开启了iptables才需要此步骤!
如果您的操作系统为CentOS系列:

iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
service iptables save #保存iptables规则

如下图:

如果您的操作系统为Ubuntu/Debian系列:

iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
iptables-save > /etc/iptables.up.rules #保存iptables规则

如下图:

3. 数据库授权

注意⚠️:远程连接新建一个帐号(帐号名不能为root)。
如:添加一个用户名为db_user,密码为db_pass,授权为% (%表示所有IP能连接)对db_name数据库所有权限,命令如下:

1)MySQL8.0版本

# mysql -uroot -p
MySQL [(none)]> create user db_user@'%' identified by 'db_pass'; #创建用户
MySQL [(none)]> grant all privileges on db_name.* to db_user@'%' with grant option; #授权
MySQL [(none)]> exit; #退出数据库控制台,特别注意有分号

2)其余MySQL版本

# mysql -uroot -p
MySQL [(none)]> grant all privileges on db_name.* to db_user@'%' identified by 'db_pass'; #授权语句,特别注意有分号
MySQL [(none)]> flush privileges;
MySQL [(none)]> exit; #退出数据库控制台,特别注意有分号

如下图:

匿名用户 answered 8年 ago
我按照这教程做了,安装wordpress提示无法连接数据库!不知道什么问题!

andy.zhang answered 8年 ago
你新建了空白数据库了没有?

wwwinewyacom answered 7年 ago
我的登录mysql提示:版本号

jirasin answered 7年 ago
Percona安装以后无法登录phpmyadmin
重启后也报错
ERROR! MySQL (Percona Server) PID file could not be found!
Starting MySQL (Percona Server).170308 18:46:54 mysqld_safe The file /usr/local/Percona-Server-5.6.35-rel80.0-Linux.x86_64.ssl101/bin/mysqld
does not exist or is not executable. Please cd to the mysql installation
directory and restart this script from there as follows:
./bin/mysqld_safe&
See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information
ERROR! The server quit without updating PID file (/data/percona/mysql.pid).

zdkx answered 7年 ago
请问大神,我开启了后想关闭  那怎么操作呢?

oneinstack answered 7年 ago
@zdkx

  1. 关闭iptables 3306端口
  2. mysql revoke收回权限