【问题标题】:Creating a Secure Connection to a Server using PHP使用 PHP 创建与服务器的安全连接
【发布时间】:2010-11-02 17:59:44
【问题描述】:

我正在尝试查询与我有远程桌面连接的服务器上的数据库。能够使用我的 php Web 应用程序查询此服务器上的数据库需要什么。

【问题讨论】:

    标签: php sql connection


    【解决方案1】:

    你需要:

    1. 数据库服务器应用程序配置为允许来自外部主机的连接

    2. 调整服务器端网络配置以允许来自外部主机(防火墙、NAT 等)的连接

    3. 数据库用户,被授予访问您要使用的数据库的权限

    4. PHP 应用程序,它以适当的用户连接到您的数据库服务器

    详细信息取决于您使用的数据库服务器。

    【讨论】:

      【解决方案2】:
      $link = mysql_connect($host, $username, $password);
      

      顺便说一句,没有什么是安全的。

      【讨论】:

        【解决方案3】:

        我使用类似的东西,但它远非安全。我将它保存在一个单独的“connection.php”中,其他文件需要一次。

        $db_host = 'INSERT IP HERE';
        $db_user = 'User';
        //My sql password for testing purposes
        $db_pass = 'PASSWORD';
        //name of the database
        $db_name = 'dbname';
        
        //connection
        $conn = new mysqli($db_host, $db_user, $db_pass ,$db_name);
        
        if (mysqli_connect_errno()) 
        {
            printf("Error connecting to DB, Errorcode: %s\n", mysqli_connect_error());
            exit;
        }
        

        【讨论】:

          【解决方案4】:

          使用 Merch 会员的回答 - 上面的 php 代码。 您需要授予权限并在从中检索数据库数据的服务器上创建用户。

          连接到mysql: 在 shell 类型中(如果你是 root 类型 root 或任何用户名): mysql -u root -p

          输入密码,

          当您登录时:

          create user someusername@your-client-ip identified by 'password';
          grant all privileges on *.* to someusername@your-client-ip with grant option;
          

          现在你可以使用上面的php代码连接到远程服务器数据库(你刚刚用来创建mysql用户your-client-ip的ip)

          当您为 mysql connect 创建 php 变量时 - 如果只是 ip 未连接,则主机在变量中使用 mysql 端口 your-client-ip:3306

          祝你好运

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-13
            • 2021-10-20
            • 1970-01-01
            • 2015-07-24
            • 2011-01-11
            • 2017-05-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多