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.
Related
I'm kind of new to programming and I'm only trying to connect to database using xampp but it gives me this error.
Warning: mysqli_connect(): (HY000/2002): No connection could be made
because the target machine actively refused it.
$conn = mysqli_connect('localhost', 'root', '', 'db');
I tried running the same code on a different PC and it works fine.
so i guess the problem lies within PC that I'm working on.
Any suggestions what to configure?
If you are sure that login and password are all correct, then check port number.
Default MySQL port is 3306. Maybe you've got different one on this machine.
Or, it is also possible, default port in PHP may be changed.
See XAMPP configuration to see MySQL port and add it to your server address.
PS: you may also see privileges for "localhost" connection. See 'user' table in 'mysql' database. As you have XAMPP installed, you should have phpmyadmin as well.
I am trying to use mysqli to insert some data into a MySQL database (let's call the schema myDatabase), but cannot successfully connect. Here's the code snippet to connect:
...
$config = parse_ini_file('../includes/config.ini');
$username = $config['username'];
$password = $config['password'];
$dbname = $config['dbname'];
$server = $config['server'];
$conn = new mysqli($server, $username, $password, $dbname);
if (!$conn || $conn->connect_error) {
die( 'Connection Failed: ('.$conn->connect_errno.') '.$conn->connect_error);
}
...
I get the following result:
Connection Failed: (1045) Access denied for user 'myUser'#'my.laptop.ip.address' (using password: YES)
Here's some details on the set-up, in case they are relevant:
The code is on my laptop running Windows 7 and using PHP 5.3.5 that came with xammpp.
The database is hosted on a remote server with MySQL5.1.52. I created a user to which I granted all privileges on myDatabase.*. No host was specified for the user (e.g. 'myUser'#'%'), as I am still in development and don't know the ip address where the code for the live application will be hosted.
If I ssh onto the database server, I can connect to mysql using the credentials for myUser and access the tables in myDatabase. I have another schema on this same server which is accessed by a different user, and have been able to use mysqli to connect without any problems.
Just to be sure it wasn't a typo, I dropped the user, and created it again, copying and pasting the username and password from the config.ini file used in my php code (and flushed privileges, of course). I did this again, except this time the host was specified, e.g. CREATE USER 'myUser'#'my.laptop.ip.address' IDENTIFIED BY 'myPassword'. I keep getting the same error and now I'm completely stumped.
Help, please.
On your mysql machine hit:
GRANT ALL PRIVILEGES ON database.* TO 'myUser'#'%' IDENTIFIED BY 'newpassword';
FLUSH PRIVILEGES;
This will allow the user to connect from any host. Once it works, you can limit it to just a specific host and database.
Okay, this is strange, but it appears the problem had to do with the password I was using. The original one contained some special characters ($, & +). When I changed it so that it only contained numbers, letters and underscore, it worked.
Is this real, or did I accidentally do something else without realizing that turned out to be the actual solution?
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!
I've done a bit of research on this today, but nothing seems to address this issue. I recently upgraded to PHP 5.3.3 from 5.1.6, as well as upgraded MySQL to 5.5 from 5.0. Afterwards, the following code generates an error saying "Can't connect to mysql database":
$connection = mysql_pconnect($dbhost, $dbusername, $dbpassword);
if (!$connection) {
//Can't connect
die('Could not connect: ' . mysql_error());
return;
}
And get the following error:
Warning: mysql_pconnect(): Can't connect to MySQL server on '199.59.157.103' (13) in /var/www/html/ws/Cust/customerWS_1_1.php on line 19 Could not connect: Can't connect to MySQL server on '199.59.157.103' (13)
I am able to connect to the remote host via the command line, and have tried everything from resetting the password to shutting down IP Tables. I'm kind of at a loss - so any help would be appreciated.
We had a similar problem with the same error message "Can't connect to mysql database" and maybe the following points will be also usefull:
check mysql user privileges (in our case and with external connections we had the old server IP address (allow access) in the table) GRANT ALL PRIVILEGES ON tablename.* TO 'username'#'145.1.1.2';
-> reload privileges after changes (FLUSH PRIVILEGES;)
disable the SELinux parameter. In new Plesk versions (>10?) this would be set enabled automatically...
--> in /etc/selinux/config change the line that says:
SELINUX=enforcing to SELINUX=disabled
See further details: http://googolflex.com/?p=482
There is a PHP 5.3 mysql driver restriction related to old authentication scheme hash stored in the database by previous mysql server. This could cause a problem with stored passwords so the databases will be unaccessible with old passwords.
To solve the problem you should recreate the mysql users with the same login and same password.
When you say "I am able to connect to the remote host via the command line" do you mean, you are connecting to the MySQL server REMOTELY? Or that you are SSHing to your MySQL server and connecting to it from the MySQL server's command line? If the latter, it's possible that when you upgraded to MySQL 5.5 you accidentally stopped MySQL from binding / listening on a public IP. Check your my.cnf.
Otherwise: is your PHP code being run on the same server as your MySQL server? It's possible that your permissions could be off. MySQL permissions are very particular about connecting to the MySQL server via it's IP address if the client only has access to connect from 'localhost', and vice-versa.
First you should make sure that the connect parameters $dbhost, $dbusername and $dbpassword are correct. Assuming these are correct, you should check your mysql ini settings. I would assume that the bind-address is set to 127.0.0.1 or localhost, but not to a public IP address.
Also note that it's a security risk if you can connect to mysql from a public IP address. You may want to look into a solution that does not require such connection.
How to log with a diferent user in a MYSQL remote database?
Here's what I've done:
Logged as root in the MYSQL:
'create user 'user'#'%' IDENTIFIED BY 'password';
'grant select on *.* to 'user'#'%';
Then I setted a PHP script which connection is this one:
$con = mysql_pconnect("xxx.xx.xxx.xxx","user","password");
$selected = mysql_select_db("database",$con);
Aaaand it isn't working:
I'm using LAMP on a cloud server, by the way;
Warning: mysql_pconnect() [function.mysql-pconnect]: Can't connect to MySQL server on 'xxx.xx.xxx.xx' (10061) in D:\path\index.php on line 21
What am I doing wrong?
EDIT: Not a firewell issue;
You might want to check the MySQL documentation on this specific problem. If I had to guess, I would say that your MySQL server may be bound only to the local (127.0.0.1) address. To troubleshoot you should probably try connecting to the server using the command line MySQL client in order to get a better idea of why exactly the connection isn't being made.
Seems to me like a firewall issue. You should check if the machine hosting the MySQL server allows connection on the port 3306 from external IPs as well.
Check if the server you have to connect to has the firewall open on the port you are trying to connect... default port is 3306
you can use
mysqladmin -h localhost
to see check what is the port mysql is using