Error to connect to MySQL database - php

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.

Related

how to connect to MySQL databases using php in zend server

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

MySql localhost hosting error on MacOS High Sierra

I'm sitting on a MacOS High Sierra(v10.13.1) and I followed a guide to install/enable Apache2, PHP7, MySQL and PhpMyAdmin locally.
The guide I followed:
https://coolestguidesontheplanet.com/install-apache-mysql-php-and-phpmyadmin-on-macos-high-sierra-10-13/).
I thought everything worked since I can log in to PhpMyAdmin with my root account and have access to create databases etc.. But trying to connect to my database in PHP I get this error:
Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Users/.../Sites/BattleriteTracker/include/base/Database.php on line 7
Warning: mysqli::__construct(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Users/.../Sites/BattleriteTracker/include/base/Database.php on line 7
Mysql connection error: php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
The code I used to connect with:
$Adress = "localhost";
$User = "root";
$Password = "...";
$Name = "BattleriteTracker";
$Connect = new mysqli($Name, $User, $Password, $Name);
if($Connect->connect_errno)
{
die("Mysql connection error: " . $Connect->connect_error);
}
Some additional info about my versions:
Apache: 2.4.27
MySQL: 5.7.20
PhpMyAdmin: 4.7.5
If anyone of you know what the problem is, please enlighten me.
Thanks for reading!

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

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.

need help connecting to RDS from local and EC2

I am trying to connect to RDS from my localhost and ec2 server.
I can connect fine with MySQL Workbench 5.2 CE but i cant seem to connect with PHP.
They are in the same zone and security group.
$dbName = "*******-****.***************.**-****-*.rds.amazonaws.com:3306";
$dbTable = "myTable";
$dbUser = "myUser";
$dbPass = "*********";
define("HOST", $dbName);
define("DBUSER", $dbUser);
define("PASS", $dbPass);
define("DB", $dbTable);
$conn = new mysqli(HOST, DBUSER, PASS, DB) or die();
these are the errors
Warning: mysqli::mysqli() [<a href='mysqli.mysqli'>mysqli.mysqli</a>]: php_network_getaddresses: getaddrinfo failed: No such host is known.
Warning: mysqli::mysqli() [<a href='mysqli.mysqli'>mysqli.mysqli</a>]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (trying to connect via tcp://*******-****.***************.**-****-*.rds.amazonaws.com:3306)
Warning: mysqli::mysqli() [<a href='mysqli.mysqli'>mysqli.mysqli</a>]: (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known.
the endpoint works fine as i said with the client, not on browser.
but what does this error mean, and how would i start debugging this?
Have you checked the security group for your amazon ec2 in your amazon admin console. please check you have opened the port for mysql (i.e 3306)

Categories