MySQL failing to establish a connection - mysql

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

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

SQLSTATE[HY000] [1045] Access denied for 'root'#'localhost' on lumen api [duplicate]

This question already has answers here:
MySQL ERROR 1045 (28000): Access denied for user 'bill'#'localhost' (using password: YES)
(43 answers)
Closed 3 years ago.
I am installing lamp with tasksel on ubuntu 18.04. I have configured as per the instructions in the
https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-18-04
After changing the database details in .env file and config/database.php files, accessed the app in browser.
created a database "mydb"
created an user "myusername" / "mypwd" and grant all privileges to this user and database.
eg-->
CREATE USER 'myusername'#'localhost' IDENTIFIED BY 'mypwd';
GRANT ALL PRIVILEGES ON * . * TO 'myusername'#'localhost';
FLUSH PRIVILEGES;
Configuration in lumen .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=myusername
DB_PASSWORD=mypwd
config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydb'),
'username' => env('DB_USERNAME', 'myusername'),
'password' => env('DB_PASSWORD', 'mypwd'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
]
In terminal run 'path/project_folder/'
php artisan cache:clear
Log:
[2017-01-23 09:34:24] local.ERROR: exception 'PDOException' with message >'SQLSTATE[HY000] [1045] Access denied for user 'myusername'#'localhost'
(using password: YES)' in Illuminate\Database\Connectors\Connector.php:119
NOTE: To validate if PDO is working or not in my machine
Tested the mysql connection with testdb.php:
$servername = "localhost";
$dbname = "testdb";
$username = "root";
$password = "secret";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("mysqli Connection failed: " . $conn->connect_error);
}
echo "
mysqli Connected successfully
";
// working correct
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("
mysqli_connect Connection failed: " . mysqli_connect_error());
}
echo "
mysqli_connect Connected successfully
";
// working correct
try {
$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 "
MySQL PDO Connected successfully
";
}
catch(PDOException $e)
{
echo "
MySQL PDO Connection failed: " . $e->getMessage();
}
?>
Output is
mysqli Connected successfully
mysqli_connect Connected successfully
MySQL PDO Connection failed: SQLSTATE[HY000] [1045] Access denied for >user 'root'#'localhost' (using password: YES)
PDO in my local machine is not working. The extensions for mysql pdo is active. Restarted Lamp server many times. But it is still not working. Can any one help?
MySQL PDO Connection failed: SQLSTATE[HY000] [1045] Access denied for >user 'root'#'localhost' (using password: YES)
This does not mean that PDO is not working, it is working. Your login just failed for user 'root'. The password can be incorrect or something else can be the problem. (such as the host)
Could you try the following?
CREATE USER 'myusername'#'%' IDENTIFIED BY 'mypwd';
GRANT ALL PRIVILEGES ON * . * TO 'myusername'#'localhost';
FLUSH PRIVILEGES;
This will create a user which can login from any host.

mysql_connect : error access denied (www-data#localhost)

i have a raspberry pi with mysql/phpMyadmin installed.
i can access to my website with my IP on internet.
http://[Ip fix not 127.0.0.1]/[site name/folder name]
html code and PHP work but...
Access denied for user 'www-data'#'localhost' (using password: NO)
$hostname = "{IP FIX}";
$username = "********";
$password = "********";
$DataBase = "horaires";
$db = mysql_connect($hostname, $username, $password);
mysql_select_db($DataBase);
who is www-data user ?
why localhost ? i am on internet!
could you help me plz ?
Olivier

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

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.