dongmu3269 2016-11-01 10:59
浏览 8
已采纳

MySQLi忽略主机参数

This is my setup (the IP numbers are fictional of course):

Server A (10.0.0.1)

hosts a database called database1 with the user db_user and the password db_pass. This user has access to the database and remote connections from any host are permitted (I know it's a leak and I will fix it once it works)

Update 1: This server shows no signs of receiving the connection (like connection refused or something like that) Port 3306 is open

Server B (20.0.20.0) hosts a PHP script which connects to the database with the following command:

$connection = mysqli_connect("10.0.0.1","db_user","db_pass","database1",3306);

My log on Server B says:

Access denied for user 'db_user'@'20.0.20.0' (using password: YES) in <path-to-php-file> in line 42

The line number matches the statement, so it is indeed the statement above which fails.

Why? I explicitly specified the IP of Server A (also tried server-a.com instead of 10.0.0.1)

Update 2: I ran the following query via commandline as MySQL-Root and this is the output:

mysql> SHOW GRANTS FOR 'db_user';
GRANT USAGE ON *.* TO 'db_user'@'%' IDENTIFIED BY PASSWORD '<password hash>'
GRANT ALL PRIVILEGES ON `database1`.* TO 'db_user1'@'%'

Seems valid to me. What strikes me as odd is that in the log of B it shows it own address (B's address) instead of A's where the Database is located. My idea is it tries to connect to a database on server B where no MySQL user db_user exists.

Update 3: I connected via SSH to server B and ran mysql --host=10.0.0.1 -udb_user -p and typed in the password => it worked. SHOW GRANTS FOR current_user; returned the same like on server A.

  • 写回答

1条回答 默认 最新

  • doucong1268 2016-11-01 15:44
    关注

    If you can't connect using mysqli, try using PDO instead. I'm not sure why, but apparently in this case PDO works.

    Personally, I like PDO better than mysqli, because of named parameters instead of ?, and the ability to provide an array of values when calling PDOStatement::execute(). You may find you like it as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看