简体   繁体   中英

Remote access to MySQL EC2 Instance for PHP application on non AWS server

I am developing a PHP application that needs to access MySQL (EC2 Instance) remotely from my machine (or any IP for that matter). The application needs to be a stand alone, meaning if tunnel is necessary then the application needs to be able to do that. Currently I am using the below code for connecting to MySQL:

$conn = mysql_connect( $serverName, $uid, $pwd) or die(print_r(mysql_error(), true));

// select database
$dbname='xxxxx';
mysql_select_db($dbname);

I have tried opening the port for MySQL in security groups in AWS console, and have played around with giving access to IPs. I know that EC2 requires keys to access, so I was wondering if any functions allow me to use the key to connect and query data, as well as get that data back into my application for use.

Here is the error message I am currently recieving:

XMLHttpRequest cannot load /myapp.php? Origin null is not allowed by Access-Control-Allow-Origin.

It doesn't seem to be able to load the data back, and it is strange that I do get some of the information from the database in the file. Somehow it is not able to come back and process the information the way I want. When the same application is on localhost it works fine

Any help would be greatly appreciated!

  1. You wont need to use the keys to connect to mysql. There is nothing special about an ec2 setup for mysql as you would in a traditional environment. To be secure, its probably best to use an ssl connection (or SSH tunnel) to mysql if its going to be remote, over the internet.

  2. The other error message has nothing to do with mysql. You are making a cross domain ajax request. By default browsers will block this. Recent browsers allow you to set a Access-Control-Allow-Origin header to allow cross domain requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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