douwei1408 2014-02-18 10:00
浏览 42
已采纳

连接到远程服务器上托管的MySQL数据库

Recently i have purchased a domain "domain.com". The hosting company has the usual CPanel to handle databases.

Using the "MySQL Databases" I:

  1. Created a database with the name "database1"
  2. Created a user with the username "user1" and password "pass1"
  3. Added user "user1" to the database "database1"

So far so good.

After that i clicked on PHPMyAdmin and redirected to the PHPMyAdmin webpage. There i see the "database1" that i have previously created. Clicking on that database, i used IMPORT to import a very simple table named "test" [columns (id,name,surname)]. Importing the table created table "test" below the database "database1", which is correct.

After that i have tried to connect to that database using the code below (connect_to_db.php):

// Create connection
$con=mysqli_connect("domain.com","user1","pass1","database1");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error() ."<br>";
}

I have uploaded the connect_to_db.php into the /home/domain/public_html/.

When i try to connect to the database1 i get the following error:

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'user1'@'xxx.xxx.xxx.xxx' (using password: YES) in /home/karkoona/public_html/connect_to_db.php on line 13

Where xxx.xxx.xxx.xxx is the public IP of domain.com (if i get it right).

Also on the right of landing page of PHPMyAdmin i see:

user: domain@localhost

Any idea why i cannot access the database with the current user? Is there any mistake in my code?

Thanks.

  • 写回答

4条回答 默认 最新

  • dongle2627 2014-02-18 10:05
    关注

    You need to grant access to user1 using it's IP to access the remote server:

    GRANT ALL PRIVILEGES ON database1.* to 'user1'@'xxx.xxx.xxx.xxx' IDENTIFIED BY 'pass1';
    

    NOTE: replace the relevant info to the current info such as xxx.xxx.xxx.xxx to the current server IP that will access the remote MySQL.


    Since you're using cPanel you can do this to allow the IP:

    • Login to your cPanel (if not already logged in).
    • Scroll down to the Databases section.
    • Click on the Remote MySQL icon.
    • Enter the IP address of the computer that will be making the remote connection.
    • Click the Add Host button.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮