Connecting mysql remotely via free host - php

I registered on 000webhost and I can connect to mysql db pages that are uploaded there via:
mysql_connect('localhost', 'user', 'pass') or die('Could not connect to database');
mysql_select_db('name') or die('Could not select database');
But it seems it does not allow connecting remotely.
Is there any free host which has mysql db and allows remote connections ?

try heliohost.orgI used it sometime back and they got good service as well.The only problem is the registration get filled too quickly

000webhost does not provide MySQL remote access for free accounts,
have a look, so either go for an upgradation
Or try http://www.freemysql.net for remote free mysql server, But again its free hence very slow,
mysql_connect('SQL**.FREEMYSQL.NET', 'USER', 'PASS') or die('Could not connect to database');
mysql_select_db('DBNAME') or die('Could not select database');
Do not use "localhost"

Related

Connect database from online PHP site hosted in Heroku

I created a php website and host it online by using Heroku recently. However, I couldn't connect my database because I use localhost phpmyadmin database from my online site. How should I connect my localhost database from my online site? Or how to make my host my database online, and then make the connection? Here is my db.php file to make the connection with mysqli, in case if there's any need to tweak the db connection PHP codes.
//step 1: Establish database connection
DEFINE("DB_HOST", 'localhost');
DEFINE("DB_USER", 'root');
DEFINE("DB_PASS", '');
DEFINE("DB_NAME", 'my_db_name');
// Create connection
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Set charset to UFT8
mysqli_set_charset($con, "utf8");
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
If you are hosting your database on your local machine, whilst having a webserver that needs to connect to your database, you will have to make sure your database can be accessed by another machine. The host is only ‘localhost’ if the Local IP is identical for the machine and database. Your db.php is correct and will function properly, as long as you ensure that the connection details are correct, since localhost is not correct there will be errors.
You might want to look into how to portforward depending on what database software/installation you are using. Or look into whether your host offers database solutions sch as or similar to PhpMyAdmin (in case of most webhosts).

How to remotely connect to OpenShift MySQL database using PHP?

I've a Tomcat 7 application running on Openshift server, say myapp.
I've also installed MySQL 5.5 cartridge and got the username and password say myUsername and myPassword respectively.
I tried to remotely connect to the database by writing a simple PHP script from my php localhost.
<?php
$link = mysql_connect('127.8.217.2:3306', 'myUsername', 'myPassword');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('myapp',$link) or die ("could not open db".mysql_error());
but the output shows
Could not connect: Access denied for user 'myUsername'#'localhost' (using password: YES)
I don't know what i am missing.
Now the question part.
1) Is it possible to remotely connect to the OpenShift MySQL database using PHP from either localhost or some other server ? If yes, How ?
If there's any question, shoot it into the comments.
EDIT
I've looked at port-forwarding with the help of this thread. but that's not i want.
Based on your explanation as having a need for a both Java and PHP application needing to connect to same server, your options are either
1) doing port forwarding or connecting through SSH, both explained in this link I offered as a duplicate of this, or
2) Trying something explained here and here to create a shared database using a scalable application - however, people in other similar threads, like this one, have claimed that it is "for Openshift/Redhat internet environments, not for regular customers of Openshift" (source). Don't know if it is so, but at least if you go this way, you should test from another openshift gear, not from local computer.

Cannot connect to MySQL database, socket error

I'm new to php, and while trying to make a connection on one of my pages to the database I set up on the phpMyAdmin page of my site.
I get this error:
"Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)"
I don't know what a socket is, or why it's trying to go to what looks like a temp file, so I don't even know where to being to troubleshoot this.
The code I'm using to make the initial connection is this:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
I know I should have access to the MySQL databases, since I logged on and made them myself.
I previously set the page up by using WAMP on my computer, and everything worked fine.
It's just when I tried making it live on the site that I ran into this error.
Any help would be awesome!!
Try using 127.0.0.1 instead of localhost.
If that does not solve it, and you have root access to your server, try the following command
service mysql restart
To restart the mysql server.
The first option will probably work. Again, if you have root access to your server, you should change the mysql config to support sockets, since it's better than the TCP-ip connection.
After contacting my hosting service several times, it seems the error occurred because my hosting service recently changed their specifications and now uses "mysql" instead of "localhost" in the host and server fields. I had used "localhost" before with another host service, so I didn't think to change this, and the latest help articles on my host's website had not updated to reflect this.

Why php PDO uses a different from hostname to the one used by mysql_connect() when connecting to a remotely hosted mysql database?

I've mysql database and php/apache on two different servers: let's say hostphp.domain.com and hostmysql.domain.com.
On the mysql server I've set a user "my_user" with permissions to connect to "my_database" db from the specific host "hostphp.domain.com".
When I connect to it using mysql_connect it does right. But when I do it via php PDO I get this error:
SQLSTATE[42000] [1044] Access denied for user 'my_user'#'%' to database 'my_database'
I've done some tests and I found the problem is ...#'%', mysql is refusing that connection because "my_user" does not have permission to connect from any host.
Also I've tried to connect using mysql_connect with a wrong password to see the error and I get this:
Could not connect: Access denied for user 'my_user'#'hostphp.domain.com' (using password: YES).
The difference is in ..#'%' and ...#'hostphp.domain.com'.
So that's my question, why php pdo do not declare hostname when connecting to a remote host? (or is doing that wrong).
Thanks and sorry for my english.
Edit.
Some code example, this does not work:
try {
$pdo = new PDO(
'mysql:host=hostmysql.domain.com;port=3306;dbname=my_database',
'my_user',
'my_pass'
);
} catch (PDOException $e) {
die($e->getMessage());
}
but this works ok:
$conn = mysql_connect('hostmysql.domain.com', 'my_user', 'my_pass');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
You have successfully connected to the host using mysql_connect, but you got no errors because you did not attempt to select the database.
Your user probably doesn't have access to your database.
Try running mysql_select_db("my_database"); after connected to the host and you should get the same error.

PHP - connecting to mysql database from different server

I'm trying to connect to a mysql database from a different server to the one the database is hosted on but I'm getting an error.
I'm guessing it might be something to do with remote permissions. Is this something I can change or do I need to get my hosting company to do this?
<?php
$con = mysql_connect("mysql4-remote.hosting.net","myadmin","password123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>
Can't connect to MySQL server on 'mysql4-remote.hosting.net' (10060)PHP Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'mysql4-remote.hosting.net' (10060) in D:\websites\abc123\www\test.php on line 2
I would say, that your login credentials or Network settings are wrong:
See this post for further help:
http://wiki.answers.com/Q/What_does_MySQL_error_number_10060_mean
If you don't have access from outside use tunneling, here is an example:
$connection = ssh2_connect('SERVER IP', 22);
ssh2_auth_password($connection, 'username', 'password');
$tunnel = ssh2_tunnel($connection, 'DESTINATION IP', 3307);
$db = new mysqli_connect('127.0.0.1', 'DB_USERNAME', 'DB_PASSWORD',
'dbname', 3307, $tunnel)
or die ('Fail: '.mysql_error());
You have to give the permission to remote user by inserting the particular remote machine IP or wildcards in USER table in mysql.
This link will help you. Click Here

Categories