Remote mysql connection - mysql

I'd been making a web on localhost and when I tried to move it on host it shows me lots of errors. Seems like it can't connect to my local database. Here's the code:
$host = "myip";
$user = "root";
$pass = "";
$db = "mydatabase";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$on = mysql_fetch_row(mysql_query("SELECT COUNT(online) FROM characters WHERE online=1"));
echo "<br>Online: $on[0]<br><br>";
And here's the output:
Warning: mysql_connect() [function.mysql-connect]: Host 'myip' is not allowed to connect to this MySQL server in * on line 46
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in * on line 48
Online:
Thanks for help

It's a mysql user permission problem. Create a new user with phpmyadmin for example and set it's host to localhost if you connect from localhost, or % if you are connecting from outside.

As Alekc correctly stated, it's a permission problem:
You can do this at the MySQL prompt:
grant all privileges on mydatabase.*
to youruser#'your_ip_address'
identified by 'your_password';
Followed by a:
flush privileges;
And then try to reconnect again. Keep in mind that:
your_ip_adress: it's not the MySQL server's IP address but the IP address you are connecting from (your web server?)
your_password: is the one you must use when establishing the connection on PHP side
Also remember that, even though this solution should work, it's not recommendable granting all privileges to a user that will be used by a web application. You can do the same with fewer permissions.
You can do all this with a nice GUI frontend. I prefer command line.

Related

Perl How to change account host using DBI module?

My company has a Perl application in server with IP : 10.10.3.39
And because of the new rule implemented, I must migrate the database to MySQL DB in server with IP : 10.10.1.18
My company DB administrator has create an account and grant the access for the apps with username : 'user'#'10.10.3.39'. So the account just can be used from server with IP 10.10.3.39
I tried the connection in the server using command mysql -h 10.10.1.18 -u user -p
[hanief#dev39 project]$ mysql -h 10.10.1.18 -u user -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 19484169
Server version: 10.0.15-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
I have a database connection script test_db.pm in Perl like this:
use DBI;
$user = "user";
$pw = 'password';
$dbh = DBI->connect("DBI:mysql:database=mysql;host=10.10.1.18;mysql_enable_utf8=1",$user, $pw, %attr) or die "Cannot connect to MySQL server\n";
And then, I tried to run it using command perl test_db.pm, I got this error
[hanief#dev39 project]$ perl test_db.pm
DBI connect('database=mysql;host=10.10.1.18;port=3306;mysql_enable_utf8=1','user',...)
failed: Access denied for user 'user'#'%' to database 'mysql' at test_db.pm line 6.
Cannot connect to MySQL server
I don't know why the account name suddenly has #'%' behind it
I have tried to change the variable $user to :
$user = "user\#10.10.3.39";
failed: Access denied for user 'user#10.10.3.39'#'10.10.3.39' (using password: YES) at test_db.pm line 6.
$user = "user\#'10.10.3.39'";
failed: Access denied for user 'user#'10.10.3.39''#'10.10.3.39' (using password: YES) at test_db.pm line 6.
$user = "'user'\#'10.10.3.39'";
failed: Access denied for user 'user'#'10.10.3.39'#'10.10.3.39' (using password: YES) at test_db.pm line 6.
But still, didn't work. The server still can't connected to DB server.
I Don't know why there's an extra #'10.10.3.39' behind the user account.
And don't know why % before, suddenly changed to 10.10.3.39
Any solution for my case?
I'm not entirely sure what's going on here, but I think I can shed a little bit of light.
MySQL (and, hence, MariaDB) has a login system that isn't just dependent on usernames and passwords. It also takes into account the host that the connection is coming from. If you look at the mysql.user table, you'll see that the first three columns are Host, User and Password.
The Host column is interesting. It can either contain an IP address or a hostname, but it can also contain the symbol '%', which means "any host".
So when the DBA told you they had:
create an account and grant the access for the apps with username : 'user'#'10.10.3.39'
I'm betting they mean they've inserted the values user and 10.10.3.39 in the mysql.user table.
That means your $user variable needs to be set to user, not user#10.10.3.39 - as MySQL will work out the IP address from your incoming connection.
So when you see errors like this:
failed: Access denied for user 'user#10.10.3.39'#'10.10.3.39'
the bit inside the first pair of quotes ("user#10.10.3.39") is the username you're trying to use and the bit inside the second pair of quotes ("10.10.3.39") is the IP address that MySQL thinks you're connecting from. Obviously, that's not going to work because that username doesn't exist in the mysql.users table.
So, we're left looking at your first example:
$dbh = DBI->connect("DBI:mysql:database=mysql;host=10.10.1.18;mysql_enable_utf8=1",$user, $pw, %attr)
or die "Cannot connect to MySQL server\n";
Which gives this error:
failed: Access denied for user 'user'#'%'
Now, the fact that this error uses "%" as your hostname, seems to imply that the MariaDB server didn't recognise the IP address that you're coming from as one of the specific IP addresses listed as allowed for your user.
So, two things I would double-check in this instance:
Are you definitely trying to connect from 10.10.3.39? This includes checking if there are any proxies between you and the DB server that might change your IP address.
Is the IP address in the mysql.user table really 10.10.3.39? Or does the value, perhaps, contain a typo?
I'm aware that this isn't a "here's the solution to your problem" post but, hopefully, it explains a bit more about what is going on and gives you a couple of avenues to investigate.

connecting to mysql db in azure linux vm with lamp stack

we are testing azure cloud platform for our future use, we saw digitalocean tutorials on how to install a lamp stack, everything seems find, but we are unable to fine, but we are unable to connect to database, we are using following details to connect
$host = "{our_url}.cloudapp.net:3306";
$user = "root";
$pass = "rootpassword";
$connection = mysql_connect($host,$user,$pass);
if($connection)
{
echo "Great";
}
else
{
echo mysql_error();
}
// we are getting this error can't connect to MySQL server on '{our_url}.cloudapp.net' (111)
things to mention
we have three end points 22 for ssh, 3306 for MySQL, 80 for http
we are using same url that we created which we create while creating ubuntu vm
1) Don't use root account
2) Root account is locked to localhost access by default
3) Create a new user and grant permissions to your database
GRANT ALL PRIVILEGES ON <mydatabase>.* TO '<myuser>'#'<myip>' IDENTIFIED BY '<mypassword>' WITH GRANT OPTION;
4) If you still want to use root account
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY '<rootpassword>' WITH GRANT OPTION;
'%' means any ip. You have lock root access to a specific ip.

MySQL/phpMyAdmin - Only root user can log in

I've been having some issues trying to log in as non-root users from a .php file onto phpMyAdmin.
I've set up a login form that uses the root user to check if the login that someone uses matches an entry in the mysql.User table and then stores the username and password as session variables which I'm using to try and log in to MySQL and perform user specific tasks later.
This is the code I'm having issues with:
$host = "localhost"; // Host name
$username = $_SESSION['myusername']; // Mysql username
$password = $_SESSION['mypassword']; // Mysql password
$db_name = "gcim"; // Database name
$tbl_name = "permissions"; // Table name
// Create connection
$conn = new mysqli($host, $username, $password, $db_name);
myusername and mypassword are just the session variables I've stored and they're set to the values exactly how they should be, "gcim" is the database I'm trying to access and "permissions" is the table
Trying to execute the code results in the following:
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'BUT03'#'localhost' (using password: YES) in C:\xampp\htdocs\gcim\permissions\permissions.php on line 12
Connection failed: Access denied for user 'BUT03'#'localhost' (using password: YES)
I'm pretty sure that the issue isn't a wrong hostname, username or password because even if I change the connection line to:
$conn = new mysqli("localhost", "BUT03", "test", "gcim");
it still gives me the same error
Here's the setup of my mysql.User table:
http://puu.sh/dDxd3/5e25f315f4.png
Anyway, any help would be very much appreciated,
Thanks
EDIT: I've been experimenting and I've come to the conclusion that the ONLY way to get a successful login to MySQL is using the root account with no password, any attempt to log in with a password will give me the same error, as well as removing the password from a non-root account and trying to log in with no password which gives me the following error: Connection failed: Access denied for user ''#'localhost' to database 'gcim'
I have a feeling that there's some issue with the server settings, was I meant to change anything from the default phpMyAdmin settings?
EDIT: I just tried to log in to phpMyAdmin itself as 'BUT03'#'localhost' and I appear to have no privileges at all even though everywhere I can see I've granted all possible privileges to that account, could this be affecting anything and does anyone know what could be causing it?
I assume passowrds are saved not in plain text, but hashed. That would mean your hashed password is 'test', and you actual password is something unknown. Change your password to test with this function
SET PASSWORD FOR 'BUT03'#'localhost' = PASSWORD('test');
see:
http://dev.mysql.com/doc/refman/5.0/en/set-password.html

How to allow remote connection to mysql database in Mac's MAMP

I have a MySQL database in my Mac using MAMP. I need to connect to this database using PHP script from a remote server with IP for example 184.173.9.... I have been trying this for 2 days now with no luck.
I tried some forum where they asked to comment out skip-networking and use bind-address with remote server's IP in my.cnf file in MAMP. Firstly I wasn't finding the my.cnf file in MAMP but eventually found one from Library/support-files/my-medium.cnf. I copied it and placed in tmp/mysql/my.cnf. But nothing works.
I also turned off the firewall in System Preferences/Security & Privacy.
The php code I am using to connect looks like this:
mysql_connect('176.249.200....:3306', 'test', 'test') or die(mysql_error());
Could you please suggest me what else I am missing?
GRANT ALL PRIVILEGES ON database.* TO 'user'#'%' IDENTIFIED BY 'newpassword';
% is a wildcard ,where we connect our MySQL server from any IP address.
you can also do '%.domain.com' or '%.10.10.10' ( for a specific Ip or domain address).
flush privileges for the changes to take effect.
MySql port 3306 is free to be accessed remotely; So we need to bind IP address in the MySQL config file.
vi /etc/mysql/my.cnf
In the file, search for the [mysqld] section block and add the new bind address bind-address = 127.0.0.1
comment it and add your Ip address.
if your Local Ip address changes randomly then use bind-address = 0.0.0.0
Then the last step is to restart the MySql server.
Login in to mysql server in your terminal as
mysql -u usename -p -A -h youripaddress
-A `Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
note : first ping your ip Address. and see the if any drops and latency issue.
I use this below piece of code to connect MySQL server in php.
$conn = '';
try {
$conn = new PDO("mysql:host=ipaddress;dbname=databasename", 'userName', 'password');
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
Here is the output:
GRANT ALL PRIVILEGES ON . TO 'test'#'%' IDENTIFI...
GRANT ALL PRIVILEGES ON test.* TO 'test'#'%'
GRANT ALL PRIVILEGES ON easytask.* TO 'test'#'%'...
IP address 184.173.9.... is a dedicated remote server where I have the PHP script. And 176.249.200.... is my local IP, 3306 is MySQL port.
First make sure it connects (you should be getting a connection denied and not a timeout/connection error) . You can test that by executing:
mysql -h ip.addr.goes.here -p -u test
if it can't connect, then it is either a firewall issue or a config issue.
If it connects and permission is denied, then it is probably a grant issue. create a user then grant access to the table
CREATE USER 'dbusername'#'%' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'dbusername'#'%'
Note, you a user will get access locally if you replace % with localhost, from a specific ip if you specify the ip or only remotely if you specify %. more details on the mysql page http://dev.mysql.com/doc/refman/5.1/en/adding-users.html
after that, flush privileges to make sure they come in action:
flush privileges

Access denied for user 'root'

I downloaded an adobe flex mysql script (from a tutorial I am learning) and ran the script in my sql 5.5. It created a database, but it was stored in my wamp folder under:
C:\wamp\bin\mysql\mysql5.1.36\data
I have a test php page that is supposed to print out the data from the database (from the adobe tutorial).
But, when I load the test page from localhost/test.php it just says:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'#'localhost' (using password: YES) in C:\wamp\www\test.php on line 7
Error connecting to MySQL
And if I run it from localhost:3306/test.php I get just gibberish:
B???
5.1.36-community-log????nKm`zell?ÿ÷??????????????Z\Ap3JZcfVZD???ÿ„Got packets out of order
What password do I use -- my wamp password / login name, or the new mysql5.5 root password? I've tried the mysql5.5 root & password, but I still can't connect.
Why is mysql5.5 loading the database under my older WAMP sql data folder? Is there some conflict having MySQL5.1 installed with WAMP, and MySQL5.5 I just installed separately?
After wasting most of a day on this problem, it turned out that all i had to do was supply the non-localhost hostname. I've no idea why (the user is allowed to connect from Any host), but it works!
root#base:~# mysql -u username -h hostname -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
And in PHP:
Test 1807:
<?php
$username = "username";
$password = "password";
$hostname = "actualhostnamenotlocalhost";
$con = mysqli_connect($hostname, $username, $password);
// Check connection
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($con);
?>
OK.
Please verify if you are using the right password, with the help of phpMyAdmin or my sql work bench
if this is a fresh installation then you will not have any password for the root
You need to set the password and the start connecting to the database using the password that is set
also if you are able to run the script without specifying the password, that means, no password is set for the database
You can use the switch "use password" to NO and connect using root
Same problem after updating my phpmyadmin version, and turn out was the config problem:
step 1)open the corresponding phpmyadmin installation directory , duplicate config.sample.inc.php to config.inc.php under the same directory .
step 2)Open config.inc.php
change
$cfg['Servers'][$i]['AllowNoPassword'] = **false**;
to
$cfg['Servers'][$i]['AllowNoPassword'] = **true**;
and save the file.
I spent almost the whole day resolving this issue so if this help anyone I solved it by
using the following code:
// - Get Connection - //
$this->dbCon = mysqli_connect($this->dbHost,$this->dbUser) or
die("Connection failed:<br/>" . mysqli_connect_error());
// - Select Database - //
mysqli_select_db($this->dbCon,$this->dbName) or
die("Selection failed: <br/>" . mysqli_connect_error());
-and also by setting
$cfg['Servers'][$i]['AllowNoPassword'] = true - in the config.inc.php file.
Hope that helps