Error when connecting in phpMyAdmin - php

This may be a stupid question, but I've been following a tutorial online for learning php and I'm using an exercise file from it that is a sql file. The instructor set up a username and password when the database was set up in the MySQL console in WAMP. I know it works this way, but now I'm trying to test it out in a different way by importing it into phpMyAdmin on my server, but I get an error when trying to connect.
It says the database connection failed because I don't have access to the "local MySQL server", and mentions a socket that it's trying to connect through (I'm not sure what that means). I'm wondering if it has something to do with the username and password I am using. Would that carry over in the file I uploaded from the lesson? I tried using the username and password the instructor used, and then I tried using the one I use for logging on to the phpMyAdmin page, but I got the same error both times. Is there another way to set a username and password or I am completely off and the problem lies somewhere else?
Here's the code I use to establish the connection:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
?>
Thanks for your help!!
Edit: the exact error I get is
"Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)"

Related

Connect database from online PHP site hosted in Heroku

I created a php website and host it online by using Heroku recently. However, I couldn't connect my database because I use localhost phpmyadmin database from my online site. How should I connect my localhost database from my online site? Or how to make my host my database online, and then make the connection? Here is my db.php file to make the connection with mysqli, in case if there's any need to tweak the db connection PHP codes.
//step 1: Establish database connection
DEFINE("DB_HOST", 'localhost');
DEFINE("DB_USER", 'root');
DEFINE("DB_PASS", '');
DEFINE("DB_NAME", 'my_db_name');
// Create connection
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Set charset to UFT8
mysqli_set_charset($con, "utf8");
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
If you are hosting your database on your local machine, whilst having a webserver that needs to connect to your database, you will have to make sure your database can be accessed by another machine. The host is only ‘localhost’ if the Local IP is identical for the machine and database. Your db.php is correct and will function properly, as long as you ensure that the connection details are correct, since localhost is not correct there will be errors.
You might want to look into how to portforward depending on what database software/installation you are using. Or look into whether your host offers database solutions sch as or similar to PhpMyAdmin (in case of most webhosts).

I get connection error when I add my database name in connection

Warning: mysqli_connect(): (42000/1044): Access denied for user '-----'#'localhost' to database '----' in ---.php on line 4
Unable to connect to MySQL
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die("Unable to connect to MySQL");
This is how I connect. If I remove DB_NAME from there, I don't get an error and connect database but whenever I add DB_NAME, I get this error. DB_NAME is definitely correct but I don't see why? By the way for DB_HOST, I use localhost.
---Solved---
I finally solved the problem. So, for those of you who have the same problem, I am gonna explain everything. Actually, I decided to use table name before my query and than mysqli gave me another error saying something like "INSERT command denied to user ..." So, I opened up phpMyAdmin and changed my DB_USER to the name that is written in Database Server section in phpMyAdmin. You will see something like this;
Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.5.42-cll - MySQL Community Server (GPL)
Protocol version: 10
User: -HERE-
Server charset: UTF-8 Unicode (utf8)
So you need to use use the username, which is written in User part. After I changed my DB_USER I was able to add DB_NAME. Basically all the problems I was having (not being able to use DB_NAME and not being able to use sql commands) caused my permission.
The key part is; I was using DB_NAME, which I used to create database in my server and it also let me connect to the database but As I can see it is not the root. So, if you are having "denied" error, make sure that you check the values you typed in mysqli_connect!
You can use
# mysqli_connect(hostname, username, password, database)
or
#mysqli_connect(hostname,username, password)
# mysqli_select_db(true, database);
or
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed : mysqli_connect_error();
}
mysqli_select_db($con,"db");
if you are using localhost then try this
<?php
$con = mysqli_connect("localhost", "root", "", "YOUR_DB_NAME");
if (mysqli_connect_error()) {
die ("Could not Connect");
}
?>
and if you are doing this online then replace localhost with host_name root with username "" with password and at last your DB_Name. I hope this would solve your problem :)

Why is my php code not connecting to my remote MySql database?

I'm trying to connect to a remote MySql database and I get this error message:
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\myLocalDiectory\RemoteConn.php on line 9 "Resource not found" error 404.
Here's my php file that I'm trying to use:
<?php
define("DB_SERVER", "serverName");
define("DB_USER", "userName");
define("DB_PASS", "password");
define("DB_NAME", "dbName");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
Am I missing something? Thanks for any help!
The first thing that I would check (if you haven't done so) is that you can in fact connect to the database from the computer that runs your PHP script. This to rule out a network or firewall problem.
The first thing would be pinging the server. In a DOS prompt run:
ping servername
Where "servername" is the same string that you put in your PHP script above. If this does not reply with a string similar to the one below, specifically, the first word is not "Reply":
Reply from 192.168.239.132: bytes=32 time=101ms TTL=124
This means that there is most likely no connectivity between the computer running the PHP script an the mysql server. I would then check if the server and the computer are properly connected to the network, if the server is up, an if there is not firewall in your computer running the PHP script or on the server.
Now, if your test above shows "Reply" to the ping, you can test if you can connect to the Mysql service from your php server. For this you can use Mysql workbench (http://dev.mysql.com/downloads/workbench/) and from there create a connection with the database parameters that you are giving to your script. If you cannot connect with Mysql workbench, you might need to disable a firewall in your Mysql server, a firewall in your computer running PHP, or enable the Mysql server to accept remote connections for the database and username that you use in your PHP script (some distributions Mysql server are installed to only accept local connections for safety).
If the problem is a permission in the server (the user can only connect locally but not from a remote computer for instance), you can enable the permission in the Mysql server with the GRANT command: http://dev.mysql.com/doc/refman/5.1/en/grant.html
define("DB_USER", "userName");
Try root instead of userName and define DB_HOST.

Unable to connect to phpmyadmin on other server

I have a website hosting on www.host1free.com. Earlier I had connected to MySQL database of the host.
Now I am having my own VPS which is accessed by ip address 5.231.36.181. I have also installed phpmyadmin. All works fine within the server.
Now I want to connect my website database to the MySQL server on 5.231.36.181.
The database on the server is accessed by 5.231.36.181/phpmyadmin
and the database on host is accessed by sql12.1freehosting.com/phpmyadmin
How to connect the database to my VPS phpmyadmin ?
whatever the details you are using for logging in to your phpmyadmin use the same usename and password for connecting to database
secondly host can be localhost or your VPS IP Address
The rectangle marked in red contains your IP ,
Use the IP Address of the server and Username and password that you used to login if you are still not getting it post us what you have tried and what error you are getting
connecting to mysql database
// Define database connection constants
define('DB_HOST', 'your IP');
define('DB_USER', 'Your DB USER');
define('DB_PASSWORD', 'db user Password');
define('DB_NAME', 'your dbname');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('cannot connect to MYSQL');
if you are confused you can create a new database user assign the database to the particular user and can use the details to connect to your DB Cheers
You connect your website to MySql database, not to PhpMyAdmin. Enter Host as localhost, username and password. If localhost is not working, try entering IP of database. There must be statet somewhere what params to use to connect to database
Use Your Mysql Details and connect to your dataBase like this:
mysqli_connect('5.231.36.181',username,password,dbname);

Cannot connect to MySQL database, socket error

I'm new to php, and while trying to make a connection on one of my pages to the database I set up on the phpMyAdmin page of my site.
I get this error:
"Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)"
I don't know what a socket is, or why it's trying to go to what looks like a temp file, so I don't even know where to being to troubleshoot this.
The code I'm using to make the initial connection is this:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
I know I should have access to the MySQL databases, since I logged on and made them myself.
I previously set the page up by using WAMP on my computer, and everything worked fine.
It's just when I tried making it live on the site that I ran into this error.
Any help would be awesome!!
Try using 127.0.0.1 instead of localhost.
If that does not solve it, and you have root access to your server, try the following command
service mysql restart
To restart the mysql server.
The first option will probably work. Again, if you have root access to your server, you should change the mysql config to support sockets, since it's better than the TCP-ip connection.
After contacting my hosting service several times, it seems the error occurred because my hosting service recently changed their specifications and now uses "mysql" instead of "localhost" in the host and server fields. I had used "localhost" before with another host service, so I didn't think to change this, and the latest help articles on my host's website had not updated to reflect this.

Categories