MySQL via SSH Tunnel Access Denied - mysql

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";
?>

Related

fix access denied for root'#'localhost' (using password: NO) in init.php on line 2

I have an e-commerce website, and I m trying to move from localhost windows to Linux Mint. the problem is that I can't connect to database.
I see my config.inc.php in lampp/phpmy admin and the username is root and password = ""
I don't know how to fix this.
<?php
$db = mysqli_connect('127.0.0.1', 'root', '', 'ecommerce');
if(mysqli_connect_errno()) {
echo 'Database connection failed with following errors: '.mysqli_connect_error();
die();
}
require_once $_SERVER['DOCUMENT_ROOT'].'/ecommerce/config.php';
require_once BASEURL.'helpers/helpers.php';

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);

Why won't this Perl DBI/DBD MySQL (MariaDB) connect to locahost work?

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";

MySQL failing to establish a connection

I recently created a webpage, while using an usbWebserver, so basically a local server.
Yesterday, I have purchased a domain and hosting, and I wanted to move my files onto the server.
I have changed my username, password and url, however I cannot get the mysql_connect to work.
<?php
$mysqlhost = "mysql04.totaalholding.nl";
$user = "a";
$passwd = "";
$mysql = mysql_connect($mysqlhost, mysql_real_escape_string($user), mysql_real_escape_string($passwd));
if (!$mysql) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('a_turfjes', $mysql);
if (!$db_selected) {
die('Could not connect: ' . mysql_error());
}
?>
The above code is how I connect to the database, which should work. (please note that a is not my username and neither is my password empty).
These are the properties of the database:
DATABASE: a_turfjes
Extern MySQL Host: mysql04.totaalholding.nl
Users: a_admin
I am not quite sure which username to use should I use the username and password from cpanel (which is required to access PHPMyAdmin), or the username and password, which is the user of the database itsself.
I'd like some help on this error. When accessing my index.php (which includes db.php (which is the file above)), I receive the following errors:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'root'#'localhost' (using password: NO) in /home/a/public_html/turfjes/db.php on line 8
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/a/public_html/turfjes/db.php on line 8
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'root'#'localhost' (using password: NO) in /home/a/public_html/turfjes/db.php on line 8
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/a/public_html/turfjes/db.php on line 8
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'#'www30.totaalholding.nl' (using password: NO) in /home/a/public_html/turfjes/db.php on line 8
Could not connect: Access denied for user 'root'#'www30.totaalholding.nl' (using password: NO)
Use the username and password of the database itself not the cpanel.
Test connection using:
$mysql_host = "localhost"; # Usually doesn"t need modified
$mysql_db = "a_turfjes"; # Database name
$mysql_user = ""; # Username
$mysql_pass = ""; # Password
$link = mysql_connect ($mysql_host,$mysql_user,$mysql_pass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
If successful, access records:
$query = "SELECT * FROM TableName";
$result = mysql_db_query ($dbname, $query, $link);
Use the database username and password. If the database is hosted in another server please give the host name to that server name

Can't connect to local MySQL server through socket

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