dongpeng7744 2017-09-10 12:25 采纳率: 100%
浏览 188
已采纳

如何连接到外部ip上的数据库?

I would like to connect to a database with an external ip of the pc on which the server, for example:

<? $db_host = "xxx.xxx.xxx.xxx" //external ip
$db_user = "user"; $db_password = "password"; $db_database = "database"; 
$conn = "mysql:host=$db_host;dbname=$db_database;charset=utf8mb4"; try {$db = new PDO($conn , "$db_user", "$db_password");}catch(PDOException $e) {echo 'Error: '.$e->getMessage();}?>

So, can you do that? And if so, how can I do it? Thanks everyone for help;)

P.s: Sorry for my bad English

  • 写回答

3条回答 默认 最新

  • douyanguo7964 2017-09-10 13:19
    关注

    First Check Your Public IP Address [ Public Ip Means External Ip Address] From Here : https://www.whatismyip.com/

    And Now Check Your System Ip Address ( Internal Ip Address using ipconfig in cmd )

    Both Are Differect So You Need To Used Public Ip Address Which One You Get From https://www.whatismyip.com

    Now, you have to Give Access permission to Database for Specific iP Address, By Default Mysql is not allowing to Access it SO You Need To Fire This Command for .

    Like This :

    SQL> GRANT ALL PRIVILEGES ON database.* TO 'user'@'your_database_pc_public_ip' IDENTIFIED BY 'newpassword';
    

    Example::

     SQL> GRANT ALL PRIVILEGES ON database.* TO 'any_name'@'public_ip' IDENTIFIED BY 'any_password';
    

    Then Its Work Fine For You :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?