且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

“拒绝用户访问"将 MySQL 数据库移动到远程服务器后

更新时间:2022-11-27 13:18:35

MySQL 权限基于它们所连接的地址以及用户.所以 root@localhost 和 root@10.4.1.163 将有两组单独的权限.如果您的代码和数据库在同一台服务器上,则将 localhost 更改为 127.0.0.1 作为 num8er 提到的可能会起作用.

MySQL permissions are based on the address which they are connecting to as well as the user. So root@localhost and root@10.4.1.163 will have two separate set of permissions. Changing localhost to 127.0.0.1 as num8er mentioned will probably work if your code and the database are on the same server.

如果您可以通过终端访问您的 php 所在的框,您可以尝试使用以下命令直接连接,排除与 php 相关的任何事情:

If you have terminal access to the box where your php is you could try connecting directly ruling out anything to do with php using this:

mysql -h 10.4.1.163 -u root -p[pass] database -e "SHOW TABLES"

注意 -p 和密码之间没有空格.如果成功,这将为您提供 database 中的表列表.

Note there is no space between -p and the password. If successful this will get you a list of tables in database.

要授予其他用户或其他主机名/IP 的访问权限,您需要按照以下方式运行:(尽管您确实应该根据您的要求创建一个具有更多受限权限的单独用户).

To grant access to other users or for another hostname/IP you will want to run something along the lines of this: (though you should really create a separate user with more restricted permissions based on your requirements).

GRANT ALL PRIVILEGES ON `database`.* TO 'root'@'10.4.1.163';

在此处查看 MySQL GRANT 上的文档 - http://dev.mysql.com/doc/refman/5.7/en/grant.html

Check the docs on MySQL's GRANT here - http://dev.mysql.com/doc/refman/5.7/en/grant.html

附带说明 - 请不要在不至少使用 mysql_real_escape_string (http://php.net/manual/en/function.mysql-real-escape-string.php)在它之前.您还可以查看 PDO(http://php.net/manual/en/book.pdo.php),它通常比现在已弃用的 mysql_ 函数

On a side note - please, please, please don't just pump any old data into a query without at least using mysql_real_escape_string (http://php.net/manual/en/function.mysql-real-escape-string.php) on it before hand. You could also look into PDO (http://php.net/manual/en/book.pdo.php) which is generally preferred over the now deprecated mysql_ functions