Unable to select database(remote) - php

every time I try to connect to database, I get this message database select error .Access denied for user '<username>'#'localhost' to database '<database>' . My site is hosted remotely . To connect to database , my connect.php is like :
$host = 'localhost';
$user = '<username>';
$pass = '<password>';
$db = '<database>';
$conn = mysql_connect($host,$user,$pass) or die('Unable to connect to host ');
#mysql_select_db($db,$conn) or die('database select error .'.mysql_error());
I have cross checked the username and password,its correct and I have given all the privileges to the user <username> .
Whats going wrong ?

Most likely, the server on which the database is hosted it set to refuse connections from any request that doesn't originate from a white-listed source. This is particularly true if you're accessing a database on a shared-hosting plan, such as a Hostmonster, 1&1, etc. plan.
If it's not a shared host, you need to change "localhost" to be the IP and port number of the server+MySQL port,

Try this
mysql_select_db($db,$conn) or die('database select error .'.mysql_error());

Related

Can't connect to MySQL server on 'XXX.XXX.XXX.XXXX' (111)

I have read up on the above error and from the way I understand, there could be many reasons that error pops up. I will try to be as specific as possible.
I have made a php file to do a simple database connect:
<?php
header('Access-Control-Allow-Origin: *');
$servername = "188.166.***.***";
$username = "root";
$password = "mypassword";
$dbname = "vod";
$con = new mysqli($servername,$username,$password,$dbname);
if($con->connect_error){
die("error ".$con->connect_error);
}
echo "success";
?>
When I run the above code I get the error:
Can't connect to MySQL server on 'XXX.XXX.XXX.XXXX' (111)
When I enter XXX.XXX.XXX.XXX in the URL bar I am taken to the index.html file of the server. And when I enter XXX.XXX.XXX.XXX/phpmyadmin I am taken to phpmyadmin.
I hope I made my question clear.
It depends on you if you want to do it like this:
CREATE USER 'user'#'%' IDENTIFIED BY 'userpassword';
GRANT ALL PRIVILEGES ON *.* TO 'user'#'%' WITH GRANT OPTION;
you can run it at your query database. just change user and password. good luck

Does a PHP file that attempts to connect to a database have to exist on the same server as the DB?

For example, when trying to connect to server xy.xy.xyz.xyz like below, does the script have to exist on that server to work?
<?php
$servername = "xy.xy.xyz.xyz";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
No it doesn't. The DB server has to be configured to allow remote connections though.
You can check this for further details for MySQL.
No, what's most likely happening is that your MySQL database isn't allowing users to sign in if they are trying to access it from an outside IP address.
Run the following command through MySQL
SELECT *
FROM information_schema.user_privileges
WHERE GRANTEE LIKE '%username%';
Change the username value to whatever the actual user name value is. If you're seeing a bunch of rows where the GRANTEE column is 'username'#'localhost' and nothing that looks like 'username'#'%', then your user is being limited to only localhost (same machine) access.
If you want to grant your user non-local access, you can use the following:
GRANT ALL PRIVILEGES
ON mydatabasename.*
TO 'username'#'%'
IDENTIFIED BY 'mypassword'
In the above, % means my user can connect from anywhere. This example grants all administrator rights to your user, but you can change that if you wish.

PHP - Cannot connect to MySQL database

I am trying to connect to my MySQL database through php, I am managing my Database with phpmyadmin. To login the username is root and I dont have a password. My problem is I cant connect, I get the "Could not connect to mySQL database" message when I try to
Below is my Code
<?php
session_start();
$server = 'localhost';
$db_usernmae = 'root';
$db_password = '';
$database = 'househockey';
if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
if (mysql_select_db($database)) {
# code...
die('couldnt connect to database');
}
?>
Im not sure if it matters, but I am using WAMP and I put my phpmyadmin folder into my htdocs folder.
Thanks
As you have written your code :
if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
This will when connection is true print the following: die('Could not connect to mySQL database'); I think what you need to test your connection, which sounds like it should work:
if(!mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
The ! will negate the returned value of your mysql_connect and tell you if you're connected.
As far as I know, PMA explicitly needs a username and password. Set a root password through
mysqladmin -u root password NEWPASSWORD and then change your PMA config, followed by a server restart. Alternatively, use MySQL workbench. It does more than create entity relationship diagrams (ERDs).
You may run into this problem if you have an anonymous user defined on your database.
"When a client tries to connect to the database, the MySQL server looks through the rows in the user table in a sorted order.
The server uses the first row that matches the most specific username and hostname."
Delete the anonymous user and try again.

Can't access database on my server

I am having some problems with my database. I created a user, and a database, but I can't seem to access it with php.
My code is:
$host = 'anapaiva.pt:2082';
$user = 'anapaiva_p1';
$pass = 'xxxx';
$db = 'anapaiva_mcmm1';
#mysql_connect($host, $user, $pass) or die('err: '.mysql_error());
#mysql_select_db($db) or die('err: '.mysql_error());
And on the webpage, appears the following errors:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'anapaiva_p1'#'apollo' (using password: YES) in /home/anapaiva/public_html/mcmm/connection/dbconn.php on line 6
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/anapaiva/public_html/mcmm/connection/dbconn.php on line 7
The same code was working with my other server, hungergamesportugal.com, but I can't seem to upload the sql file to that database (it says I don't have permissions... I don't know why), and on this server it allows me to import the sql file, but I can't seem to access the database.
Can someone please help? :/
You're using the wrong port. 2082 is for cPanel. 3306 is for MySQL (usually).
Generally, you can omit the port entirely:
$host = 'anapaiva.pt';
You also need to assign the connect function to a variable:
$dh = mysql_connect($host, $user, $pass);
So that you can close the connection when you're done with it:
mysql_close($dh);
You can allow permission using cpanel, add permission to remote mysql. I don't remember exact name, but it was something like, remote database. on clicking you can add ip address to allow database permission. This may help you. search remote database at cpanel. May be this help you, http://forums.cpanel.net/f354/enable-remote-access-customer-database-251951.html
Test same user on localhost
$mysql_host = "localhost";
$mysql_database = "db_name";
$mysql_user = "root";
$mysql_password = "password";
$con = mysql_connect($mysql_host,$mysql_user, $mysql_password);
mysql_select_db($mysql_database, $con);
OR
Check user privileges
Using GRANT statement check whether user has rights.
Reference: http://dev.mysql.com/doc/refman/5.0/en/show-grants.html

Error connecting to SQL server with MSSQL and PHP

$myServer = "sql2008";
//$myUser = "zach";
//$myPass = "pass";
//$myUser = "DOMAIN/zach";
//$myPass = "pass";
//$myUser = "zach#DOMAIN.net";
//$myPass = "pass";
$myUser = "sa";
$myPass = "pass";
$myDB = "Database";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());
So what i have noticed is that i can connect with SA (server admin) but i can not connect with my user name, which is a domain user that has a lot of permissions.
the only one that is giving me an error message is when the username is zach#DOMAIN.net.
Couldn't connect to SQL Server on sql2008. Error: Login failed for user 'zach#DOMAIN.net'.
that leads me to believe that there must be some setting on the server that allows for logins, but i dont know where to look.
*UPDATE****
So after looking in SQL Server Management Studio, I realized that it accepts logins from the sysadmin server roles group. So i added my AD account to the group, but i was still not able to connect with my user name. i used they three approaches i listed above. is there a fourth option?
That being said, is there a way that i can tell it to connect to a group not sysadmin? for security reason i would prefer not to connect to sysadmin?
Go to login account properties, there's must be some tab there about status that has grant and deny option... I don't have SQL in this PC. It looks something like this:
http://www.google.com.ph/imgres?q=sql+login+grant&um=1&hl=tl&client=firefox-a&hs=tvs&sa=N&rls=org.mozilla:en-US:official&biw=1366&bih=602&tbm=isch&tbnid=LMuu3zBDlLIn5M:&imgrefurl=http://kb.bizagi.com/Goto50061.aspx&docid=P4HAWWO6mFEFMM&imgurl=http://kb.bizagi.com/Uploads/Images/SQL%2525204.jpg&w=709&h=630&ei=nB63TteSBqWriAexnszxDA&zoom=1&iact=hc&vpx=360&vpy=292&dur=1302&hovh=212&hovw=238&tx=161&ty=115&sig=103098649454558437088&page=1&tbnh=129&tbnw=146&start=0&ndsp=21&ved=1t:429,r:15,s:0
I'm not into Active Directory that much but I have tried adding a DOMAIN/GROUP to login account in SQL server. Look at this post, it teaches how to add a login for a group in a domain. How to add Active Directory user group as login in SQL Server
The solution that I found was SQL Server did not like the fact that I was trying to use a domain account. I realized that any SQL user account that i created works fine, it is just when I try to use AD accounts it doesn't work.
I am sure that some people will want to use AD, but i don't need it in this situation.

Categories