Access local MySQL server within my network - php

I have a website that has a page that needs to show all the information that is in the local MySQL server (not in the hosting). How am I gonna do that?
I only tried to change the connection details but no luck on that matter.
db_connect.php (connect to hosting server)
<?php
$servername = "localhost";
$username = "xxx";
$password = "yyy";
$database = "dbdb";
$conn = new mysqli($servername, $username, $password, $database);
if($conn->connect_error){
die("FAILED TO CONNECT : " . $conn->connect_error);
}
?>
to db_local.php (connect to my local server)
<?php
$servername = "192.168.x.xx";
$username = "";
$password = "";
$database = "info";
$conn = new mysqli($servername, $username, $password, $database);
if($conn->connect_error){
die("FAILED TO CONNECT : " . $conn->connect_error);
}
?>
Is it possible? I only need to do that as a first aid solution in my main problem. I am getting this kind of error:
FAILED TO CONNECT : Can't connect to MySQL server on '192.168.x.xx' (110)

mysql might only accept connections from localhost, if you are connecting to the db from your local computer then you need to change the address to localhost
If you are not connecting to the DB via local machine then you need to make sure that port 3306 is open on the server, selinux is off ( assuming you are on linux ) and that you have the proper credentials.
If that doesnt work edit your my.cnf and change the line bind-address: 127.0.0.1 to the machines local ip.

Hope this works...
https://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
connecting to mysql server on another PC in LAN

Related

Connection to database using XAMPP

It is my first time using PHP. I am using XAMPP on a mac.
MySQL, Apache, and localhost:8080 are all working right now.
I created this file called test.php and saved it inside lampp/htdocs:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Connection</title>
</head>
<body>
<?php
$servername = "localhost:8080";
$username = "root";
$password = "root";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error($conn));
}
else
{
echo "Connected successfully";
}
?>
</body>
</html>
However, when I go to localhose:8080/test.php a blank page opens but nothing shows up
Port 8080 is for your web server. Your MySQL server listens by default on port 3306. You can check in your XAMPP settings if the port is different, but it should be 3306. This means that you can omit the port number.
To connect to the database using mysqli you only need 3 lines of code:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'pass', 'db_name', 3306); // you can omit the last argument
$mysqli->set_charset('utf8mb4'); // always set the charset
The servername argument to mysqli_connect been to be just the servername.
If you want to supply a port number then that goes in the fifth argument to mysqli_connect. It isn't mixed into the first.
That said:
since localhose:8080/test.php shows a web page, it is clear that you are running your HTTP server on port 8080, not your database server.
for performance reasons you should prefer using a socket connection to MySQL/MariaDB, not a TCP/IP port

How to fix MySQL State [2002] Database "Connection Refused "

I am setting a new live server for my Laravel application its work perfectly on localhost but not on live server. Now its showing SQL State [2002] connection refused. I also try with mysqli_connect and PDO but the error remains the same. Is possible a problem with Hosting Provider?
<?php
$servername = "examrunner.com";
$database = "XXXXXXXX";
$username = "XXXXXXX";
$password = "XXXXXXXXXX";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Use server name as localhost. Because cPanel hosts your databases locally, use localhost as the database's hostname.
$servername = "localhost";
Please check below mentioned points to resolve this issue :
Check database and user exists
Make sure that Database user and Database connected to each other and you have given sufficient privileges to user.
If site hosted on current serve user hostname = 'localhost'
Try to print detailed error.

Connect MYSQL to webserver

I have a web server running on a raspberry pi model B, i have port forwarded port 80 for http and this is successfully working. However, because there is a login page on my site, i need mysql. How do i connect my current site using php to a mysql date base, will i need to port forward the mysql port?
i've pasted the code below:
<?php
/*PHP for connecting website to database*/
$servername = "ip/hostname";
$database = "database";
$username = "username";
$password = "password";
$conn = mysqli_connect($servername, $username, $password,
$database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
No, you will not need port forward. It's not the user who is logging in into mysql database, it's the php server doing the connection.
As a server hostname in your php file you can simply use localhost, if the mySQL db and php server are on same device/machine.

mysqli_connect connection error in GoDaddy

My system using core php and it is working fine in localhost. When I host into live then there is an error occurs site not working. I hosted in GoDaddy server and below are the code.
//connection code
<?php
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "******";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
But i get below error in browser
PHP Warning: mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it.
in G:\PleskVhosts\projectname.com\httpdocs\pages\header.php on line 13
Please help me to fix it.
Not a problem in your code. May be service is not listing on your port or firewall is blocking your connection. In this case most possible reason is firewall.
You need to try "netstat -anb" for checking a running expected port. Here is a link my be help you:
No connection could be made because the target machine actively refused it?

Connect to remote MySql server with XAMPP

I have an script that i want it to run for more that 5 minutes. But my current hosting service does not allow me to modify the max_time_limit in the php.ini (That is set to 2 minutes)...
So i thought that i could run the script with XAMPP and send the data to my database but i cant connect to the remote database.
This is the code for the connection:
$servername = "217.70.186.108"; //I've also tried with the name of the webpage (metagame.gg)
$username = "the_username"; //the username is not root since I've read that root can only connect from localhost. This user has all privileges. This user was created with the permision to be connected from any server (%)
$password = "the_password";
$dbname = "the_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
It displays this error:
Connection failed: An error occurred during the attempt to connect because the connected party did not properly responded after a period of time, or failed in the established connection because connected host has failed to respond.
Any help would be highly appreciated

Categories