how to connect to MySQL databases using php in zend server - php

I am trying to connect to my MySQL version 8.0.11 database in zend server version 7.2.10 using php but I could not connect it
Warning: mysqli::__construct(): Unexpected server respose while doing caching_sha2 auth: 109 in C:\Program Files (x86)\Zend\Apache24\htdocs\connectdatabase.php on line 7
Warning: mysqli::__construct(): MySQL server has gone away in C:\Program Files (x86)\Zend\Apache24\htdocs\connectdatabase.php on line 7
Warning: mysqli::__construct(): (HY000/2006): MySQL server has gone away in C:\Program Files (x86)\Zend\Apache24\htdocs\connectdatabase.php on line 7
Connection failed :MySQL server has gone away
I am getting the following warnings when i try to run my code.
i have searched and tried ALTER USER 'username'#'hostname' IDENTIFIED WITH mysql_native_password BY "userpassword" command but it does not work for me
<?php
$servername = "localhost";
$username = "root";
$password = "hello";
$conn = new mysqli($servername,$username,$password);
if(mysqli_connect_error())
{
die("Connection failed :" . mysqli_connect_error());
}
echo "CONNECTED SUCCESSFULLY";
?>

Try with caching_sha2_password:
ALTER USER 'username'#'hostname'IDENTIFIED WITH caching_sha2_password BY 'userpassword';

Related

Mysql connection over SSL with PHP mysqli

I'm trying to setup PHP mysqli connection to MariaDB database for two VPS servers and need to encrypt the communications due to it being over public network.
Currently I can connect from the client server to database server via commandline mysql client normally and I have checked via tcpdump that the connection is encrypted. However for some reason I can't figure out the PHP part. It's relatively basic nginx + php5-fpm + mariadb setup but mysql is working on non default port.
Debian Jessie, Php5 5.6.7, Mariadb 10.0.16, nginx 1.6.2
Here's the test script:
<?php
$DB_NAME = '';
$DB_HOST = '111.111.111.111';
$DB_USER = 'username';
$DB_PASS = 'password';
$mysqli = mysqli_init();
if (!$mysqli) {
die('mysqli_init failed');
}
//have tried witha and without the following with multiple variations
$mysqli->ssl_set(NULL, NULL, NULL,'/etc/mysql/ssl/',NULL);
if (!$mysqli->real_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, 11111, NULL,MYSQLI_CLIENT_SSL )) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
$query = "SHOW STATUS LIKE 'ssl_cipher'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
print_r($row);
}
}
else {
echo 'NO RESULTS';
}
mysqli_close($mysqli);
?>
Main error I'm getting without the ssl_set:
2015/07/11 15:58:34 [error] 2857#0: *374 FastCGI sent in stderr: "PHP message: PHP Warning: mysqli::real_connect(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /srv/www/test.php on line 15
PHP message: PHP Warning: mysqli::real_connect(): Cannot connect to MySQL by using SSL in /srv/www/test.php on line 15
PHP message: PHP Warning: mysqli::real_connect(): [2002] (trying to connect via tcp://192.168.130.123:42139) in /srv/www/test.php on line 15
PHP message: PHP Warning: mysqli::real_connect(): (HY000/2002): in /srv/www/test.php on line 15".....
Any ideas would be appreciated. This is really killing me.
Maybe this problem occurs due to the changes made in PHP 5.6. I guess you are using self-signed certificates? If your DB enables peer_name validation by DEFAULT, there is no way to disable this in PHP. So when generating you certificates you have to use the right "Common Name" for each one:
CA: hostname
Server: FQND, e.g. hostname.example.com
Client: somename
The important part is the server certificate where the Common Name has to be the same as the host you are connecting to.
What it looks like is this
SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
My guess is that you don't have the correct CA set up. Some DB systems (like Amazon Web Services RDS) have their own CA file. You're using the capath argument so make sure the PEM files are in that path. If they are, the next thing I would do is switch to the third argument of ssl_set and specify the PEM file directly
$mysqli->ssl_set(NULL, NULL, '/path/to/ca.pem', NULL, NULL);

Error to connect to MySQL database

I am a beginner in php, and for a project I want to connect me with my database but the problem is :
I need 4 variables:
$serveurname
$username
$password
$databasename
When i do :
$link = new mysqli($servername,$username,$password)
There are two errors :
Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/ubuntu/workspace/index.php on line 21
and
Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/ubuntu/workspace/index.php on line 21
I think the problem is : serveurname information is bad but I don't know where can I find it on phpmyadmin?
On phpmyadmin i see :
MySQL
Serveur: info-arie.iut.bx1 via TCP/IP
Version du serveur: 5.5.43-0+deb7u1
Version du protocole: 10
Utilisateur: dfrances#info-morgane.iut.bx1
Jeu de caractères pour MySQL: UTF-8 Unicode (utf8)
but information (at right of the screen on phpmyadmin) seems bad because in my code I have :
$servername = "info-arie.iut.bx1";
$username = "dfrances#info-morgane.iut.bx1";
You can find phpmyadmin mysql connection info in file config.inc.php inside the phpmyadmin folder. Find entries like the following and use it for your code.
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['user'] = 'someuser';
$cfg['Servers'][$i]['password'] = 'somepass';
From your error message, the host where you run your PHP script cannot contact the database server.

mssql_connect unable to connect to server

I'm trying to connect to a remote sql server database via php in our debian server.
<?php
// Some basic variables
$Server = "myservername";
$User = "myuser";
$Password = "mypwd";
$Database = "facts_sql";
// Connect to the database
$dbconnect = mssql_connect($Server,$User,$Password);
?>
And I got the error.
PHP Warning: mssql_connect(): message: Login failed for user ''. (severity 14) in /var/www/Information.php on line 8
PHP Warning: mssql_connect(): General SQL Server error: Check messages from the SQL Server (severity 14) in /var/www/Information.php on line 8
PHP Warning: mssql_connect(): Unable to connect to server: myservername:1433 in /var/www/Information.php on line 8
Are there any advices? Thanks

I want to access another server database in PHP

I am really a beginner in PHP and Mysql. I made a database on static IP 192.168.1.211 which is based on CentOS(only Command prompt) and on this IP there is no other software like easyPHP, and I am working on static IP 192.168.1.20 based on Windows 7. I also installed easyPHP and Dreamweaver... using Dreamweaver I made one .php file and I tried to use the database which is on 192.168.1.20..
using below code
<?php
$server2 = '192.168.1.211';
$con = mysqli_connect(server2,'root','password','vvani');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
but I get an error as shown below
Notice: Use of undefined constant server2 - assumed 'server2' in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Failed to connect to MySQL: php_network_getaddresses: getaddrinfo failed: No such host is known.
How can I access the database on 192.168.1.211 from 192.168.1.20 in PHP code?
I also tried hard to find a solution from Google, but I am not getting a perfect solution.
Just read errors and correct it..
<?php
$server2 = '192.168.1.211';
$con = mysqli_connect($server2,'root','password','vvani'); // here, $server2 is variable, not constant
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Change server2 to $server2. If that still doesn't work then it's possible the other server does not allow requests in such a way so you will have to update the MySQL settings on that server to allow external access to the port (or otherwise forward it).
Thanks Elon Than and Explosion Pills,
I changed server2 to $server2, but I get new warning
Warning: mysqli_connect(): (HY000/1130): Host '192.168.1.20' is not allowed to connect to this MySQL server in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
And I research on it and I try to give remotely access to database on another server.
Follow some few steps and solved problem.
# mysql -u root -p
mysql> GRANT ALL ON Database.* TO <un>#'localhost' IDENTIFIED BY 'password';
Thanks again.

Setting up MySQL issue under Eclipse PHP

I have a snippet of code under the PHP file in Eclipse PHP.
$username = mysql_real_escape_string( $username );
It generates an error Warning:
mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /web_development/php_work/new_test/test.php on line 37
Looks like the Eclipse PHP couldn't connect to the MySQL of XAMPP, so How do I fix that?
I added two lines
#mysql_connect('localhost','root', '') or die('Could not connect to : ' . mysql_error());
#mysql_select_db('test') or die( "-Unable to select database");
but Eclipse still throws me.
mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Can't connect to local MySQL server through socket '/tmp/mysql.sock'
thx.

Categories