Connecting to MySQL database in XAMPP on Mac - php

I am using cakePHP to connect to a database in XAMPP phpMyAdmin. I have added a new user, set the password and set the privilleges. I cannot for the life me create a connection to the database on my localhost. What I have done so far.... I have a live web server with a database. I can connect to this database no problem. I have a database running in MySQL workbench on localhost. I can connect to this no problem. This tells me that there is nothing wrong with my connection code and the problem must sit with phpMyAdmin in XAMPP. I have created a simple connection PHP script to try and connect to the database and it doesn't work. The same script can connect to my live web server and my localhost MySQL workbench database but not my XAMPP database. My connection script is as follows:
<?php
$con= mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO Persons (FirstName) VALUES ('Glenn')"))
{
echo("Error description: " . mysqli_error($con));
}
mysqli_close($con);
?>
The error message I get is;
Failed to connect to MySQL: Access denied for user 'root'#'localhost' (using password: NO)Error description:
If i go to phpMyAdmin in XAMPP and go to the test database I can clearly see that this user has full access. I have tried using the host name as 127.0.0.1, localhost and ::1. Nothing seems to work. I just dont understand why XAMPP will not let me connect to the database. I hope I'm not missing something obvious so any help will really be appreciated. here is a screen shot of the users and their privilleges for the database test

That's the second reason why one should never use 'root' unless one knows exactly what one is doing. Only a few expirenced ones really know what they're doing each and every moment...
Add a specific useraccount like 'data' on your localhost and grant it the needed priviledges on your DB.

I am actually using the username 'login' with an auto generated password that is of high complexity. This however doesn't work either. I have used the 'root' user with no password to highlight the fact that not even the admin accounts can connect.

Open config.inc.php file in the phpmyadmin directory
Find line 21: $cfg['Servers'][$i]['password'] = '';
Change it to: $cfg['Servers'][$i]['password'] = 'your_password';
Restart XAMPP

Related

XAMPP - Access denied for user 'michael'#'localhost' (using password: YES). Using a php file to connect to the MySQL

I am getting this error when I try to use a php login page to connect to the database. I have both apache and MySQL running. My apache is listening on port 8080. I have created a user "michael" with password "17701788" with privileges to all databases using the phpmyadmin interface. I have tried changing the conf.ini file of phpmyadmin but it still didnt work.
This is my dbconnect.php file
<?php
$dbConn = mysqli_connect('localhost', 'michael', '17701788', 'autoservice');
if(!$dbConn) {
die("Failed to connect to database " . mysqli_connect_error());
}
?>
Check the hostname. You might wanna try 127.0.0.1 instead of localhost. Its the same thing but sometimes things get funny. if this does not work. delete the current created user and create another. this time;
1. be sure not to copy and paste the password especially as it could have white spaces.
2. Enter each credential carefully.
run and lets see.
When the coffee gets finished, stress makes us not see very tiny errors. lol

php can't recognize table from phpMyAdmin

I created tables in phpMyAdmin - but when I try to connect php to the table it says:
can't use college unknown database 'college'
I was trying to move a zip file to the folder where the php file is (using xampp):
<?php
define('DB_NAME','college');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_HOST','localhost');
$link=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$link){
die('couldnt connect:'.mysql_error());
}
$db_selected=mysql_select_db(DB_NAME,$link);
if(!$db_selected){
die('can use'.DB_NAME.':'.mysql_error());
}
echo 'connect successful';
?>
First:
Confirm that you are accessing phpMyAdmin on the correct host. In your code, it's clear that PHP is attempting to connect to MySQL on localhost. Is phpMyAdmin running on localhost? If not, then you've identified your error - ensure that you are connecting to the correct server - wherever you are running phpMyAdmin.
Second:
If you're sure that you are running phpMyAdmin on the same host as you're trying to connect to with your php, then double-check that the correct database actually exists.
Connect to phpMyAdmin, and select the tab labeled 'SQL' at the top. In the query box, enter SHOW DATABASES;, then select the 'go' button underneath the query box.
On the results page, ensure that your 'college' database exists. If it's not listed in the results, then you will need to re-create the database.
Otherwise, it's likely a permissions issue with your user (root) accessing your 'college' database.
If you provide more detailed information, we may be able to provide more assistance.

XAMPP Mysql connection error in php from remote system

I have windows 7 system and installed xampp in it.
I can access phpmyadmin using http://ipaddress:8080/phpmyadmin from remote system.
But if i try to access it from php it will give me error :
Warning: mysqli::mysqli(): (HY000/2013): Lost connection to MySQL server at 'reading initial communication packet', system error: 20
My php code is
define("HOST", "X.X.X.X");
define("PORT", "port"); // The host you want to connect to.
define("USER", "user"); // The database username.
define("PASSWORD", "password"); // The database password.
define("DATABASE", "DBname"); // The database name.
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE, PORT);
// or without port $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
I have also tried firewall turning off, and also allow port 3306 to firewall, but no luck. Granted all PRIVILEGES to this user with host any (%).
I have also tried using bind_address = 0.0.0.0, commenting that line, and also bind_address = X.X.X.X (my IP).
Also tried changing socket type from "MySQL" to "TCP/IP". skip-networking line is also commented default.
I have tried to access from command prompt using:
mysql -h X.X.X.X -u root -p
This will give error:
Warning: mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on 'X.X.X.X' (110).
Using localhost in same system in connection string it works fine.
Where i am wrong i can't figure out. I have searched and tried all possible solutions.
Thanks for help.
Note: rather than above, right now my conf file is same as at time of installation. Please don't mark this question as duplicate.
Edit: #Jay Blanchard, As i have referred that solution also, in that case system error : 0, in my case system error : 20. Thanks for help.
My research says that error 20 on sql means it is being ran from a user that is not granted system admin rights. This limits the abilities of the sql server. The way to fix this is make sure the user the server is being ran from has admin rights. This can be done in the control panel from a windows user that has admin rights.

PDO access denied to external host, but I can access to phpmyadmin cp

I'm trying to connect to an external database through this script:
$dsn = 'mysql:host=xxx.xxx.xxx.xxx;dbname=dbname';
$user = 'user';
$password = 'pass';
try {
$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
die();
}
Regarding the data (host, username, database and password) everything is correct because when I tip xxx.xxx.xxx.xxx/phpmyadmin on the browser and I enter the user and password it let me in to the database with permissions to create, delete and such.
But when I try to connect through PDO it gives me the "Connection failed: SQLSTATE[28000] [1045] Acess denied for..."
The information displayed on the PHPMYADMIN panel is as follows:
MySQL
Server: Localhost via UNIX socket
Server version: 5.5.20
Protocol version: 10
User: user#localhost
MySQL charset: UTF-8 Unicode (utf8)
Web server
Apache
MySQL client version: 5.5.20
PHP extension: mysqli
Is it possible that I'm forced to use mysqli due to that "PHP extension: mysqli" property?
Thanks in advance!!!
Nothing seems wrong with your code except the setAttribute method is setting the error output to raise warnings.. but you're in a try/catch block.. so you may want to consider using PDO::ERRMODE_EXCEPTION.
Secondly, check your remote permissions for that username/password. There may be an IP limitation for that user account. You can use the wildcard % for the HOST on user accounts or (suggested) provide the server IP that is connecting TO the database.
I would venture a guess that phpmyadmin is working properly because phpmyadmin is on the host you're trying to connect to. My guess is that the server or user account is configured to only have access via 127.0.0.1/localhost - you can another record for the user you are trying to connect with and provide a wildcard or specific host for that user. Based on the information you've provided here, it seems like you'll just have to get MySQL setup to accept connections from other hosts.
Meanwhile I've tried to run the application on local but it seems that it can't connect either, so maybe this can be of some help when it comes to identify the problem above.
This time (in localhost), the error is:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'xxxxxxx_xxxxxxx'#'localhost' (using password: YES)
I created the user on my own phpmyadmin granting him all the permissions over this database, so I can't understand why even in localhost I can't connect to it. Can it be related to the problem above or is it something completely different that should go on a different topic? Thank you!

How to Connect to a Mysql database using PHP?

<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
This is the code I am using to check if I am able to connect to Mysql.
I am having problem connecting using this code. Should I change localhost to the name of the website?
I tried ("www.abc.com","login username", "password") even this is not working.
EDIT:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mobile4_you'#'cl79.blahblah.com' (using password: YES) in /home/mobilew4/public_html/mysql.php on line 2 Access denied for user 'mobile4_you'#'cl79.blahblah.com' (using password: YES)
If you're using something like cPanel, it will prefix your username and your database with your username for cpanel, for example:
> cPanel login: foo
> Database User: bar
> Database: ey
Your database user will be foo_bar and your database will be called foo_ey
Localhost refers to where the database is running. If it's running on the same server as your PHP server localhost should work fine. What kind of problems are you having? Are you sure that your username/password is correct?
You should replace :
localhost by the name of the server on which your MySQL server is
if your MySQL server is on the same server as PHP, then it's localhost
username by the login that allows you to access your database
password by the corresponding password
Of course, this is supposing you actually have a MySQL server up and running, with a user account created on it.
Didn't you hosting company provide you with any information related to MySQL ? Using those informations, what error message does mysql_error() give ?
Do you have a mysql user which you've set up? You'll need to do that. In cPanel you can do it by clicking on MySQL databases, and on phpMyAdmin you can just add a new user on the start page. Put that users' username and password in.
You need to pay attention to the error, if any, reported by mysql_error(). This should give you some information you can use to diagnose the connection problem.
See this MySQL Forge wiki page about connection failure:
http://forge.mysql.com/wiki/Error2003-CantConnectToMySQLServer
Or this page from the MySQL Manual:
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html

Categories