connect to mysql database which is in ubuntu server - php

I am using below code to connect MySQL database in PHP.
try {
shell_exec("ssh -f -L 3307:127.0.0.1:3306 ronak#server_ip sleep 60 >> logfile");
$this->_conn = $this->dbh = new PDO('mysql:host=127.0.0.1;dbname=my_db', DB_USER, DB_PASS);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Couldn't connect to database. Please try again!");
}
I want to direct connect with remote MySQL host. Server is on azure virtual machine. How can I do such a configurations?
I already opened 3306 port in azure portal for MySQL. I don't know how to use it in here without ssh tunnel.
Thanks.

1) change my.conf (whatever your mysql conf file is called). And set bind-address to 0.0.0.0 as it is prob 127.0.0.1
2) stop/restart mysql daemon
Connections now are not limited to those from localhost (what you are when you ssh). The default is localhost for obvious security reason until dev guy tweaks it
3) add database user perhaps coming in from diff ip addr
CREATE USER 'fred7'#'192.168.2.7' IDENTIFIED BY 'my_password';
4) grants
GRANT ALL PRIVILEGES ON test7db.* TO 'fred7'#'192.168.2.7';
5) firewall
For 3 and 4, let's say the mysql server is on aws ec2 and i am sitting at a public library using sqlyog or workbench. That prog will alert me if connect failure stating something like connection failed for user 'fred7'#'gfs6.nyc.comcastbusiness.net'. So it is pretty obvious how to do 3 and 4 then.
Good luck!

Related

connect live server from local server in php mysql [duplicate]

I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());
My problem is that I am unable to connect locally, receiving the error:
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
This is not the case when I upload the same PHP file to the server. I am able to query the database with no problems at all.
I am unable to connect via command line either, but I can access cPanel which rules out the chance of my IP being banned accidentally.
My local server is running PHP 5.2.9, the remote server 5.2.12
firewall of the server must be set-up to enable incomming connections on port 3306
you must have a user in MySQL who is allowed to connect from % (any host) (see manual for details)
The current problem is the first one, but right after you resolve it you will likely get the second one.
It is very easy to connect remote MySQL Server Using PHP, what you have to do is:
Create a MySQL User in remote server.
Give Full privilege to the User.
Connect to the Server using PHP Code (Sample Given Below)
$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306
I just solved this kind of a problem.
What I've learned is:
you'll have to edit the my.cnf and set the bind-address = your.mysql.server.address under [mysqld]
comment out skip-networking field
restart mysqld
check if it's running
mysql -u root -h your.mysql.server.address –p
create a user (usr or anything) with % as domain and grant her access to the database in question.
mysql> CREATE USER 'usr'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON testDb.* TO 'monty'#'%' WITH GRANT OPTION;
open firewall for port 3306 (you can use iptables. make sure to open port for eithe reveryone, or if you're in tight securety, then only allow the client address)
restart firewall/iptables
you should be able to now connect mysql server form your client server php script.
This maybe not the answer to poster's question.But this may helpful to people whose face same situation with me:
The client have two network cards,a wireless one and a normal one.
The ping to server can be succeed.However telnet serverAddress 3306 would fail.
And would complain
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
when try to connect to server.So I forbidden the normal network adapters.
And tried telnet serverAddress 3306 it works.And then it work when connect to MySQL server.

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.

MySQL Error 111 Can't connect to server

I have a connection.php file that is suppose to connect to a remote database.
Here is the code:
<?php
try {
$conn = new PDO('mysql:host=IP;port=PORT;dbname=DBNAME', 'USERNAME', 'PASSWORD');
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
Now all my variables I believe are correct since I can connect to the database through Toad. I used this same PDO format for my own database connection through localhost and it works fine. I am not sure what the problem is. Since i can use Toad i believe that the server already allows remote access to it, but i am not sure on that. Any input would be nice.
Also this is the Error that PDOException is coming back with:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'IP' (111)
111 means connection refused,
It probably means that your MySQL server is only listening the localhost interface.
If you have lines like this :
skip-networking
bind-address = 127.0.0.1
You should comment them In your my.cnf configuration file (add a # at the beginning of the lines), and restart MySQL.
well, in you /etc/mysql/my.cnf :
you should find out this:
skip-networking
bind-address = 127.0.0.1
You gotta comment the first one, and change the ip to 0.0.0.0 or to the ip of your remote host that you want to allow (recommended)
Also, you may don't have a user to connect to that host, i mean, mysql users are like 'root'#'192.168.1.1', so you may try to access with a user that doesn't exists. Try to create it and then grant to it all privilegies.
Just execute this couple commands on mysql:
CREATE USER 'root'#'192.168.1.100' IDENTIFIED BY '***';
GRANT ALL PRIVILEGES ON * . * TO 'root'#'192.168.1.100' IDENTIFIED BY '***';
Also, make sure that mysql is listening to in the right port, if using linux:
netstat -tlpn
should do the work, at least that's how i fixed similar problems.
I was facing the same error for a few hours now.
Turns out I had given the wrong port in my code.
My port to connect was the default port 3306 I was using 3327.
Hope it helps someone maybe.

Logging with a user in a remote MySQL database

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

Connecting to remote MySQL server using PHP

I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());
My problem is that I am unable to connect locally, receiving the error:
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
This is not the case when I upload the same PHP file to the server. I am able to query the database with no problems at all.
I am unable to connect via command line either, but I can access cPanel which rules out the chance of my IP being banned accidentally.
My local server is running PHP 5.2.9, the remote server 5.2.12
firewall of the server must be set-up to enable incomming connections on port 3306
you must have a user in MySQL who is allowed to connect from % (any host) (see manual for details)
The current problem is the first one, but right after you resolve it you will likely get the second one.
It is very easy to connect remote MySQL Server Using PHP, what you have to do is:
Create a MySQL User in remote server.
Give Full privilege to the User.
Connect to the Server using PHP Code (Sample Given Below)
$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306
I just solved this kind of a problem.
What I've learned is:
you'll have to edit the my.cnf and set the bind-address = your.mysql.server.address under [mysqld]
comment out skip-networking field
restart mysqld
check if it's running
mysql -u root -h your.mysql.server.address –p
create a user (usr or anything) with % as domain and grant her access to the database in question.
mysql> CREATE USER 'usr'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON testDb.* TO 'monty'#'%' WITH GRANT OPTION;
open firewall for port 3306 (you can use iptables. make sure to open port for eithe reveryone, or if you're in tight securety, then only allow the client address)
restart firewall/iptables
you should be able to now connect mysql server form your client server php script.
This maybe not the answer to poster's question.But this may helpful to people whose face same situation with me:
The client have two network cards,a wireless one and a normal one.
The ping to server can be succeed.However telnet serverAddress 3306 would fail.
And would complain
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
when try to connect to server.So I forbidden the normal network adapters.
And tried telnet serverAddress 3306 it works.And then it work when connect to MySQL server.

Categories