Php Remote access to Ec2 Mysql server - php

Im trying to acess an EC2 mysql server from another EC2 instance using PHP.
$con = mysqli_connect('elastic_ip_host','user','pass','database');
if(!$con){
echo mysqli_connect_error(); echo mysql_error();
}
//Also trying PDO
try
{
$dbcon = new PDO('mysql:host=elastic_ip_host;dbname=database','user','pass');
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected!';
}
catch(PDOException $e)
{
echo 'ERROR! '.$e->getMessage();
die();
}
Can't connect to MySQL server on 'elastic_ip_host' (13)ERROR! SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'elastic_ip_host' (13)
Using MySQLWorkbench app in my computer, Im connecting pretty fine using Elastic IP (number). Just PHP is not connecting. Checking phpinfo I see it has pdo and mysqli all ok.
Do you have any idea?
(Also Im using percona and I run that command first install that ask if I will not be able to connect remotly with root. But I created a new user as %. Thats what I am using in MySqlWorkbench program from my computer to access the database fine)

Running:
setsebool -P httpd_can_network_connect=1
As root in ssh fixed my problem :)

Related

PHP code is working with MySQL but not in LAMP

I am new to web development. I recently installed LAMP stack and was trying to connect my PHP code to MySQL which established a connection and created a database (All the code was written in a directory in opt/lampp/htdocs).
<?php
$servername = "localhost:3306"; // specifying the port number seems necessary
$username = "akshay";
$password = "xxxx";
$dbname = "sys";
try {
//valid database needs to be mentioned
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully<br>";
$sql = "CREATE DATABASE myDBPDO";
// use exec() because no results are returned
$conn->exec($sql);
echo "Database created successfully<br>";
} catch(PDOException $e) {
echo "Connection Error: " . "<br>" . $e->getMessage();
}
$conn = null;
?>
And it worked fine. But when I proceeded further to create a table in my database, the server gave an error:
Connection failed: SQLSTATE[HY000] [1049] Unknown database 'myDBPRO'
I later found out that MySQL was running independently and not in the lamp stack. (i.e. it always showed "ok" whenever I ran sudo /opt/lampp/lampp start whereas Apache and ProFTPD showed "already running". I was not able to open myphpadmin either).
To connect MySQL to the LAMP, I stopped MySQL:
sudo service mysql stop
And then restarted my lamp stack: sudo /opt/lampp/lampp start (This time MySQL was connected as it showed already connected when I ran the command again and I was able to open pyphpadmin too.) So, I tried to run my code again but this time, it would not even connect. It gave the following error:
Connection Error:
SQLSTATE[HY000] [1045] Access denied for user 'akshay'#'localhost' (using password: YES)
I have the following doubts:
How was my code initially working if MYSQL was not connected to LAMP stack?
Why was the created database not present in PHPMyAdmin?
How to make MySQL work with LAMP?
(Forgive me if something is too obvious, I am quite new to this.)

How to access mysql server within a server?

I am running mysql on a linux server. I have the linux server IP and mysql runs on localhost on that linux server. Now how do I access mysql running on localhost server remotely?
I can do this by ssh into linux server then running mysql, but how would I do it with in a php script? For example...
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
(this script only connects to mysql directly which is not possible remotely)
In my script I would need to access linux server then mysql server unless there is a way to access mysql directly (remotely). How is this achievable?
You need change bind address in my.cnf and restart your mysql service.
Official documentation:
https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_bind_address
If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.
By default all connections restricted localhost.
In order to access your mysql instance from your server; you need to install workbench (or some other mySql explorer) on your development machine.
Create a connection to mysql instance using server ip address as host name and provide username and password to open connection. If every thing goes well you will see your databases in explorer.

PDO can't connect to database

Our school let us sue a small database that I am trying to access through the school network.
Connecting using
mysql -D ddddd -h hhhh -u uuuu
works fine (Note that contrary to multiple questions on this site, hhhh is not localhost). However, using
new PDO('mysql:host=hhhh;dbname=dddd, uuuu, pppp)
does not, and brings up an error:
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock''
How do I locate this socket? (NB: In case it is relevant, the schools computers run on FreeBSD and I only have a very limited permissions).
the error:
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock''
means that the PDO is trying to connect to MySQL via local socket (linux socket is file).
But my guess is that you want to connect via hostname/ip. Your code Has to be wrong. From the example it looks like you are using php. Try double check everything.
Here is an example from php.net:
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Use correct IP address to connect.
If u r using localhost then by default socket will be used on Linux so
Use 127.0.0.1 as host instead of localhost.

php mysql connect fails even though shell client succeeds

I have a weird problem.
I'm trying to connect from a linux host to a remote MySQL server on a Windows host.
If I try to connect from shell using the mysql client it works perfectly.
#mysql -h 192.168.x.x -u MyUser -pMyPassword
I've also tried connecting from a simple python script and it works as well.
But if I try to connect from a php script it fails with a "Can't connect to MySQL server on '192.168.x.x' (13)" error.
mysql_connect ('192.168.x.x', 'MyUser', 'MyPassword');
the remote server is running 5.0.41-community-nt on Windows (no info about the OS version)
the client machine is running CentOS 6.2 and is itself equipped with a MySQL 5.1.61 server.
The PHP MySQL module is using the sampe API version 5.1.61
The problem MUST be inside PHP as the connection is successful from the shell client (and even from a python script) but I don't have a clue.
Any help will be higly appreciated.
Use the simple PHP way to connect MySQL:
<?php
$link = mysql_connect('192.168.x.x', 'MyUser', 'MyPassword');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
Please, show us the full code.

Why php PDO uses a different from hostname to the one used by mysql_connect() when connecting to a remotely hosted mysql database?

I've mysql database and php/apache on two different servers: let's say hostphp.domain.com and hostmysql.domain.com.
On the mysql server I've set a user "my_user" with permissions to connect to "my_database" db from the specific host "hostphp.domain.com".
When I connect to it using mysql_connect it does right. But when I do it via php PDO I get this error:
SQLSTATE[42000] [1044] Access denied for user 'my_user'#'%' to database 'my_database'
I've done some tests and I found the problem is ...#'%', mysql is refusing that connection because "my_user" does not have permission to connect from any host.
Also I've tried to connect using mysql_connect with a wrong password to see the error and I get this:
Could not connect: Access denied for user 'my_user'#'hostphp.domain.com' (using password: YES).
The difference is in ..#'%' and ...#'hostphp.domain.com'.
So that's my question, why php pdo do not declare hostname when connecting to a remote host? (or is doing that wrong).
Thanks and sorry for my english.
Edit.
Some code example, this does not work:
try {
$pdo = new PDO(
'mysql:host=hostmysql.domain.com;port=3306;dbname=my_database',
'my_user',
'my_pass'
);
} catch (PDOException $e) {
die($e->getMessage());
}
but this works ok:
$conn = mysql_connect('hostmysql.domain.com', 'my_user', 'my_pass');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
You have successfully connected to the host using mysql_connect, but you got no errors because you did not attempt to select the database.
Your user probably doesn't have access to your database.
Try running mysql_select_db("my_database"); after connected to the host and you should get the same error.

Categories