授予从任何 IP 地址远程访问 MySQL 数据库的权限

发布于 2024-12-19 11:10:22 字数 240 浏览 2 评论 0原文

我知道这个命令:

GRANT ALL PRIVILEGES
ON database.*
TO 'user'@'yourremotehost'
IDENTIFIED BY 'newpassword';

但是它只允许我授予特定的 IP 地址来访问这个远程 MySQL 数据库。如果我想让任何远程主机都可以访问这个 MySQL 数据库怎么办?我该怎么做?基本上我将这个数据库公开,以便每个人都可以访问它。

I am aware of this command:

GRANT ALL PRIVILEGES
ON database.*
TO 'user'@'yourremotehost'
IDENTIFIED BY 'newpassword';

But then it only allows me to grant a particular IP address to access this remote MySQL database. What if I want it so that any remote host can access this MySQL database? How do I do that? Basically I am making this database public so everyone can access it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

无需解释 2024-12-26 11:10:23

您可以通过编辑文件 /etc/my.cnf 禁用所有安全性:

skip-grant-tables

You can disable all security by editing file /etc/my.cnf:

skip-grant-tables
云淡月浅 2024-12-26 11:10:22
TO 'user'@'%'

% 是通配符 - 如果需要,您还可以执行 '%.domain.example''%.123.123.123' 等操作。

TO 'user'@'%'

% is a wildcard - you can also do '%.domain.example' or '%.123.123.123' and things like that if you need.

紙鸢 2024-12-26 11:10:22

此内容最初来自https://rtcamp.com/tutorials/mysql/remote -访问/

启用远程访问(授予)
Home / 教程 / Mysql / 启用远程访问(授予)
如果您尝试从远程计算机连接到 mysql 服务器,并遇到如下错误,那么这篇文章适合您。

错误 1130 (HY000):不允许主机“1.2.3.4”连接到此
MySQL服务器

更改mysql配置

从编辑mysql配置文件开始

vim /etc/mysql/my.cnf

注释掉以下行。

#bind-address           = 127.0.0.1
#skip-networking

如果您没有找到skip-networking行,请添加它并注释掉它。

重新启动 mysql 服务器。

~ /etc/init.d/mysql restart

更改 GRANT 权限

您可能会惊讶地发现,即使在进行上述更改后,您也无法获得远程访问权限或获得访问权限但无法访问所有数据库。

默认情况下,您使用的 mysql 用户名和密码允许在本地访问 mysql-server。所以需要更新权限。 (如果您想为此目的创建单独的用户,可以使用 CREATE USER 'USERNAME'@'localhost' IDENTIFIED BY 'PASSWORD';)

运行如下命令以从所有计算机进行访问。 (将 USERNAMEPASSWORD 替换为您的凭据。)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

运行如下命令以授予来自特定 IP 的访问权限。 (将 USERNAMEPASSWORD 替换为您的凭据。)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'1.2.3.4' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

您可以将 1.2.3.4 替换为您的 IP。您可以多次运行上述命令以授予多个 IP 的访问权限。

您还可以指定单独的USERNAME &用于远程访问的PASSWORD

您可以通过以下方式检查最终结果:

SELECT * from information_schema.user_privileges where grantee like "'USERNAME'%";

最后,您可能还需要运行:

mysql> FLUSH PRIVILEGES;

测试连接

从终端/命令行

mysql -h HOST -u USERNAME -pPASSWORD

:如果您获得 mysql shell,请不要忘记运行 showdatabases;检查您是否具有远程计算机的正确权限。

额外提示:撤销访问权限

如果您不小心向用户授予了访问权限,那么最好方便地使用撤销选项。

以下将从所有计算机上撤销 USERNAME 的所有选项:

mysql> REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'USERNAME'@'%';
Following will revoke all options for USERNAME from particular IP:

mysql> REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'USERNAME'@'1.2.3.4';
Its better to check information_schema.user_privileges table after running REVOKE command.

如果您在运行 REVOKE 命令后看到 USAGE 权限,那就没问题了。这和没有特权一样好。我不确定是否可以撤销。

This content was originally from https://rtcamp.com/tutorials/mysql/remote-access/

Enable Remote Access (Grant)
Home / Tutorials / Mysql / Enable Remote Access (Grant)
If you try to connect to your mysql server from remote machine, and run into error like below, this article is for you.

ERROR 1130 (HY000): Host ‘1.2.3.4’ is not allowed to connect to this
MySQL server

Change mysql config

Start with editing mysql config file

vim /etc/mysql/my.cnf

Comment out following lines.

#bind-address           = 127.0.0.1
#skip-networking

If you do not find skip-networking line, add it and comment out it.

Restart mysql server.

~ /etc/init.d/mysql restart

Change GRANT privilege

You may be surprised to see even after above change you are not getting remote access or getting access but not able to all databases.

By default, mysql username and password you are using is allowed to access mysql-server locally. So need to update privilege. (if you want to create a separate user for that purpose, you can use CREATE USER 'USERNAME'@'localhost' IDENTIFIED BY 'PASSWORD';)

Run a command like below to access from all machines. (Replace USERNAME and PASSWORD by your credentials.)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

Run a command like below to give access from specific IP. (Replace USERNAME and PASSWORD by your credentials.)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'1.2.3.4' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

You can replace 1.2.3.4 with your IP. You can run above command many times to GRANT access from multiple IPs.

You can also specify a separate USERNAME & PASSWORD for remote access.

You can check final outcome by:

SELECT * from information_schema.user_privileges where grantee like "'USERNAME'%";

Finally, you may also need to run:

mysql> FLUSH PRIVILEGES;

Test Connection

From terminal/command-line:

mysql -h HOST -u USERNAME -pPASSWORD

If you get a mysql shell, don’t forget to run show databases; to check if you have right privileges from remote machines.

Bonus-Tip: Revoke Access

If you accidentally grant access to a user, then better have revoking option handy.

Following will revoke all options for USERNAME from all machines:

mysql> REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'USERNAME'@'%';
Following will revoke all options for USERNAME from particular IP:

mysql> REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'USERNAME'@'1.2.3.4';
Its better to check information_schema.user_privileges table after running REVOKE command.

If you see USAGE privilege after running REVOKE command, its fine. It is as good as no privilege at all. I am not sure if it can be revoked.

无人接听 2024-12-26 11:10:22

为了能够从任何 IP 地址与您的用户连接,请执行以下操作:

允许 MySQL 服务器接受远程连接。为此,打开mysqld.conf文件:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

搜索以“bind-address”开头的行并将其值设置为0.0.0.0:

bind-address                    = 0.0.0.0

最后保存文件。

注意:如果您运行的是 MySQL 8+,默认情况下 bind-address 指令可能不在 mysqld.cnf 文件中。在这种情况下,将该指令添加到文件 /etc/mysql/mysql.conf.d/mysqld.cnf 的底部。

现在重新启动 MySQL 服务器,可以使用 systemd 或使用旧的服务 命令。这取决于您的操作系统:

sudo systemctl restart mysql # for ubuntu
sudo systemctl restart mysqld.service # for debian

最后,MySQL 服务器现在能够接受远程连接。

现在我们需要创建一个用户并授予其权限,这样我们就可以使用该用户远程登录。

以 root 或任何其他具有 root 权限的用户身份连接到 MySQL 数据库。

mysql -u root -p # for password based login
sudo mysql -u root # if authentication is done using  auth_socket

现在在 localhost 和 '%' 通配符中创建所需的用户,并授予对所有数据库的权限这样的。

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypass';
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypass';

然后,

GRANT ALL ON *.* TO 'myuser'@'localhost';
GRANT ALL ON *.* TO 'myuser'@'%';

最后不要忘记刷新权限

FLUSH PRIVILEGES;

注意:如果您在数据库服务器上配置了防火墙,则还需要打开端口 3306 MySQL 的默认端口以允许流量到达 MySQL。

To be able to connect with your user from any IP address, do the following:

Allow MySQL server to accept remote connections. For this, open the mysqld.conf file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Search for the line starting with "bind-address" and set its value to 0.0.0.0:

bind-address                    = 0.0.0.0

And finally save the file.

Note: If you’re running MySQL 8+, the bind-address directive may not be in the mysqld.cnf file by default. In this case, add the directive to the bottom of the file /etc/mysql/mysql.conf.d/mysqld.cnf.

Now restart the MySQL server, either with systemd or use the older service command. This depends on your operating system:

sudo systemctl restart mysql # for ubuntu
sudo systemctl restart mysqld.service # for debian

Finally, MySQL server is now able to accept remote connections.

Now we need to create a user and grant it permission, so we can be able to login with this user remotely.

Connect to MySQL database as root, or any other user with the root privilege.

mysql -u root -p # for password based login
sudo mysql -u root # if authentication is done using  auth_socket

Now create the desired user in both localhost and '%' wildcard and grant permissions on all DB's as such.

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypass';
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypass';

Then,

GRANT ALL ON *.* TO 'myuser'@'localhost';
GRANT ALL ON *.* TO 'myuser'@'%';

And finally don't forget to flush privileges

FLUSH PRIVILEGES;

Note: If you’ve configured a firewall on your database server, you will also need to open port 3306 MySQL’s default port to allow traffic to MySQL.

烟─花易冷 2024-12-26 11:10:22

假设上述步骤完成,MySQL 3306端口可以自由远程访问。不要忘记在MySQL配置文件中绑定公网IP地址。

例如,在我的 Ubuntu 服务器上,作为 root

nano /etc/mysql/my.cnf

在文件中,搜索[mysqld]节块并添加新的绑定地址。在此示例中,它是 192.168.0.116。它看起来像这样:

......
.....
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.

bind-address        = 127.0.0.1
bind-address        = 192.168.0.116

.....
......

您可以删除 localhost (127.0.0.1) 绑定,如果您选择,但随后您必须专门给出一个 IP 地址来访问本地计算机上的服务器。

最后一步是重新启动 MySQL 服务器(在 Ubuntu 上):

stop mysql

start mysql

对于其他系统,或者 #/etc/init.d/mysql restart

现在可以通过以下方式远程访问 MySQL 数据库:

mysql -u username -h 192.168.0.116 -p

Assuming that the above step is completed and MySQL port 3306 is free to be accessed remotely. Don't forget to bind the public IP address in the MySQL configuration file.

For example, on my Ubuntu server, as root:

nano /etc/mysql/my.cnf

In the file, search for the [mysqld] section block and add the new bind address. In this example, it is 192.168.0.116. It would look something like this:

......
.....
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.

bind-address        = 127.0.0.1
bind-address        = 192.168.0.116

.....
......

You can remove the localhost (127.0.0.1) binding if you choose, but then you have to specifically give an IP address to access the server on the local machine.

Then the last step is to restart the MySQL server (on Ubuntu):

stop mysql

start mysql

Or #/etc/init.d/mysql restart for other systems.

Now the MySQL database can be accessed remotely by:

mysql -u username -h 192.168.0.116 -p
演出会有结束 2024-12-26 11:10:22

需要更改配置文件才能通过 localhost 启用连接。

要通过远程 IP 地址进行连接,请以“root”用户身份登录并在 MySQL 中运行以下查询。

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;

CREATE USER 'username'@'%' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;

这将创建一个可在本地主机以及远程 IP 上访问的新用户。

另外,注释位于 /etc/mysql/my.cnf 中的 my.cnf 文件中的以下行:

bind-address = 127.0.0.1

使用以下命令重新启动 MySQL 服务器:

sudo service mysql restart

现在您应该能够连接远程连接到您的 MySQL 服务器。

Configuration file changes are required to enable connections via localhost.

To connect through remote IP addresses, log in as a "root" user and run the below queries in MySQL.

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;

CREATE USER 'username'@'%' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;

This will create a new user that is accessible on localhost as well as from remote IPs.

Also, comment the below line from your my.cnf file located in /etc/mysql/my.cnf:

bind-address = 127.0.0.1

Restart your MySQL server using:

sudo service mysql restart

Now you should be able to connect remotely to your MySQL server.

中二柚 2024-12-26 11:10:22

以下是我如何授予权限:

GRANT ALL ON yourdatabasename.* TO root@'%' IDENTIFIED BY
'yourRootPassword';

如上所述,% 是一个通配符,这将允许任何 IP 地址连接到您的数据库。我在这里所做的假设是,当您连接时,您将拥有一个名为 root 的用户(尽管这是默认值)。输入 root 密码就可以了。请注意,我在用户 root 周围没有任何单引号 (')。

Here is how I got to grant the privileges:

GRANT ALL ON yourdatabasename.* TO root@'%' IDENTIFIED BY
'yourRootPassword';

As noted, % is a wildcard and this will allow any IP address to connect to your database. The assumption I make here is when you connect you'll have a user named root (which is the default though). Feed in the root password and you are good to go. Note that I don't have any single quotes (') around the user root.

小…红帽 2024-12-26 11:10:22

使用此命令:

GRANT ALL ON yourdatabasename.* TO root@'%' IDENTIFIED BY 'yourRootPassword';

然后:

FLUSH PRIVILEGES; 

然后注释掉文件“/etc/mysql/mysql.conf.d/mysqld.cnf”中的以下行(必需!):

bind-address = 127.0.0.1 

对我有用!

Use this command:

GRANT ALL ON yourdatabasename.* TO root@'%' IDENTIFIED BY 'yourRootPassword';

Then:

FLUSH PRIVILEGES; 

Then comment out the below line in file "/etc/mysql/mysql.conf.d/mysqld.cnf" (is required!):

bind-address = 127.0.0.1 

Works for me!

别想她 2024-12-26 11:10:22

运行以下命令:

mysql -u root -p

MySQL 客户端会话:

GRANT ALL ON *.* to root@'ipaddress' IDENTIFIED BY 'mysql root password';
FLUSH PRIVILEGES;
exit

然后尝试从您指定的 IP 地址进行连接:

mysql -h address-of-remove-server -u root -p

您应该能够连接。

Run the following:

mysql -u root -p

MySQL client session:

GRANT ALL ON *.* to root@'ipaddress' IDENTIFIED BY 'mysql root password';
FLUSH PRIVILEGES;
exit

Then attempt a connection from the IP address you specified:

mysql -h address-of-remove-server -u root -p

You should be able to connect.

一身软味 2024-12-26 11:10:22

您可以通过此命令解决MariaDB的问题:

注意:

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

% 是通配符。在本例中,它指的是所有 IP 地址。

You can slove the problem of MariaDB via this command:

Note:

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

% is a wildcard. In this case, it refers to all IP addresses.

浅唱ヾ落雨殇 2024-12-26 11:10:22

使用 MySQL 服务器 8 远程访问数据库:

CREATE USER 'root'@'%' IDENTIFIED BY 'Pswword@123';

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;

To remotely access a database with MySQL server 8:

CREATE USER 'root'@'%' IDENTIFIED BY 'Pswword@123';

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;
和影子一齐双人舞 2024-12-26 11:10:22
GRANT ALL PRIVILEGES ON *.* TO 'user'@'ipadress'
GRANT ALL PRIVILEGES ON *.* TO 'user'@'ipadress'
岁月苍老的讽刺 2024-12-26 11:10:22
  • 使用管理员用户启动 MYSQL
    • mysql -u admin-user -p(提示时输入密码)
  • 创建一个新用户:
    • 创建由“密码”标识的用户“newuser”@“%”; (% -> 任何主机)
  • 授予权限:
    • 将 db_name.* 上的选择、删除、插入、更新授予“newuser”@“%”;
    • 同花顺特权;

如果您正在运行 EC2 实例,请不要忘记使用 MYSQL/Aurura 在安全组中添加入站规则。

  • START MYSQL using admin user
    • mysql -u admin-user -p (ENTER PASSWORD ON PROMPT)
  • Create a new user:
    • CREATE USER 'newuser'@'%' IDENTIFIED BY 'password'; (% -> anyhost)
  • Grant Privileges:
    • GRANT SELECT,DELETE,INSERT,UPDATE ON db_name.* TO 'newuser'@'%';
    • FLUSH PRIVILEGES;

If you are running EC2 instance don't forget to add the inbound rules in security group with MYSQL/Aurura.

幸福还没到 2024-12-26 11:10:22

编辑文件:

/etc/mysql/percona-server.cnf

在文件中附加以下代码。

<代码>[mysqld]
bind-address = 0.0.0.0

创建用于远程访问的用户。

$ mysql -u root -p      
mysql> GRANT ALL ON *.* to snippetbucketdotcom@'%' IDENTIFIED BY 'tejastank';   
mysql> FLUSH PRIVILEGES;    
mysql> exit

所有 linux 服务器都可以工作,

对于 MSWin c:\ 查找安装位置 \ 文件路径

Edit File:

/etc/mysql/percona-server.cnf

Append below code in file.

[mysqld]
bind-address = 0.0.0.0

Create user for remote access.

$ mysql -u root -p      
mysql> GRANT ALL ON *.* to snippetbucketdotcom@'%' IDENTIFIED BY 'tejastank';   
mysql> FLUSH PRIVILEGES;    
mysql> exit

All linux server works,

For MSWin c:\ Find insatallation location \ file path

娇俏 2024-12-26 11:10:22

只需将用户创建到某个数据库,例如

GRANT ALL PRIVILEGES ON.* TO ''@'%' IDENTIFIED BY ''

然后转到

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf 并更改行 bind-address = 127.0.0.1bind-address = 0.0.0.0

之后,您可以从任何 IP 连接到该数据库。

Just create the user to some database like

GRANT ALL PRIVILEGES ON <database_name>.* TO '<username>'@'%' IDENTIFIED BY '<password>'

Then go to

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf and change the line bind-address = 127.0.0.1 to bind-address = 0.0.0.0

After that you may connect to that database from any IP.

千年*琉璃梦 2024-12-26 11:10:22

打开您的mysql控制台并执行以下命令(输入您的数据库名称、用户名和密码):

将您的数据库名称.* 上的所有内容授予由 IDENTIFIED BY 管理@'%'
'你的根密码';

然后执行:

同花顺特权;

打开命令行并使用具有root权限的任何编辑器打开文件/etc/mysql/mysql.conf.d/mysqld.cnf

例如:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

然后注释掉以下行:

绑定地址 = 127.0.0.1

使用命令重新启动 mysql 以反映更改:

sudo service mysql restart

Enjoy ;)

Open your mysql console and execute the following command (enter your database name,username and password):

GRANT ALL ON yourdatabasename.* TO admin@'%' IDENTIFIED BY
'yourRootPassword';

Then Execute:

FLUSH PRIVILEGES;

Open command line and open the file /etc/mysql/mysql.conf.d/mysqld.cnf using any editor with root privileges.

For example:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Then comment out the below line:

bind-address = 127.0.0.1

Restart mysql to reflect the changes using command:

sudo service mysql restart

Enjoy ;)

倾城花音 2024-12-26 11:10:22

您需要更改 mysql 配置文件:

从编辑 mysql 配置文件开始

vim /etc/mysql/my.cnf

添加:

bind-address = 0.0.0.0

You need to change the mysql config file:

Start with editing mysql config file

vim /etc/mysql/my.cnf

add:

bind-address = 0.0.0.0
〗斷ホ乔殘χμё〖 2024-12-26 11:10:22

在 Ubuntu 上有效的方法是向用户授予所有权限:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;

并在 /etc/mysql/mysql.conf.d/mysqld.cnf 中设置绑定地址:

bind-address            = 0.0.0.0

然后重新启动 mysql 守护进程:

service mysql restart

what worked for on Ubuntu is granting all privileges to the user:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;

and setting the bind address in /etc/mysql/mysql.conf.d/mysqld.cnf:

bind-address            = 0.0.0.0

then restarting the mysql daemon:

service mysql restart
々眼睛长脚气 2024-12-26 11:10:22

然后转到这个目录“/etc/mysql/mysql.conf.d
编辑此文件“mysqld.cnf

$nano mysqld.cnf

bind-address            = 127.0.0.1

mysqlx-bind-address     = 127.0.0.1

编辑为

bind-address            = 0.0.0.0

mysqlx-bind-address     = 0.0.0.0

Go to this directory "/etc/mysql/mysql.conf.d" then
edit this file " mysqld.cnf"

$nano mysqld.cnf

bind-address            = 127.0.0.1

mysqlx-bind-address     = 127.0.0.1

edit to

bind-address            = 0.0.0.0

mysqlx-bind-address     = 0.0.0.0
风向决定发型 2024-12-26 11:10:22

例如在我的 CentOS 中

sudo gedit /etc/mysql/my.cnf

注释掉以下行

#bind-地址 = 127.0.0.1

然后

sudo 服务 mysqld 重新启动

For example in my CentOS

sudo gedit /etc/mysql/my.cnf

comment out the following lines

#bind-address = 127.0.0.1

then

sudo service mysqld restart

跨年 2024-12-26 11:10:22

如果您想从任何 IP 地址授予对数据库的远程访问权限,请运行 mysql 命令,然后运行以下命令。

GRANT ALL PRIVILEGES ON *.*
TO 'root'@'%' 
IDENTIFIED BY 'password' 
WITH GRANT OPTION;

If you want to grant remote access of your database from any IP address, run the mysql command and after that run the following command.

GRANT ALL PRIVILEGES ON *.*
TO 'root'@'%' 
IDENTIFIED BY 'password' 
WITH GRANT OPTION;
隱形的亼 2024-12-26 11:10:22

我看到有很多答案,但除了接受的答案很短且缺乏解释之外,它们都很长。由于我无法编辑它,我正在添加我的答案。阿迪特询问:

公开此数据库,以便每个人都可以访问它

GRANT ALL PRIVILEGES
ON database.*
TO 'username'@'remote_host'
IDENTIFIED BY 'password';

上面的代码为来自给定远程主机的用户授予权限,您可以通过更改 TO 'username'@'yourremotehost' 来允许用户从任何远程主机连接到 MySQL code> 到 TO '用户名'@'%'

因此,向用户授予从任何远程主机连接的权限的更正查询是:

GRANT ALL PRIVILEGES
ON database.*
TO 'username'@'%'
IDENTIFIED BY 'password';

I see there are many answers, but they are quite long ones except for the accepted answer, which is quite short and lacks explanation. As I can't edit it, I am adding my answer. Adit asked about:

making this database public so everyone can access it

GRANT ALL PRIVILEGES
ON database.*
TO 'username'@'remote_host'
IDENTIFIED BY 'password';

Above code grants permissions for a user from a given remote host, you can allow a user to connect from any remote host to MySQL by changing TO 'username'@'yourremotehost' to TO 'username'@'%'.

So, the corrected query for granting permissions to a user to connect from any remote host is:

GRANT ALL PRIVILEGES
ON database.*
TO 'username'@'%'
IDENTIFIED BY 'password';
挽你眉间 2024-12-26 11:10:22

在网站面板中,例如 cPanel,您可以添加一个 % (百分号)允许访问 MySQL 数据库的主机名。

通过添加一个 %,您可以从任何 IP 地址或网站甚至桌面应用程序访问您的数据库。

In website panels, like cPanel, you may add a single % (percentage sign) in allowed hostnames to access your MySQL database.

By adding a single %, you can access your database from any IP address or website even from desktop applications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文