How to configure XAMPP to connect to MySQL remotely - php

I have a problem to connect my online MySQL database from my local system.
I installed XAMPP on my Windows 7 and created a PHP file with this code:
<?php
$db_path = mysqli_connect('printcity24.com', 'printci1_admin', 'xr10s20191', 'printci1_db', '3306');
if(!$db_path) {
echo mysqli_connect_error();
}else{
echo "Connected successfully";
}
?>
Then i created a database on my website : www.printcity24.com
My web host admin configured my host and opened firewall.
When i use XAMPP command line to connect to my database every thing is ok, i can connect to my database remotely with this code :
# mysql -u printci1_admin -p -h printcity24.com
but when i use php code to connect to my database i get this error :
Warning: mysqli_connect(): MySQL server has gone away in D:\Xampp Server\htdocs\st\index.php on line 2
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in D:\Xampp Server\htdocs\st\index.php on line 2
MySQL server has gone away
I upload my php code on to other websites and test for connection and everything is "ok" but on my local xampp can't connect.
In this link said i have to use this command :
setsebool -P httpd_can_network_connect=1
but I don't know where to put this code and how to configure my xampp.

Related

Connect PHP to mysql server running on Mac [duplicate]

This question already has answers here:
Warning: mysqli_connect(): (HY000/2002): No such file or directory
(6 answers)
Closed 2 years ago.
I am trying to connect to my MySQL server on a mac using PHP after starting the server using brew services start mysql After this I can see that the MySQL server has successfully started. However, when I try to access my webpage using:
<?php
/* Attempt to connect to MySQL database */
$link = mysqli_connect('localhost', 'root', 'password', 'databasename');
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
The webpage start spinning continuously and doesn't load. I was wondering what's the issue as I can log into the root from the command line as well as see the tables. And the bre shows MySQL server running in the background.
EDIT I managed to print my error however, now I get an error:
Warning: mysqli_connect(): (HY000/2002): No such file or directory However, homebrew shows my MySQL server running.
Try to change your host from localhost to 127.0.0.1
$link = mysqli_connect('127.0.0.1', 'root', 'password', 'databasename');
You better make sure if mysqli extension is available in php.ini
(If it is not enabled, go into the php.ini file and remove ; from the mysqli line.)
If mysqlit is enabled, try running the code below and restart the server/terminal php task if anything changes
cd /var mkdir mysql cd mysql ln -s /tmp/mysql.sock mysql.sock
Check your credentials you give into the mysqli_connect
If everything is okay, and you cant still connect try installing helpful programs like MAMP. They will create an Apache Server with Php and MySql server on local computer without a problem with full configuration

MySQL Wordpress Nginx dnsmasq: Error: Error establishing a database connection

I'm trying to get a local development environment set up. I can't get wordpress to connect to mysql. I can duplicate the error with the following command:
wp core install --url=http://uganda.localhost/ --title="Uganda Aid" --admin_user="Jack" --admin_password="thepassword" --admin_email="JackWinterstein#msf.org"
Result:
Error: Error establishing a database connection. This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `localhost`. This could mean your host’s database server is down.
Things I tried:
Looked in wp-config and made sure the database name and credentials were correct. I can connect via cli
Updated homebrew.mxcl.mysql.plist to include --bind-address=*
Looked at the mysql socket location: mysql_config --socket producef /tmp/mysql.sock
Updated php.ini to reflect the attached below
Updated my.cnf to reflect bind-address = *
I am using dnsmasq (as shown in https://medium.com/#charlesthk/wordpress-on-os-x-with-nginx-php-mysql-62767a62efc4)
Environment
Darwin osx10.14, mysql Ver 8.0.17, nginx version: nginx/1.17.3, Wordpress 5.2.2, PHP 7.3.9
I figured it out. The problem was that caching_sha2_password authentication method unknown to the client. To fix this I ran:
ALTER USER jack#localhost IDENTIFIED WITH mysql_native_password BY 'thepassword';
I found this by: creating a basic test script:
<?php
$link = mysqli_connect('localhost', 'jack', 'thepassword');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
And the resulting error mentioned the caching_sha2_password authentication method.

php pdo in linux, not able to connect to remote mysql server but in wamp it's working fine [duplicate]

I am facing a weird problem here
we have a server A where the app files are stored
and B server with database
Tried to connect via command prompt from server A to B using the command
mysql -h xx.xx.xx.xx -u root -p password - and it worked
NOw i tried to create a php script in server A to connect to server B
the command is
$this->db=new PDO('mysql:host=xx.xx.xx.xx;dbname=databasename','root','password');
Connection failed: SQLSTATE[HY000] [2003] Can't connect to MySQL
server on 'xx.xx.xx.xx' (13) Fatal error: Uncaught exception
'Exception' with message 'SQLSTATE[HY000] [2003] Can't connect to
MySQL server on 'xx.xx.xx.xx' (13)'
Unable to find a solution on this.
Can any help on this?
thank you
I got it working by running a command in the database server :)
setsebool httpd_can_network_connect_db=1
thanks for the replies yycdev
Try specifying the port in your connection string and ensure the database server is set to allow remote connections and the port is open on your firewall (both of these I suspect are already done as you are able to connect via the terminal but it never hurts to verify and check things).
Change your PDO connection and add the port=3306 or if you're using MAMP use port 8889
$this->db=new PDO('mysql:host=xx.xx.xx.xx;port=3306;dbname=databasename','root','password');
Another thing to check is if – SELinux is blocking network connections. Login as root and run
setsebool -P httpd_can_network_connect=1
I don't know much about that, but try to place the port in back of the script:
$this->db=new PDO('mysql:host=xx.xx.xx.xx;dbname=databasename','root','password',3306);

Moving a SIte from Drupal to Raw PHP ... Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I don't understand this the error,
Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I am not familiar with drupal or any frameworks. Please advice me about this error and if I want to host my raw php site which I created in local WAMP server and connect to the database through, ...
#Database Connection
$dbc = mysqli_connect('localhost','****user', '***pw', '***db') OR die('Error: '.mysqli_connect_error());
what should I do?
Check whether mysql is running with the following command:
mysqladmin -u root -p status
And if not try changing your permission to mysql folder. If you are working locally, you can try:
sudo chmod -R 755 /var/lib/mysql/
Reference Connect to Server

PHP mysql_connect vs Ubuntu mysql terminal use

I'm using PHP in order to connect to MySQL with the following way:
$link = mysql_connect('...host...', '...username...', '...password...');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
and it connects just fine.
But when trying from my terminal this
mysql -h ...host... -u ....username... -p ...password...
I give my password and i take as a result this:
ERROR 2003 (HY000): Can't connect to MySQL server on '...host...' (111)
Any ideas how this can be solved?
Try sudo mysql -h ....etc.
I'm running linuxMint and had to use either su or sudo. I think by default you have to be root to enter the terminal. But with Ubuntu root is inaccessible, the only feature I don't like about Ubuntu.
The php code is running on the web server, your local command is running on your machine.
MySQL accounts are not just usernames - they are username + hostname combos. So both have to match for MySQL to find the account.
Since your hostname is different from the one the web server uses MySQL does not consider it a valid user. (It's possible to set the hostname to % which means "Any".)

Categories