简体   繁体   English

如何使用PHP从其他主机连接到MySQL数据库

[英]How to connect to MySQL database from other host using PHP

I need to connect to MySQL using PHP, but database is on a other host , I can't do It correctly. 我需要使用PHP连接到MySQL,但是数据库在另一台主机上,我无法正确执行。

I'm useing this code. 我正在使用此代码。 Error is in this line: 错误在这一行:

$mysqli = new mysqli("hostex.lt","myInfo","pass","myInfoo");

"hostex.lt". “ hostex.lt”。 Database is in hostex.lt, phpMyAdmin: myadmin.hostex.lt I've tried It but It always print error:error:error:error:error:error:error:error:error:error: 数据库在hostex.lt中,phpMyAdmin:myadmin.hostex.lt我已经尝试过,但它总是打印error:error:error:error:error:error:error:error:error:error:

This code working in other project where I use localhost, so here is problem. 此代码在我使用localhost的其他项目中有效,因此这里是问题。

        for($i=1;$i<=10;$i++)
        {
    if ($stmt = $mysqli->prepare("INSERT into game (taskai, userName) VALUE (?,?) ")) {
    if (!$mysqli->set_charset("utf8")) {
        printf("Error loading character set utf8: %s\n", $mysqli->error);
    } else {
    //    printf(" ", $mysqli->character_set_name());
    }


    $stmt->bind_param('ss', $score, $name);
      // $stmt->bind_param("s", $username);

       $stmt->execute();

       if ($stmt->error != '') {
           echo ' error:'.$stmt->error;
       } else {
           echo 'success';
       }
       $stmt->close();
    } else {
       echo 'error:'.$mysqli->error;
    }
    }
    $mysqli->close();
    ?>

UPDATE 更新

I've got this error: 我有这个错误:

错误

By default MySQL only allows access from localhost, if you want to access it from a different host you have to allow it explicitly. 默认情况下,MySQL只允许从本地主机访问,如果要从其他主机访问它,则必须明确允许它。

Log into PHPMyAdmin, go to Privileges, add a user 'myDbName'@'www.ftpDomainName.lt' (the host part after the @ is important) and give it access to your database. 登录到PHPMyAdmin,转到Privileges,添加用户'myDbName'@'www.ftpDomainName.lt'(@后的主机部分很重要),并授予其对数据库的访问权限。

If you don't have access to the Privileges section in your PHPMyAdmin you will have to contact the hoster of your database. 如果您无权访问PHPMyAdmin中的“特权”部分,则必须联系数据库的托管人。

This is unlikely to be a PHP problem per se , but most probably something on the database server that is blocking the connection. 本质上 ,这不太可能是PHP问题,但很可能是数据库服务器上的某些问题阻止了连接。

Try the following in this order, running your code after each step: 按此顺序尝试以下操作,在每个步骤之后运行代码:

New MySQL user 新的MySQL用户

  • Add a new database user via PHPMyAdmin. 通过PHPMyAdmin添加新的数据库用户。
  • Go to the Privileges and then add user. 转到特权,然后添加用户。
  • The Host can be set to a '%' (wildcard - allows all servers access. This is less secure but can be tightened up later). 主机可以设置为'%'(通配符-允许所有服务器访问。安全性较差,但以后可以加强)。
  • Fill in a username and password which you will then use in the PHP code. 填写用户名和密码,然后将其用于PHP代码。
  • Retry your code. 重试您的代码。

Database Server 数据库服务器

On the database server ask the sysadmin to edit /etc/mysql/my.cnf (assumption is that this is a Linux machine with the my.cnf stored at this location -- your sysadmin will know how to find the correct file). 在数据库服务器上,请系统管理员编辑/etc/mysql/my.cnf(假设这是一台Linux计算机,其my.cnf存储在此位置-系统管理员将知道如何查找正确的文件)。

  • Comment out the sections 'bind-address = 127.0.0.1' and 'skip-networking' (if its there.) using a # character at the start of the line. 在行的开头用#字符注释掉“ bind-address = 127.0.0.1”和“ skip-networking”(如果存在)两部分。
  • Save the file 保存文件
  • Restart the MySQL service (service mysql restart). 重新启动MySQL服务(服务mysql重新启动)。
  • Retry your code 重试您的代码

Firewall 防火墙功能

If the above steps haven't worked, then most likely a firewall is blocking the connection. 如果上述步骤不起作用,则很可能是防火墙阻止了该连接。

  • On the server, allow port 3306 to accept incoming connections 在服务器上,允许端口3306接受传入的连接
  • If there is a shared firewall device or the cloud hosting partner (eg. AWS) has a firewall, allow port 3306 如果存在共享的防火墙设备或云托管合作伙伴(例如,AWS)具有防火墙,请允许端口3306
  • Retry your code. 重试您的代码。

Note that if the sysadmin of the MySQL server does not want to open the port on the firewall or enable external connections to the database, there is not a lot you can do 请注意,如果MySQL服务器的sysadmin不想打开防火墙上的端口或启用与数据库的外部连接,则您无能为力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM