Connect external database in php - google-app-engine

I am beginner in php as well Google-app engine. I have created a php webpage, I just need this to connect to external MySQL database to log user visit stats.
Under stats.php how can I include("config.php"); also in config.php how to connect:
<?php
// change these variables
$host=("example.com"); //host
$uname="abc";//MySQL username
$pass="Abc#123";//MySQL password
$db="stats"; //MySQL Database
//don't need to change
$con = mysql_connect($host,$uname,$pass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db, $con);
?>
When I run stats.php I am getting below error on appspot.com
Failed to connect to MySQL: Unable to find the socket transport "tcp" - did you forget to enable it when you configured PHP?
I have also confirmed connectivity to my server from outside host, port 3306 is also opened.
$ mysql -u webadmin –h (server ip) –p
I am not getting proper way to use fetch url, please help.
Thanks.

Unfortunately this won't work (yet). The reason is that the App Engine PHP runtime doesn't currently support sockets. Other App Engine runtimes (Python/Java/Go) support sockets, so it's a safe bet that it'll be added to PHP at some point in the future.

$host="example.com"; // should contain a valid host name or an ip address of the mysql server to which you want to connect
Nothing else needs to be changed, username and password only if required different. You have to make sure that the remote database does allow external connections for that username. Besides, you'd be better off using mysqli_* instead of old deprecated mysql_* extension for MySQL.
$link = mysqli_connect($host,$uname,$pass,$db) or die("Error " . mysqli_error($link));
And under stats.php you can simply include any code file the way you already mentioned:
<?php
include("config.php");
?>

$con = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('sandsbtob',$con) or die ("could not open db".mysql_error());
and also firewall of the server must be set-up to enable incomming connections on port 3306
Hope this will help you

Related

Database connection is showing

When I try to connect my database with "dbconnect.php", it is showing like this:
could not connect: Access denied for user 'zigmasta_ajas'#'127.0.0.1' (using password: YES)
Why does this happen? I don't know much about sql and php. My website is hosted. The host provides phpmyadmin and mysql database.I have created a database and a user using mysql wizard. I think all are going well. Then why does this error happen? Please help me in detail.
The following is php code I used. please check the code. Does this code contain errors?
<?php
$user_name="zigmasta_ajas";
$password="password";
$server="127.0.0.1";
$database="zigmasta_dbtest";
$db_handle=mysql_connect($server,$user_name,
$password);
if(!$db_handle)
{
die('could not connect:'.mysql_error());
}
echo'connected successfully';
$db_found=mysql_select_db($database);
if($db_found)
{
echo"database found";
}
else{
echo"database not found";
}
mysql_close($db_handle);
?>

Allow remote mysql access on linux (through webmin or shell)

Spec:
Ubuntu 14.04
webmin/virtualmin 1.791
I am using following code to test remote mysql database connection:
<?php
$db_host = "123.456.789";
$db_name = "database";
$db_user = "user";
$db_pass = "password";
$db_table_prefix = "prefix_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
GLOBAL $mysqli;
if(mysqli_connect_errno()) {
echo "Conn Error = " . mysqli_connect_error();
exit();
}
?>
I keep getting this error:
No connection could be made because the target machine actively
refused it.
Research shows this means the server is "not listening". Before I ran the above script I've already tried to allow remote mysql access through webmin gui. What I did is editting "database manage->host permissions" and make it as follows:
This was supposed to allow remote mysql access but it doesn't work. Also I read from somewhere else that to allow remote mysql access I need to edit /etc/mysql/my.cnf; I have thought that after I edit the "host permissions" in webmin this file would be changed, but it was not. On the other hand, I couldn't find the lines I was supposed to edit in my.cnf, so I am stuck here.
Any help is appreciated.
You can do this via webmin too,
Create your user account for remote access
Webmin > Servers > MySQL Database Server > User permissions
Allow the MySQL server to listen to remote requests
Webmin > Servers > MySQL Database Server > MySQL Server Configuration
MySQL server listening address - set it to any
Restart MySQL using service mysql restart or directly from webmin.
Allowing MySQL to listen to any port is not a good idea , unless you are the only one who can access that network ,
Don't leave it like this afterwards , Its better to Allow certain hosts on certain domains , like your IP , Or simply just dont listen on all ports if its not required i.e when you are finished with your session.
You can also do this via /etc/mysql/my.cnf
Just add a binding adress of your choice instead of localhost
I have got it to work, however not through webmin at all.
First I need to comment out the following line in /etc/mysql/my.cnf:
#bind-address = 127.0.0.1
I guess instead of simply commenting it out, I can also change 127.0.0.1 to my local IP address. Many google results stop here, but this is not enough. The next step is to grant the local user privileges: On remote server, I need to run the following commands:
$ mysql -u root -p
Enter password:
mysql> use mysql
mysql> GRANT ALL ON . to user#'localIP' IDENTIFIED BY
'password';
mysql> FLUSH PRIVILEGES;
Actually I have seen this when I was doing google search before I asked the question here, but I just ignored it because I thought I've done it. It turns out I not only need to grant privileges on server side but also need to do it for "local user".
Feel free to comment here if there's still something I missed out or you know how to do it throught webmin(I am still wondering what editting "host permissions" in webmin does).

How can i find the Hostname of Remote database in Toad

i am trying to connect to the remote database.With given username and password ,i am able to connect in toad.but in the code while handling connection pooling i am getting null connection.so i want to recheck the hostname for this particular DB?how can i check it in Toad?
String url = "jdbc:oracle:thin:#hostname:dbname";
connection = DriverManager.getConnection(url,"username","password");
System.out.println("got the connection"+connection);
i tried connecting using the credentials which i am havving but i getting null connection.i want to recheck the hostname..any idea how can i check the hostname in toad for remote Database?
You may try this:
SELECT host_name FROM v$instance

laravel 4 local developing but I want to use a remote database

I have set up xampp server where I am developing a laravel app:
Every day (when I make an update to my app) I have to export and import the SQL from the database. That takes a lot of time if I have worked on 10 apps that day :(
Is there a way I can use the online database with a local test version of laravel?
I am using shared hosting and not migrations. I insert tables myself.
Online I see a tab:
access hosts: 'localhost'
I can add a host, but what name should I add and how can I get laravel to access this host?
Could it be 'subdomain.mywebsitename.nl/hostname'?
I'm too scared my app will crash if I change it :s
This i tried this after the solution i got:
I filled in a host. This is the same url i fill in my adress bar.
'host' => 'http://safetyanalyse.nl:2222/',
'host' => 'http://safetyanalyse.nl',
Both options do not work :( Do i have to put the port number somewhere else?
I also have this error:
SQLSTATE[HY000] [1130] Host 'D97A1650.cm-3-3a.dynamic.ziggo.nl' is not allowed to connect to this MySQL server
Step 2 (next i tried this).
In the error it says something about the host, i tried filling in this host.
'host' => 'D97A1650.cm-3-3a.dynamic.ziggo.nl',
Now i get the error:
SQLSTATE[HY000] [1045] Access denied for user 'safety_cert-user'#'d97a1650.cm-3-3a.dynamic.ziggo.nl' (using password: YES)
Try to use domain name as host, that should work.
Also, look for port, if specified.
All this information should be provided to you is hosting control panel. If not, I would change hosting provider.
Edit
Define your host without protocol.
In your app/config/database.phpset
'host' => 'safetyanalyse.nl',

How do I catch a connection failure to a nonexistant Rose::DB database?

My Perl application uses Rose::DB, Rose::DB::Object (ORM) and Tk on Windows XP. I need to check whether the database connection information (host, port, database name, user, password) leads to a valid connection. If I call $db->connect and use e.g. a nonexistant host for testing, Rose::DB says:
DBI connect('dbname=my_db;host=192.168.70.85;port=5432','postgres',...) failed: could not connect to server: No route to host (0x00002751/10065)
How do I catch this and create a new message for my application? Is this a question about try/catch in general?
Thank you for your help!
A solution that seems to work so far was posted to me from John:
my $ok = eval { $db->connect };
if(!$ok || $#) { # Connection failed }

Resources