我已经在我的本地机器上安装了MySQL Community Edition 5.5,我想允许远程连接,这样我就可以从外部源连接。
我该怎么做呢?
我已经在我的本地机器上安装了MySQL Community Edition 5.5,我想允许远程连接,这样我就可以从外部源连接。
我该怎么做呢?
当前回答
请按照下面提到的步骤来设置MySQL用户的通配符远程访问。
(1) Open cmd. (2) navigate to path C:\Program Files\MySQL\MySQL Server 5.X\bin and run this command. mysql -u root -p (3) Enter the root password. (4) Execute the following command to provide the permission. GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'IP' IDENTIFIED BY 'PASSWORD'; USERNAME: Username you wish to connect to MySQL server. IP: Public IP address from where you wish to allow access to MySQL server. PASSWORD: Password of the username used. IP can be replaced with % to allow user to connect from any IP address. (5) Flush the previleges by following command and exit. FLUSH PRIVILEGES; exit; or \q
其他回答
启用远程根访问可能是危险的。如果您要设置具有更严格权限的用户帐户,则更可取。以下三个步骤可以做到这一点。
Ensure that the line starting with bind-address ... is at least commented out in your my.ini or my.cnf file. If it doesn't exist, move on. You can find this file in C:\ProgramData\MySQL\MySQL Server 8.0 on Windows. Afterwards, check that the user account you are establishing the connection with does not have localhost in the Limit to Hosts Matching field. While it isn't recommended, you can instead put % in that field for testing purposes. You can do this by opening a local connection to the server with MySQL Workbench, then going to Server>Users and Privileges from the menu bar and finding the user account you want to connect with.
“限制主机匹配”字段是不允许您连接非本地。也就是说,它将接受的连接限制为一个IP地址模式。理想情况下,您应该从静态IP地址或子网访问MySQL服务器,这样您就可以尽可能地限制。
显然,您的防火墙应该允许MySQL服务器应用程序通过您想要的端口进行通信。您和服务器之间的物理网络设备应该允许在您想要连接的端口上进行通信。(通常端口为3306)
在我的情况下,我试图连接到一个远程mysql服务器在cent操作系统。在经历了许多解决方案(授予所有特权,删除ip绑定,启用网络)后,问题仍然没有得到解决。
事实证明,在寻找各种解决方案时,我遇到了iptables,这让我意识到mysql端口3306不接受连接。
下面是关于我如何检查和解决这个问题的一个小说明。
检查端口是否接受连接:
telnet (mysql server ip) [portNo]
添加ip表规则,允许端口上的连接:
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
不建议在生产环境中这样做,但如果您的iptables没有正确配置,添加规则可能仍然不能解决问题。在这种情况下,应该做以下工作:
service iptables stop
希望这能有所帮助。
对于OS X用户,请注意bind-address参数通常是在launchd plist中设置的,而不是在my.ini文件中。所以在我的情况下,我删除了<string>——bind-address=127.0.0.1</string>从/Library/LaunchDaemons/homebrew.mx .mariadb.plist。
当我在一个Java项目中使用MySQL服务器作为数据库时,我遇到了这个挑战。
我是这样做的:
首先,确认您的MySQL服务器配置允许远程连接。使用您喜欢的文本编辑器打开MySQL服务器配置文件:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
向下滚动到绑定地址行,并确保它要么被注释掉,要么被替换为0.0.0.0(以允许所有远程连接),要么被替换为您想要远程连接的ip -address。
完成必要的更改后,保存并退出配置文件。通过重新启动MySQL服务,应用MySQL配置文件所做的更改:
sudo systemctl restart mysql
接下来,登录到已安装的服务器上的MySQL服务器控制台:
mysql -u root -p
输入mysql用户密码
检查您想要的用户已经访问的主机。在我的例子中,用户是root:
SELECT host FROM mysql.user WHERE user = "root";
这给了我这样的输出:
+-----------+
| host |
+-----------+
| localhost |
+-----------+
接下来,我运行下面的命令,授予root用户对名为my_database的数据库的远程访问权限:
USE my_database;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'my-password';
注意:%授予用户从网络上的所有主机进行远程访问。您可以使用命令grant ALL PRIVILEGES ON *指定要授予用户访问权限的单个主机的Ip-Address。*“root”@“ip地址”IDENTIFIED BY“my-password”;
之后,我检查了用户现在已经访问的主机。在我的例子中,用户是root:
SELECT host FROM mysql.user WHERE user = "root";
这给了我这样的输出:
+-----------+
| host |
+-----------+
| % |
| localhost |
+-----------+
最后,您可以尝试从另一个服务器使用命令连接到MySQL服务器:
mysql -u username -h mysql-server-ip-address -p
其中u代表用户,h代表mysql-server-ip-address, p代表密码。所以我的情况是:
mysql -u root -h 34.69.261.158 -p
输入mysql用户密码
根据你的MySQL服务器版本,你应该得到这个输出:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
参考资料:如何允许远程连接到MySQL
这是所有。
我希望这对你们有帮助
如果mysqld有一个绑定地址设置为环回/本地地址(例如127.0.0.1),服务器将无法从远程主机到达,因为环回接口无法从任何远程主机到达。
将此选项设置为0.0.0.0(::对于IPv4+6)以接受来自任何主机的连接,如果您希望只允许在一个接口上连接,则将该选项设置为另一个外部可达地址。
源