dongwuqi4243
2012-09-24 16:53
浏览 93
已采纳

是否可以使用php pdo连接到ec2实例上的远程数据库?

I'm using the following to connect to a mysql database from the localhost

<?php
function testdb_connect ()
{
   $dbh = new PDO("mysql:host=localhost;dbname=test", "testuser", "testpass");
   return ($dbh);
}
?>

However when I tried to connect to this database (database is running on ec2-12-34-56-78.compute-1.amazonaws.com) from a different server, using the following code

$dbh = new PDO("mysql:host=ec2-12-34-56-78.compute-1.amazonaws.com;dbname=test", "testuser", "testpass");

I'm unable to connect.

Is it possible to connect to a remote database on an ec2 instance with php pdo?

How would I pass an authentication parameter (ex. private key)

图片转代码服务由CSDN问答提供 功能建议

我正在使用以下内容从localhost连接到mysql数据库 < pre> &lt;?php function testdb_connect() { $ dbh = new PDO(“mysql:host = localhost; dbname = test”,“testuser”,“testpass”); return( $ dbh); } ?&gt;

但是,当我尝试连接到此数据库时(数据库正在 ec2-12-上运行 - 34-56-78.compute-1.amazonaws.com )来自不同的服务器,使用以下代码

  $ dbh = new PDO(“mysql:  host = ec2-12-34-56-78.compute-1.amazonaws.com; dbname = test“,”testuser“,”testpass“); 
   
 
 

我无法连接。

是否可以使用php pdo连接到ec2实例上的远程数据库?

如何传递身份验证参数(例如私有) key)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanchen7703 2012-09-24 17:01
    已采纳

    You should probably consider using RDS for your database rather than implementing on EC2 unless you have a very unique database that requires a high degree of customization (i.e. clustered configurations, etc.). Running on EBS-backed volume (which you would need to do to be able to persist the physical data files), will subject you to slow disk I/O. If you are not running on EBS-backed EC2, then your data is transient and can not be considered as being on reliable physical storage. If this is OK for your design (you just need transient info in your database), then you would probably be even better served but just putting your information into Elasticache or some form of in-memory cache.

    RDS uses MySQL (well, you can also opt to use Oracle). You would access it EXACTLY like you would access your own MySQL server (same PHP abstraction, same SQL, same almost everything (you don't get root access, but rather a form of super-user access). RDS also provide you easy to implement (i.e. push button) configuration for multi-az (high-availability, synchronously-updated standby), replication slaves, DB instance re-sizing, and data snapshots.

    In either case (for RDS or EC2), you would need to make sure that your EC2 or RDS security groups allows access from the EC2 instances (or other servers) that host your application. In case of EC2 only you could either place the servers in the same security group, and provide port 3306 access on that group, or better would be to create two security groups (one for app and one for db). In the db security group provide port 3306 (or whatever port you are using) to the security group(s) to which the app server(s) belong.

    For an RDS, you would need EC2 security group for app server(s) and a DB security group for the RDS instance). You would need to provide access to the app server security group in the RDS security config.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dounayan3643 2012-09-24 17:33

    I don't know the specifics of how this might work with AWS but the first thing I would do is get an SSH tunnel running between the machines.

    Then PHP/PDO would basically just think that you're connecting to a local database. In my experience it also makes the connection faster to establish as it doesn't have to do a DNS lookup to find the remote server... quite a big deal when you think that every PHP page load might have to connect to the remote DB.

    I use this on intranets when an application needs to manage data stored on a remote database and it works like a champ.

    I find SSH tunnels perfectly stable but I use a program called autossh to attempt to reconnect SSH tunnels when they go down.

    For completeness here's the command I use to start autossh so it establishes and maintains a particular SSH tunnel. Added here because I found the autossh docs pretty confusing to work out what options I wanted.

    autossh -M 0 -f -L3307:127.0.0.1:3306 -p 22 -N -f username@xxx.xxx.xxx.xxx

    This forwards port 3307 on your web server to 3306 on the remote DB server. So in PHP you would connect to 3307. You could choose 3306 if you wanted, I chose local port 3307 just in case you had a local MySQL as well as a remote. The -p switch is the port that SSH is running on on the remote machine.

    You can add this command to /etc/rc.local (on CentOS at least) to establish the SSH tunnel on server start.

    Hope this helps!

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题