Google cloud sql with php - google-compute-engine

I'm having a problem to connect my DB cloud sql with php.
I've tried every possible way to connect (pdo, mysqli & mysql)
none of them worked.
I've put an IPv4 for the could sql instance and authorized the compute engine IP in the allowed networks section.
When I'm trying to connect from the compute engine with
mysql --host=cloud_sql_IP --user=my_user --password
it's working and I'm able to see the tables.
On the php side I've put this code:
$db = mysql_connect(<CLOUD_SQL_IPV4>, 'root', '');
if (!$db){
die('Connect Error (' . mysql_error());
}
and I get "Connect Error (Permission denied)"
when I'm trying this way:
$conn = mysql_connect(":/cloudsql/<COMPUTE_INSTANCE>:<DB>", "root", "");
if (!$conn) {
die('Connect Error (' . mysql_error());
}
I'm getting:
"Connect Error (No such file or directory"
What else should I do?
Thanks!

Well after diggin into it for 2 days I managed to connect cloud DB
first run this for the permission fix (centos)
setsebool httpd_can_network_connect=1
And for the php it should be written this way:
new mysqli(
<IP_V4>, // host
'root', // username
'', // password
<DB_NAME>, // database name
null,
'/cloudsql/<GCE_INSTANCE>:<DB>'
);

Related

Remote db connection from another remote ip perl

I am trying to connect one remote server where I have installed MySQL but it is taking localhost
Remote server where mySQL is installed: 131.116.220.228
Remote server from where db needs to be connected: 131.116.220.220 ('ov046hanpgate01.ddc.teliasonera.net')
use DBI;
print "Testing 1\n";
$DSN="DATABASE=MySQL80;port=3306;host=131.116.220.228";
print "Testing 2\n";
my $dbh = DBI->connect("dbi:mysql:$DSN", "ShrutiTest", "Shruti#92", {PrintError => 0})
or die "Couldn't connect to database: " . DBI->errstr;
print "Testing 3\n";
$dbh->disconnect;
error: Couldn't connect to database: Host 'ov046hanpgate01.ddc.teliasonera.net' is not allowed to connect to this MySQL server at D:\Integrations\OWF\Scripts\test27july.pl line 6.
Looks like you need to allow the user ShrutiTest to connect from the ip where you are running the script, viz. 131.116.220.220
It can be done at the MySQL server by any admin:
GRANT ALL PRIVILEGES ON *.* TO 'ShrutiTest'#'131.116.220.220'
This syntax "DBI:mysql:database=$db;host=$host;port=$port" worked for me in the perl script to connect to a remote MySQL DB from an Unix server.
Solved the same. Code used is:
#!/usr/bin/perl -w
use DBI;
## mysql user database name
$db ="Buffering";
## mysql database user name
$user = "ShrutiBuffer";
## mysql database password
$pass = "Shruti1234";
## user hostname : This should be "localhost" but it can be diffrent too
$host="131.116.220.228";
$port="3306";
## SQL query
$query = "select * from buffering.insert_data";
my $dbh = DBI->connect("DBI:mysql:database=$db;host=$host;port=$port",$user, $pass);
$sqlQuery = $dbh->prepare($query)
or die "Can't prepare $query: $dbh->errstr\n";
$sqlQuery->execute
or die "can't execute the query: $sqlQuery->errstr";
print "values are:: ";
while (#row= $sqlQuery->fetchrow_array()) {
print join(", ", #row), "\n";
}
$sqlQuery->finish;
exit(0);

Perl codes always connecting to same MySQL database although it is configured to connect to different server

Perl codes always connecting to same MySQL(local but in error showing FQDN of server) database although it is configured to connect to different server.
please see the codes.
$datasetname="DBI:mysql:database=applications;host=entdb"
$$dbobject = DBI -> connect ($datasetname, $username, $password,
{RaiseError => 0, PrintError => 0})
or $errflg2 = 1;
print TT (" : ".time()."\n");
close (TT);
if ($errflg2 > 0)
{
$errmsg = "ERROR opening up the database: $datasetname\n";
$errmsg .= " Error number: " . $DBI::err . "\n";
$errmsg .= " Error text : " . $DBI::errstr . "\n";
print "$errmsg";
ilog ($ifile, $errmsg);
mail_it ($errmsg);
if ($debug != 0) { close (DB); }
exit (1);
}
else
{ print "Opened the '" . $datasetname . "' database.\n"; }**
enter code here
error
ERROR opening up the database: DBI:mysql:database=applications;host=entdb
Error number: 1045
Error text : Access denied for user 'entdb'#'vpl121' (using password: YES)
see in code i referred entdb, but Perl connecting to VPL121. Perl codes are running on vpl121.
In mysql all user accounts are identified via username#hostname format, where hostname is the host name or ip address of the computer from which the code connects to the mysql database.
You did write that the perl code runs on vpl121, therefore this is the hostname that mysql uses. Apparently, you do not have any user account that matches the 'entdb'#'vpl121' or the password is incorrect or the given user does not have access to the entdb database.
My guess is that you do not have any matching user accounts. Consider perhaps creating an 'entdb'#'%' user account, where % as hostname stands for any host name or ip address.

MySQL via SSH Tunnel Access Denied

I want to use SSH tunnel to connect to MySQL DB on remote host.
I've set up the tunnel with command:
ssh user#host -L 3307:remote_mysql_hostname:3306
I can successfull connect with HeidiSQL using this settings:
hostname: localhost
user: remote_mysql_user_login
password: remote_mysql_user_password
port: 3307
But when i use PDO in PHP to connect, i get:
Access denied for user 'remote_mysql_user_login'#'localhost' (using password: YES)
My PDO Dns is sth like this:
mysql:type=Core_Db_Adapter_Pdo_Mysql;host=localhost;port=3307;dbname=db_name;
Where is the trick?
Solution:
#symcbean thanks!
The problem was (as suggested by #symcbean) in hostname.
Changing to '127.0.0.1' fix the problem.**
I borrowed from your example and was able to connect to a remote host using your same SSH tunnel setup. Can you share the PHP script you are using to get some more info about your setup?
You may try a hostname other than 'localhost' for your remote database you are connecting to.
setup ssh tunnel - I setup an entry in /etc/hosts for my_db_host_name instead of using localhost
ssh my_user#my_remote_host -L 3307:my_db_host_name:3306
<?php
$dsn = 'mysql:host=my_db_host_name;dbname=my_db;port=3307';
$username = 'my_user';
$password = 'my_pass';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
$result = $dbh->query("select * from my_table");
try {
foreach($result as $row) {
echo "column1: " . $row['column1'];
}
} catch (PDOException $pde) {
echo "PDO exception: $pde";
}
echo "done \n";
?>

Uploading MySQL database to server?

I have created a website that has a MySQL database. I'm using phpmyadmin and xampp. Now I have a website to upload the files to (using Filezilla). How do I upload the MySQL database to the server and connect it to the website? I'm guessing there is already a tutorial somewhere but I couldn't find it.
If you have your database file ready to upload then in phpmyadmin there is an import option as shown in the image below:
In order to connect it to your website will depend on your server side language choice...
Example with PHP to connect your website to your database (using pdo):
<?php
$dsn = 'mysql:dbname=name_here;host=127.0.0.1';
$user = 'root'; //change to your username
$password = '******'; //change to your password
try {
$pdo = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit;
}
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
Once you have that at top of your PHP file you can connect. This one way to do it via PHP.

mysql_connect access denied error when trying to access remote database

I'm trying to connect to a remote database. Here's my code:-
php $con = mysql_connect("2toria.com","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("myTable", $con);
$result = mysql_query("SELECT * FROM Contestants");
while($row = mysql_fetch_array($result)) { echo $row['Name']; echo "<br />"; }
mysql_close($con);
The database, table, username and password names are all correct (I've changed them here for obvious reasons), but I'm getting the following error:-
Warning: mysql_connect() [function.mysql-connect]: Access denied for
user 'username'#'bluechip6.ukhost4u.com' (using password: YES) in
/home/toriaco/public_html/bigbro/index.php on line 9 Could not
connect: Access denied for user 'username'#'bluechip6.ukhost4u.com'
(using password: YES)**
First possible reason:
I know antagonist (a Dutch hosting service) blocks all connections that are not from localhost for security reasons, and I don't think they are the only ones. (So always connect to localhost, not a http://... URL!)
Second possible reason:
The password/username is wrong.