PHP - Mysql: can't connect to database - php

I've setup mysql server 8.0, Apache2.4, php 7.2 and created a simple database.
As a start I am trying to write a php code for connecting to the database but i can't figure it out.
My code:
<?php
$con = mysqli_connect("localhost","Don","password","db1");
if (!$con) {
echo "Error: " . mysqli_connect_error();
exit();
}
echo 'Connected to MySQL';
?>
I get Error: The server requested authentication method unknown to the client.
I've tried several things: connect to root, 192.168.0.1 instead of localhost but nothing is working.
Any ideas? Thanks.

(post for someone who has the same problem)
i searched about the old password issue but i couldnt find the old_passwords var, not even in my.cnf file.
so what i did was uninstalling mysql server 8.0 and installing 5.7.2 and it worked.
ps i didnt configure anything differently while setting up the 5.7.2 version.

Related

php mysql connect fails even though shell client succeeds

I have a weird problem.
I'm trying to connect from a linux host to a remote MySQL server on a Windows host.
If I try to connect from shell using the mysql client it works perfectly.
#mysql -h 192.168.x.x -u MyUser -pMyPassword
I've also tried connecting from a simple python script and it works as well.
But if I try to connect from a php script it fails with a "Can't connect to MySQL server on '192.168.x.x' (13)" error.
mysql_connect ('192.168.x.x', 'MyUser', 'MyPassword');
the remote server is running 5.0.41-community-nt on Windows (no info about the OS version)
the client machine is running CentOS 6.2 and is itself equipped with a MySQL 5.1.61 server.
The PHP MySQL module is using the sampe API version 5.1.61
The problem MUST be inside PHP as the connection is successful from the shell client (and even from a python script) but I don't have a clue.
Any help will be higly appreciated.
Use the simple PHP way to connect MySQL:
<?php
$link = mysql_connect('192.168.x.x', 'MyUser', 'MyPassword');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
Please, show us the full code.

MySQL failing to establish a connection using PHP

Using ubuntu 12.04 64 bit on Lenovo t410.
Using apache2 and Mysql 5.5 and attempting to connect via localhost
I am attempting to establish a connection to a database that I made on localhost. When the line of code is reached to establish a connection, it seems Mysql simply hangs, and there is no error message displayed after. I verified that an echo works immediately prior to the connection attempt. I know that apache2 server is working as I can access the index page and display my html form.
I have tried etc/mysql/my.cnf setting the bind address to localhost.
My line of code looks like:
// Attempts to establish connection to MySql server
$connection = mysql_connect("localhost","username","password");
// Prints error message if the connection to MySql fails
if (!$connection){
die("Connection failed: " . mysql_error());
}
echo "Connection established.";
I tried the connection line with single quotes and with no semi-colon as well.
I am willing to post the contents of any configuration file I have if the error isn't syntax. I haven't done anything fancy to Ubuntu, everything is the default install. I am new to CS and especially databases, PHP, and networking. This is my little experiment that I am stuck on.
Any help is greatly appreciated. Thanks,
Don
Can it be, because there is no error message, that the connection IS established, but you didn't do anything with it?
I mean, what is the rest of your code, is there after your code here something like:
mysql_select_db("database_name",$connection);
After reading your last comment, it appears the mysql extensions are not being loaded. Have a look at your php.ini, uncomment the following line (remove the semicolon at the beginning of the line) and restart your apache:
extension=php_mysql.so
Make sure the extension exists in the php extensions directory.
Due to the fact that you are using MySQL version > 4.1.3 it is strongly recommended that you use the mysqli extension instead. Have a look at this: PHP: MySQL Overview
try to set
$mysql_user = "your_username";
$mysql_pass = "your_password";
$mysql_server = "Servername";
$link = mysql_connect($mysql_server,$mysql_user,$mysql_pass);
if (!$link) {
header('HTTP/1.1 500');
exit();

Apache 2.2 Mysql and PHP connection failed (Windows)

My current situation is that I'm using:
1) Mysql Version 5.1
2) Apache Server 2.2
3) Php Version 5.2.17
And... I have the simplest PHP Code Here.
But poorly, the connection is always failed (On Browser). I tried to follow some steps from the StackOverFlow, and also some other forums such as here;
I already tried to:
1) Ensure the System32\drivers\etc\host file is using the safe
Host File Content Here.
2) Ensure the Apache is having Safe Error Content Here.
3) Ensure the mysqli & mysql extension of PHP is Installed
properly accordingly.
But then... The latest result is still same, problem still exist. Sigh. Is there something I forgotten?
if you are using Windows 7 then you need to change localhost to 127.0.0.1.
Is the database running? You need to check that it is started by going to the MySQL bin directory and running mysql command (if you are requested for a password, just hit enter). If you connect then the user is root and the password is blank
What username and password are you using to access the database?
Why are you using #mysql_connect? remove the "#" symbol
<?php
$url = 'localhost';
$link = mysql_connect($url, 'user', 'pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Trouble connecting to SQL Server with PHP

I'm trying to connect to my local SQL Server 2008 R2 (have also tried it with 2005, same result exactly) with PHP. I'm using PHP 5.1 which still supports php_mssql and mssql_connect().
for some reason PHP just won't find my server, I can connect via ODBC flawlessly an that's fine, but I would like to connect to SQL Server directly.
I have connected PHP to SQL Server a million times on different servers, this one seems to be the only one giving me issue.
This is my little test code to try and get the connection working.
//define connection garbage
$db['hostname'] = "USER90C6\SQLEXPRESS";
$db['username'] = "user";
$db['password'] = "password";
$db['database'] = "kal_auth";
//connection string
$conn = mssql_connect($db['hostname'], $db['username'], $db['password']);
//does it work? :o
if($conn)
{
echo "works";
}
else
{
echo "fails";
}
The error this code produces:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: USER90C6\SQLEXPRESS in C:\xampp\htdocs\test.php on line 9
fails
Is there something seriously wrong with my setup? or am I missing something.
Did you enable TCP/IP on the server's configuration tool?
Is the firewall blocking any ports you might be using?
Are your MDAC (microsoft data access components) updated?
turn on mssql.secure_connection in php.ini
I think you don't miss anything.. Your connection string seems to be right (you receive a "Unable to connect" error..).
In my opinion, you problem can be a version incompatibility or a user privileges mistake. First of all: look at DLL driver you are using in PHP and check it's compatibility with you MSSQL version.
Maybe can be a good idea a fresh PHP install, with the latest stable release, if it is possible. Give a look at: http://www.php.net/manual/en/mssql.requirements.php
Good luck.

php and mysql connect issues on windows server / IIS6

I installed php and mysql on a Windows 2003 server running IIS6. I uncommented the lines for the mysql and mysqli extensions. I am able to run phpinfo, and am seeing the mysql and mysqli sections in phpinfo.
I then run the following code to attempt a connection to the database -
<?php
$link = mysql_connect('localhost', 'root', 'mypassword');
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
When I attempt to load this through a browser, I am getting a generic 500 server error. I don't know where else to look to troubleshoot this issue. Can someone point me in the right direction?
I am also able to access the mysql database using mysql workbench on the server.
Thanks for any thoughts.
I solved this by referring to this post - PHP has encountered an Access Violation at 77FCAFF8
Ultimately, I uninstalled MySql and then reinstalled at the root of my filesystem in order to eliminate any spaces in the path. I then recycled my application pools, and am now able to connect.
thanks.

Categories