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);
Related
use DBI;
my $dbh = DBI->connect ('DBI:mysql:host=localhost;database=test', 'user', 'password')
or die "failed to connect\n";
Results in an error message:
DBI connect('host=localhost;database=test','user',...) failed: Can't connect to MySQL server on 'localhost' (10061) at connect.pl line 3.
using: DBI 1.641, perl v5.26.2 on Windows 10 and running MariaDB 10.2.14
mysqld is running on the computer, and the server can be connected to with the standard "mysql test -u user -p" command
On another PC running Windows 7 with a very similar setup - but with DBI 1.636 - the connect() succeeds with the same perl code. Is is possible that DBI:mysql and Windows 10 aren't compatible?
It seems you have a space after the word "connect", anyway...try this:
my $driver = "mysql";
my $database = "DBname";
my $ip = "localhost";
my $db = "DBI:$driver:DBNAME:$ip:database=$database";
my $username = "mysqluser";
my $password = "mysqlpass";
my $cn = DBI->connect($db, $username, $password)
or print "Couldn't connect to database: " . DBI->errstr . "\n\n";
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.
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>'
);
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";
?>
I'm getting the following error on my site when I upload it or submit a page:
mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
What in the world does this mean?
Since the error is being thrown by the call to mysql_real_escape_string() it rather implies that you didn't call mysql_connect() first and pass a valid db handle to mysql_real_escape_string (or the call to mysql_connect() failed).
In some circumstances, the mysql extension will attempt to connect automatically using the default settings in php.ini, failing over to my.cnf if these are not available - which obviously are not valid. Or it may be that the settings are valid but the mysqld is not running.
Have you got scripts which are connecting to the database successfully?
Do you have a username and password for the database?
Try:
check_running();
$user=''; // fill in your details
$password=''; // fill in your details
$hosts=array(
'localhost', '127.0.0.1', $_SERVER['HTTP_HOST'], $_SERVER['SERVER_ADDR']
);
foreach ($hosts as $addr) {
try_con($addr, $user, $password);
try_con($addr . ':3306', $user, $password);
}
function try_con($host, $user, $password)
{
$dbh=mysql_connect($host, $user, $password);
if ($dbh) {
print "Connected OK with $host, $user, $password<br />\n";
} else {
print "Failed with $host, $user, $password<br />\n";
}
}
function check_running()
{
// this assumes that you are using Apache on a Unix/Linux box
$chk=`ps -ef | grep httpd | grep -v grep`;
if ($chk) {
print "Checking for mysqld process: " . `ps -ef | grep mysqld | grep -v grep` . "<br />\n";
} else {
print "Cannot check mysqld process<br />\n";
}
}
Yes exactly, some servers pass the default connection parameters when using mysql functions before connection and throw off an error, some other servers work just fine wherever you place the code
it is always safer to just place mysql_real_escape_string() after establishing mysql_connect() connection