I have the error: SQLSTATE[HY000] [2005] Unknown MySQL server host - php

I'm using a mysql database. I would like to connect to it with the script i wrote :
<?php
function getDatabase() {
$host = 'localhost:3306';
$db = 'freya';
$login = 'root';
$pw = 'helloitsme';
try {
return new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $login, $pw);
} catch (Exception $e) {
die('Erreur : '.$e->getMessage());
}
}
$db = getDatabase();
I have seen that this error is recurent but none of the solutions worked for.
I checked the my.cnf, and i'm sure that i'm using the port where the mysql db is.
I'm also sure that the db name, the login and the password are correct, because i'm using them to reach the db with the shell.
What could be the problem ?

You don't need to specify the port as 3306 is default for mysql, but if you do, the correct connection string is
'mysql:host=localhost;port=3306 ...'

Related

mysql error SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)

I am relatively new to php and have been following a tutorial for a beginner project. This project includes a database using the DBO object with mysql. I am using MAMP to run php. When I try to run my project it gives me the error in the title. Here is my code for database setup:
<?php
$dsn = 'mysql:host=localhost;dbname=assignment_tracker';
$username = 'root';
try {
$db = new PDO($dsn, $username);
} catch (PDOException $e) {
$error = "Database Error: ";
$error .= $e->getMessage();
include('view/error.php');
exit();
}
I am able to enter the phpmyadmin page. Looking at the user table in the mysql database shows this:
My port in the php.ini file is 8889. (My apache port is 8888 but my mysql is 8889 not sure which one it should be set to.) Looking at other answers on this website have been really confusing for me so any help would be greatly appreciated.
Try adding mysql's port to the $dns and add the password (if it's not null) as part of the connection. Try this
<?php
$dsn = 'mysql:host=127.0.0.1;port=8889;dbname=assignment_tracker';
$username = 'root';
$password = '';
try{
$db = new PDO($dsn, $username, $password);
}catch (PDOException $e){
$error = "Database Error: ";
$error .= $e->getMessage();
include('view/error.php');
exit();
}

Database error in Ubuntu

im having a problem, my project is working in my windowsOS, and i try it to transfer in Ubuntu16.04 Operating system, with static ip address. now when i trying to access the project it keep saying Database Connection Error!
<?php
$db_password = "Welcome1";
$server = "192.168.2.218";
try{
$pdo_conn = new
PDO("mysql:host=$server;dbname=boldr","phpmyadmin","
{$db_password}");
} catch (PDOException $e){
die("Database Connection Error!");
}
$conn = mysqli_connect("192.168.2.218","phpmyadmin","{
$db_password}") or die(mysqli_error($conn));
mysqli_select_db($conn,'boldr') or die("cannot select DB");
?>
is your database on the same server? If yes try 127.0.0.1 or localhost.

How to connect to two different hosts at the same time

I am developing a dynamic website and I'm using both local and remote host. I've tried to implement a code in which I stablish connection first with the localhost. If connection to the locahost is false or can't be established, I go to the second connection (remote server or host). But this time, I want to do something different. Now I want to know if it's possible to connect to two hosts/servers at the same time with no errors. The databases and tables are the same. I'm asking this, because I just want to avoid sql backup. What do you say? Moreover, I want to use the same variable $pdo, so I don't need to change or repeat all connections to the tables.
This is my current code:
<?php
try {
//Local Host - XAMPP
$dsn = 'mysql:host=localhost;dbname=bananas';
$user = 'root';
$pw = '';
$sessionpath = 'C:/xampp/tmp';
$pdo = new PDO($dsn, $user, $pw);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) { //$e
//echo 'Error: '.$e->getMessage();
if(!isset($pdo) || $pdo == false){
try{
//Remote Host
$dsn = 'mysql:host=bananas123;dbname=bananas';
$user = 'mybigbanana';
$pw = '6969';
$sessionpath = '/php_sessions';
$pdo = new PDO($dsn, $user, $pw);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e) {
echo 'Error: '.$e->getMessage();
}
}
}
?>
Note: I don't want to do something like having two variables for each connection, like, for example, $pdo and $pdo2. I want only one variable for both connections, which is $pdo.

error occurs while connecting PDO on server

this is my code it's working in local host but not in server
$dbtype = 'mysql'; // Normally mysql
$host = 'localhost'; // This is normally set to localhost
$db_username = 'name'; // DB username
$db_password = 'pass'; // DB password
$db_name = 'db_name'; // DB database name
$dbprefix = 'db_'; // Do not change unless you need to!
$cat_table="db_cat";
try
{
$pdo = new PDO('mysql:host='. $host .';port=2082;dbname='.$db_name, $db_username, $db_password);
}
catch (PDOException $e)
{
die($e);
}
?>
ERROR MESSAGE
exception 'PDOException' with message 'SQLSTATE[HY000] [2000] mysqlnd cannot connect to
MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password').
This will store a new, and more secure, hash value in mysql.user. If this user is used in
other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag
from your my.cnf file' in /home/jeywigki/public_html/hotel/db/config.php:13 Stack trace:
#0 /home/jeywigki/public_html/hotel/db/config.php(13): PDO-
>__construct('mysql:host=loca...', 'table', 'pass') #1 /home/jeywigki/public_html/hotel/index.php(3): require_once('/home/jeywigki/...') #2 {main}
kindly help me.
thanks in advance
I am not sure but it could be the issue.
you can try this:
mysql:host=$host;dbname=$db_name inside in quotes.
try
{
$pdo = new PDO("mysql:host=$host;dbname=$db_name", $db_username, $db_password);
}
catch (PDOException $e)
{
exit('Error Connecting To DataBase');
}
docs

Connecting to a remote mysql server

How would I connect to the demo phpmyadmin server in php? My code looks like this.
<?php
$host = 'http://demo.phpmyadmin.net/STABLE/';
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
but I get this as my error
QLSTATE[HY000] [2005] Unknown MySQL server host 'http://www.demo.phpmyadmin.net/STABLE/' (1)
You seem to be confusing two things:
the demo phpMyAdmin front-end that is backed by a db server and db/schema
the db server and schema itself
PDO needs the latter, the db server itself.
Inspecting the front-end code of the demo, I don't see anything in there that would give us the actual connection details for the db server. And that's as I would expect: I find it hard to believe that the makers/maintainers of the phpMyAdmin demo would make their actual db server available for public remote connections.
change your hostname from
$host = 'http://demo.phpmyadmin.net/STABLE/';
to your original remote hostname like eg $host = 'ukld.db.5510597.hostedresource.com';
MySQL does not work on HTTP
<?php
$host = 'demo.phpmyadmin.net';
// High chances that this is NOT your mysql hostname.
// It will not even by like /STABLE/ as you mentioned it.
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>

Categories