MySQL Mysql添加远程访问用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16287559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:29:12  来源:igfitidea点击:

Mysql adding user for remote access

mysqlremote-access

提问by user2333586

I created user user@'%'with password 'password. But I can not connect with:

user@'%'password 'password. 但我无法连接:

mysql_connect('localhost:3306', 'user', 'password');

When I created user user@'localhost', I was able to connect. Why? Doesn't '%' mean from ANY host?

当我创建 user 时user@'localhost',我能够连接。为什么?'%' 不是来自任何主机吗?

回答by apesa

In order to connect remotely you have to have MySQL bind port 3306 to your machine's IP address in my.cnf. Then you have to have created the user in both localhost and '%' wildcard and grant permissions on all DB's as such .See below:

为了远程连接,您必须让 MySQL 将端口 3306 绑定到 my.cnf 中您机器的 IP 地址。然后,您必须在 localhost 和 '%' 通配符中创建用户,并授予对所有数据库的权限见下文:

my.cnf (my.ini on windows)

my.cnf(Windows 上的 my.ini)

#Replace xxx with your IP Address 
bind-address        = xxx.xxx.xxx.xxx

then

然后

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'@'%';
flush privileges;

Depending on your OS you may have to open port 3306 to allow remote connections.

根据您的操作系统,您可能必须打开端口 3306 以允许远程连接。

回答by MSS

Follow instructions (steps 1 to 3 don't needed in windows):

按照说明操作(Windows 中不需要步骤 1 到 3):

  1. Find mysql config to edit:

    /etc/mysql/my.cnf (Mysql 5.5)

    /etc/mysql/conf.d/mysql.cnf (Mysql 5.6+)

  2. Find bind-address=127.0.0.1in config file change bind-address=0.0.0.0(you can set bind address to one of your interface ips or like me use 0.0.0.0)

  3. Restart mysql service run on console: service restart mysql

  4. Create a user with a safe password for remote connection. To do this run following command in mysql (if you are linux user to reach mysql console run mysqland if you set password for root run mysql -p):

    GRANT ALL PRIVILEGES 
     ON *.* TO 'remote'@'%' 
     IDENTIFIED BY 'safe_password' 
     WITH GRANT OPTION;`
    
  1. 找到要编辑的 mysql 配置:

    /etc/mysql/my.cnf (Mysql 5.5)

    /etc/mysql/conf.d/mysql.cnf (Mysql 5.6+)

  2. bind-address=127.0.0.1在配置文件更改中查找bind-address=0.0.0.0(您可以将绑定地址设置为您的接口 ips 之一或像我一样使用 0.0.0.0)

  3. 重新启动在控制台上运行的 mysql 服务: service restart mysql

  4. 创建具有安全密码的用户以进行远程连接。要执行此操作,请在 mysql 中运行以下命令(如果您是 linux 用户以访问 mysql 控制台运行mysql,并且您为 root 运行设置了密码mysql -p):

    GRANT ALL PRIVILEGES 
     ON *.* TO 'remote'@'%' 
     IDENTIFIED BY 'safe_password' 
     WITH GRANT OPTION;`
    

Now you should have a user with name of userand password of safe_passwordwith capability of remote connect.

现在您应该有一个具有远程连接功能的用户名user和密码safe_password

回答by caramba

for what DB is the user? look at this example

用户是什么数据库?看看这个例子

mysql> create database databasename;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on databasename.* to cmsuser@localhost identified by 'password';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

so to return to you question the "%" operator means all computers in your network.

所以回到你的问题,“%”运算符意味着你网络中的所有计算机。

like aspesa shows I'm also sure that you have to create or update a user. look for all your mysql users:

就像 aspesa 显示的那样,我也确信您必须创建或更新用户。查找您所有的 mysql 用户:

SELECT user,password,host FROM user;

as soon as you got your user set up you should be able to connect like this:

设置好用户后,您应该能够像这样连接:

mysql -h localhost -u gmeier -p

hope it helps

希望能帮助到你