Error connecting PHP to MariaDB database - php

I am trying to create a simple web app that prints out database info from MariaDB/MySQL using PHP, im not even at that stage yet... I have trouble when connecting to the database and get the following error:
Host '10.155.237.62' is not allowed to connect to this MariaDB server
Now,
I have not done a secure installation so my Mariadb Server allows access from wildcard host and infact any host.I have also tried the code with the root account but received the same error. Can anyone tell me if its my code or whether its a procedure im not aware of that disallows connectors to access the information?
Here is my code:
<?php
$con=mysqli_connect("vm-007.server.com","testuser","testpass","my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MariaDB: " . mysqli_connect_error();
}
?>
Thanks in advance!

i would test your mysql database access from command line from remote machine
mysql -h vm-007.server.com -u testuser my_db -p
it will ask you for a password and type that in.
If it fails then you are probably dealing with a mysql setup problem.
likely you are dealing with something like the mysql daemon only listening to the localhost interface i.e. 127.0.0.1 . Even if it allows access from any host if it isn't listening to a give interface like eth0 it doesn't matter.

If you have not created a database, do it from a cmd prompt:
CREATE DATABASE blobblablob;
Now use the USE keyword for that name like so:
USE blobblablob;
Now give yourself permission to use your database:
GRANT ALL ON 'blobblablob' TO 'me'#'localhost' IDENTIFIED BY 'mypassword';
Don't forget to end all your statements with a ; (semicolon).
Now get out of the cmd prompt by typing exit.
You can check in your browser your PHP scripts. They should start working for that database now
since you have the controluser permissions.

Go to user account->add user account->create account with user name and password... let host name be %...click ok
click privileges on created user and grant global permission.
then change ur localhost name as ip:192.168.3.2 and username as newcreatedusername and password .... save it.
open your application from any other remote system along with ip:port ---192.168.1.2:9090/dashboard/working-files/welcome.php-------press enter

Related

Remote SQL connection: mysqli_connect(): (HY000/1045): Access denied for user

I have a database that I want to transfer to my remote server from localhost. The file being too big, I wrote the code to copy the table. But the problem the connection is not successful. My code looks like this.
$server_conn=mysqli_connect($db_host,$db_user,$db_pass,$db_name);
if (!$server_conn) {
die("Server Connection Fail: " . mysqli_connect_error());
}
Where $db_host is the ip address of my server.
When I load the script the error I get is
Server Connection Fail: Access denied for user db_user#'117.202.126.83' (using password: YES)
The credentials are correct, the host is correct, and the weirdest part is 117.202.126.83 is my IP address.
I don't know how is it ignoring the host and taking my connection IP address for host. Even when I give the db_host as the IP address of my remote server.
Also, I am running the script using Easy PHP 14.1 running APACHE 2.4.7,MYSQL 5.6.15 and PHP 5.4.24.
What am I missing?
UPDATE: I am not sure if I was stupid or if the hostgator instructions were.
They ask us to add the cpanel username followed by an underscore before the database name and database username.
In the cpanel, the cpanel username is prefixed by force to all user and db names. So in my case, I had the variables as cpanelUsername_cpanelUsername_databaseName and so on.
Thank you guys for your replies
Go to the cpanel where your phpMyAdmin has the access.
Click on Remote MySQL
Add the access host (Your local IP which is running the script)
Then you can get access to remote mysql server.
You can then test the connection using MySQL Workbench ()
are you sure you use the proper external database hostname/ip?

MySQL Can't Connect to External Server

I am trying to connect to an external server on a fresh MySQL install, but it just seems to timeout. Access has been granted by the other servers admin to the particular account and ip address. I try running the following:
mysql --host=hostname -u username -ppassword database
But just get the following:
ERROR 2003 (HY000): Can't connect to MySQL server on 'hostname' (110)
I have adjusted the following in my /etc/mysql/my.cnf file:
bind-address = 0.0.0.0
and restarted, but it didn't help. Any recommendations?
If you have checked that the firewall is letting MySQL traffic (usually port 3306) pass through and the service is running, the most probable cause is fine grained permission on the server. The user may not have been allowed to connect from any host, probably the permissions to connect are IP based or something similar.
I would try to use that code but not providing the password, like:
mysql -h hostname -u username -p database
and intert the password after that command (it will ask for it).
Also you could try to provide a port: --port=3306 for example.
You can also try to provide the protocol --protocol=TCP.
The only last idea that i have is that you dont provide the database and enter the database after you access the server...
sorry, can't do anything else from here with that data.
Try to grant the user to access database from the remote computer.
to do that:
mysql> GRANT all on database.* to 'username'#'%' IDENTIFIED BY 'user_password';
this is not recomended becouse this will allow connection to your mysql server from anywhere.
for allow connection to user in a certain ip address (we use 8.8.8.8 as example) use code below
mysql> GRANT all on database.* to 'username'#'8.8.8.8' IDENTIFIED BY 'user_password';
The problem lies in your permisions granted to your user. try changing permisions and hope it will help you .

cant connect database server using ip address

I am using PHP and MySQL to make a database connection , problem is :
the web application project in my local machine when am trying connect to database remote server every thing is work fine
mysql_connect("x.xx.xx.x","username","password") or die(mysql_error());
when i deploy the web application in the remote server that have the mysql database and try to use that web application am getting no database error connection !! if i change database connection to :
mysql_connect("localhost","username","password") or die(mysql_error());
using localhost (127.0.0.1) instead of using ip address ever thing work fine , i have tried check /etc/my.conf file and add bind-address=192.168.15.15 and restart mysql server
still cant connect to database
any idea !!!
You need to check two things. First, make sure the port 3306 is open for connections. Second, if you're connecting remotely, you'll need to create a wildcard user (The host will be a percentage sign %). Take a look at the mysql.user table. If the user you're trying to connect to has a Host of localhost, you won't be able to connect remotely.
To create a new user:
CREATE USER 'newuser'#'%' IDENTIFIED BY 'password';
Give that user permissions, I suggest to only the database in question:
GRANT ALL PRIVILEGES ON your_database.* TO 'newuser'#'%';
Finally, reload the privileges:
FLUSH PRIVILEGES;
problem was i was enabling the SElinux , so i enabled httpd network with commad :
setsebool -P httpd_can_network_connect=1

Access remote database MySQL via PHP

I have a MySQL database set up on 000webhost.com. I want to access the data in my database via PHP. I tried:
<?php
include("connect.php");
mysql_select_db("XXXXXXX_users", $con) or die(mysql_error());
$result = mysql_query("SELECT * FROM data ORDER BY id DESC");
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$user = $row['usrname'];
}
echo "$user";
?>
But it always returns as: "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."
What do I do?
I do not have root access
I'm trying to run the script on Apache
When debugging this sort of thing, it's always good to make sure you can connect using the command line mysql tool first, to rule out issues related to PHP itself.
$ mysql -u myuser -h mysql.example.com -p
You'll get a more descriptive error message as well, e.g. ERROR 1045 (28000): Access denied for user 'root'#'remote.example.com' (using password: YES)
If the mysql port is even open on a public interface to begin with (which is usually a terrible idea), mysql itself has its own layer of access control based on the connecting host. You may need to have an account created specifically with a wildcard hostname, like so:
CREATE USER 'myuser'#'%' IDENTIFIED BY 'mypassword';
The #'%' part, specifically, allows connection from any host, local or remote.
Once you can successfully connect from the command line, it's then a simple issue of replicating your command line arguments as arguments to mysql_connect()
A lot(if not all?) of hosting providers restrict remote access to the mysql database. Meaning that you can only connect via localhost. There is probably a mysql config section on the hosting provider that will allow you to config an ip address from which a certain user can connect from. Good luck.
Most of hosts doesn't provide a remote a access to MySQL server's as it a security risk, may you should try another free MySQL server's that allow remote access, try this but its a bit slow.

php access to remote database

Help!
I have a PHP (PHP 5.2.5) script on HOST1 trying to connect to an MySql database HOST2. Both hosts are in Shared Host environments controlled through CPanel.
HOST2 is set to allow remote database connections from HOST1.
The PHP connect I'm using is:-
$h2 = IPADDRESS;
$dbu = DBUSER;
$dbp = DBPASS;
$DBlink = mysql_connect($h2, $dbu, $dbp);
This always fails with:-
Access denied for user '<dbusername>'#'***SOMESTRING***' (using password: YES)
nb: SOMESTRING looks like it could be something to do with the shared host environment.
Any ideas???
BTW: I can make remote connections to HOST2 from my laptop using OpenOffice via ODBC, and SQLyog. The SQLyog and ODBC settings are exactly the same as the PHP script is trying to use.
somestring is probably the reverse-lookup for your web-server.
Can you modify privileges from your cPanel? Have you done anything to allow access from your workstation (ODBC)?
The error-message seems to indicate that you have network-access to the mysql-server, but not privileges for your username from that specific host.
If you're allowed to grant privileges for your database, invoking:
GRANT SELECT ON database.* TO username#ip.address.of.host1 IDENTIFIED BY 'password'
might work for you. I just wrote this out of my head, you might want to doublecheck the syntax in mysql-docs.
Have you read the MySQL documentation on Causes of Access denied Errors?
Have you contacted support for your hosting provider? They should have access to troubleshoot the database connection. People on the internet do not have access.
Do you need to specify the database name? Your account might have access to connect only to a specific database. The mysql_connect() function does not allow you do specify the database, but new mysqli() does. I'm not sure if this is relevant -- it might allow you to connect but give you errors when you try to query tables that aren't in your database.
Are you sure you're using the right password? MySQL allows each account to have a different password per client host. Admittedly, this is not a common configuration, but it's possible. Your hosting provider should be able to tell you.
Just some ideas:
HOST1 does not have remote access to HOST2 (shared host is disallowing)
MySQL account does not have access from HOST1 (IP address specified on account creation, or wildcard)
Edit:
In response to your comment, I meant that HOST1 cannot get to the MySQL port on HOST2. Web services will work, of course, because port 80 is open to the public. As another user pointed out though, you are getting a response, so you are reaching it. I would try specifying the DB, and double checking the account creation command you ran.
For the second piece, I meant this: http://dev.mysql.com/doc/refman/5.0/en/adding-users.html
You can specify what host the username can connect from. If it isn't set to HOST2's IP or the wildcard, HOST2 can't log in with those credentials.
The error message means that you can contact the mySql server, but the user you are trying to log in as, does not have access.
Either the user does not have access at all, or it has access locally, but not from the host you are connecting from.
You should try to use the hostname and port like $h2 = IPADDRESS:3307;

Categories