简体   繁体   English

允许所有远程连接,MySQL

[英]Allow all remote connections, MySQL

I had been using SQL Server and am now using MySQL for a project.我一直在使用 SQL Server,现在在一个项目中使用 MySQL。 With SQL Server, our developers can connect to the remote database on their local machines if they know the host, username, password.使用 SQL Server,如果我们的开发人员知道主机、用户名和密码,他们就可以连接到本地计算机上的远程数据库。 With MySQL, though, to give a developer access from their local machines, I have been having to log in to MySQL and execute:然而,对于 MySQL,为了让开发人员从他们的本地机器访问,我不得不登录到 MySQL 并执行:

GRANT ALL ON *.* to user@address IDENTIFIED BY 'password'; 
flush privileges;

Where address is the IP address of the developer's machine.其中address是开发者机器的IP地址。 Of course, if they change networks, I have to execute it again.当然,如果他们改变网络,我必须再次执行。 Is there a way to allow all remote connections like I have experienced with SQL Server, or is this a bad idea for some reason?有没有办法允许所有远程连接,就像我在 SQL Server 上所经历的那样,或者出于某种原因这是一个坏主意? We have username and password still.. I'm obviously a little confused.我们还有用户名和密码。我显然有点困惑。

Also: this is a development database and is only accessible from our internal network.另外:这是一个开发数据库,​​只能从我们的内部网络访问。 I understand why it is a bad idea to give everyone access to a production database.我理解为什么让每个人都可以访问生产数据库是个坏主意。

As pointed out by Ryan above, the command you need is正如上面 Ryan 所指出的,您需要的命令是

GRANT ALL ON *.* to user@'%' IDENTIFIED BY 'password'; 

However, note that the documentation indicates that in order for this to work, another user account from localhost must be created for the same user;但是,请注意,文档指出为了使其工作,必须为同一用户创建来自localhost另一个用户帐户; otherwise, the anonymous account created automatically by mysql_install_db takes precedence because it has a more specific host column.否则,由mysql_install_db自动创建的匿名帐户优先,因为它具有更具体的主机列。

In other words;换句话说; in order for user user to be able to connect from any server;为了让用户user能够从任何服务器连接; 2 accounts need to be created as follows:需要创建2个帐户,如下所示:

GRANT ALL ON *.* to user@localhost IDENTIFIED BY 'password'; 
GRANT ALL ON *.* to user@'%' IDENTIFIED BY 'password'; 

Read the full documentation here.此处阅读完整文档

And here's the relevant piece for reference:这是相关的部分供参考:

After connecting to the server as root, you can add new accounts.以 root 身份连接到服务器后,您可以添加新帐户。 The following statements use GRANT to set up four new accounts:以下语句使用 GRANT 设置四个新帐户:

mysql> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
    ->     WITH GRANT OPTION;
mysql> CREATE USER 'monty'@'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'
    ->     WITH GRANT OPTION;
mysql> CREATE USER 'admin'@'localhost';
mysql> GRANT RELOAD,PROCESS ON *.* TO 'admin'@'localhost';
mysql> CREATE USER 'dummy'@'localhost';

The accounts created by these statements have the following properties:这些报表创建的帐户具有以下属性:

Two of the accounts have a user name of monty and a password of some_pass.其中两个帐户的用户名是 monty,密码是 some_pass。 Both accounts are superuser accounts with full privileges to do anything.这两个帐户都是具有执行任何操作的完全权限的超级用户帐户。 The 'monty'@'localhost' account can be used only when connecting from the local host. 'monty'@'localhost' 帐户只能在从本地主机连接时使用。 The 'monty'@'%' account uses the '%' wildcard for the host part, so it can be used to connect from any host. 'monty'@'%' 帐户使用 '%' 通配符作为主机部分,因此它可用于从任何主机连接。

It is necessary to have both accounts for monty to be able to connect from anywhere as monty .必须同时拥有 monty 的两个帐户才能像 monty 一样从任何地方连接 Without the localhost account, the anonymous-user account for localhost that is created by mysql_install_db would take precedence when monty connects from the local host.如果没有 localhost 帐户,当 monty 从本地主机连接时,由 mysql_install_db 创建的 localhost 的匿名用户帐户将优先。 As a result, monty would be treated as an anonymous user.因此,monty 将被视为匿名用户。 The reason for this is that the anonymous-user account has a more specific Host column value than the 'monty'@'%' account and thus comes earlier in the user table sort order.其原因是匿名用户帐户具有比 'monty'@'%' 帐户更具体的主机列值,因此在用户表排序顺序中排在更早的位置。 (user table sorting is discussed in Section 6.2.4, “Access Control, Stage 1: Connection Verification”.) (用户表排序在第 6.2.4 节“访问控制,阶段 1:连接验证”中讨论。)

You can disable all security by editing /etc/my.cnf:您可以通过编辑 /etc/my.cnf 禁用所有安全性:

[mysqld]
skip-grant-tables

Install and setup mysql to connect from anywhere remotely DOES NOT WORK WITH mysql_secure_installation !安装和设置 mysql 以从任何地方远程连接不适用于 mysql_secure_installation! ( https://dev.mysql.com/doc/refman/5.5/en/mysql-secure-installation.html ) ( https://dev.mysql.com/doc/refman/5.5/en/mysql-secure-installation.html )

On Ubuntu, Install mysql using:在 Ubuntu 上,使用以下命令安装 mysql:

sudo apt-get install mysql-server

Have just the below in /etc/mysql/my.cnf在 /etc/mysql/my.cnf 中有以下内容

[mysqld]
#### Unix socket settings (making localhost work)
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock

#### TCP Socket settings (making all remote logins work)
port            = 3306
bind-address = 0.0.0.0

Login into DB from server using使用从服务器登录到数据库

mysql -u root -p mysql -u 根 -p

Create DB user using the below statement使用以下语句创建数据库用户

grant all privileges on *.* to ‘username’@‘%’ identified by ‘password’;

Open firewall:打开防火墙:

sudo ufw allow 3306

Restart mysql重启mysql

sudo service mysql restart
GRANT ALL ON *.* to user@'%' IDENTIFIED BY 'password'; 

Will allow a specific user to log on from anywhere.将允许特定用户从任何地方登录。

It's bad because it removes some security control, ie if an account is compromised.这很糟糕,因为它删除了一些安全控制,即如果帐户被盗用。

您还需要在配置文件中禁用以下行:bind-address = 127.0.0.1

mysql> CREATE USER 'monty'@'192.168.%.%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'192.168.%.%'

Mabey you only need: Mabey 你只需要:

Step one:第一步:

grant all privileges on *.* to 'user'@'IP' identified by 'password';

or要么

grant all privileges on *.* to 'user'@'%' identified by 'password';

Step two:第二步:

sudo ufw allow 3306

Step three:第三步:

sudo service mysql restart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM