Unable to connect mysql and php in Lamp on online server - php

I am using XAMPP on my computer. My website is running fine on it. Now I uploaded my files to server using scp. Now I tried to change my connection parameter. I change it
<?php
$host ="104.236.227.173";
$username = "****";
$password = "******";
$database = "****";
#session_start();
?>
The host is the server on which my website is running.
Mysite
But online I am unable to connect to database.
I installed Lamp online.
I have changed settings in my.conf. But it does not work.
I have used the following command too
GRANT ALL ON `database`.* TO username#'%' IDENTIFIED BY 'password';
Please help me to solve this.

As shared using 'localhost' instead the IP address should do the trick. You're accessing the mysql on the same server as the php script is running. Unless the mysql server is configured to be available via IP address, 'localhost' is default behaviour. Glad it helped!

Related

Can I use my mysql database on one host and my server files on another?

I have rented a hosting provider which gives me MySQL database. I would like to locally connect to that database using PHP. Please see below $dbServer property.
$dbServer = "localhost"; // I'm talking about this one
$dbUsername = "username";
$dbPassword = "blabla";
$dbName = "db_name";
How I could connect to that MySQL? What kind of URL should I put?
Let's say my domain from hosting company is - www.randomdomain.tld
Can I somehow change localhost to an actual host link so it would work on any website?
Maybe. You would have to ensure all of these are true:
MySQL is configured to listen on a network interface you can route to from the other computer. If it only listens on localhost (with the loopback network interface or a UNIX socket) then it won't be accessible.
The network port it listens to must not be firewalled so that the other computer cannot access it.
The credentials used to connect to the database must allow access from the computer you are connecting from. (It is not usually to allow access to dbuser#localhost).
And if yes, where I should get that information?
localhost just means "this computer".
You know a hostname you can access it from because you typed it into your browser's address bar in order to access PHPMyAdmin over HTTP.
Yes, you can do that.
In CPanel there is an option called Remote MySQL. Your hosting provider must allow this option. You can find the documentation here

MySQL connect failed. Can't connect to MySQL server on 'http' (4)

I try to connect my android application using JSON Parser to the web hosting. But whenever I try to connect (even just open using url in the browser) I will get the error message.
<?php
$dbHost = 'http://sql4.000webhost.com/localhost';
$dbUser = 'a6410240_cbetTD';
$dbPass = 'xxxxxx';
$dbName = 'a6410240_cbetTD';
$conn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName,$conn);
?>
This is my database.php file. The full error message is
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'http' (4) in /home/a6410240/public_html/database.php on line 8.
I have tried change the $conn but still it didn't worked for me.
Thanks
If your database and application is on same server then use "locahost" in $dbhost.
And if your database and application is on different servers then you need to use IP address or hostname in $dbhost and the database user should be added on database server provided with required privileges.
The problem you are having was already mentioned in one of the comments, this one to be precise.
For your solution to work, all you need to do is omit the part http:// at the beginning and probably /localhost at the end.
The host is only the domain you are referring to. In this case sql4.000webhost.com. With /localhost you tried to already connect to a database, although your configured database is supposed to be a6410240_cbetTD.
MySQL use TCP port 3306 by default
($dbport) and hostname or IP address ($dbhost). For LAMP (Linux-Apache-MySQL-php) you can find a lot of tutorials.
Usually MySQL server listens internal port (which can't be reached via Internet) for security purposes.
If you familiar with docker, you can simply download examples of LAMP solutions from hub.docker.com.

Connect to MongoDB on Appfog from localhost

I'm trying to run my application on my local pc. Everything looks perfectly fine on the cloud, it works and connects to MongoDB with the code below:
$services_json = json_decode(getenv("VCAP_SERVICES"),true);
$mongo_config = $services_json["mongodb-1.8"][0]["credentials"];
// Generally will be localhost if you're querying from the machine that Mongo is installed on
$config['mongo_host'] = $mongo_config["hostname"];
// Generally will be 27017 unless you've configured Mongo otherwise
$config['mongo_port'] = $mongo_config["port"];
// The database you want to work from (required)
$config['mongo_db'] = $mongo_config["db"];
// Leave blank if Mongo is not running in auth mode
$config['mongo_user'] = $mongo_config["username"];
$config['mongo_pass'] = $mongo_config["password"];
But since I cannot get the vcap_services from my local pc, I'm using constant values for the connection to connect my app to the remote mongo server on appfog:
// Generally will be localhost if you're querying from the machine that Mongo is installed on
$config['mongo_host'] = "***";
// Generally will be 27017 unless you've configured Mongo otherwise
$config['mongo_port'] = "***";
// The database you want to work from (required)
$config['mongo_db'] = "db";
// Leave blank if Mongo is not running in auth mode
$config['mongo_user'] = "***";
$config['mongo_pass'] = "***";
But when I try to execute the app through http://localhost/my-app, it gave me this error:
Unable to connect to MongoDB: Failed to connect to: ***: Connection timed out
What can be the problem?
* values are deleted for the privacy.
Did you try connecting without the VCAP variables not on localhost but the actual server? AppFog may not give authorization to connect without VCAP variables or something may be wrong on the values you manually get.
I believe this is an authorization problem. Try another cloud service if you cannot handle with it. This is not the only solution.

PHP & MySQL: Remote Access to MySQL servers?

Following the post on Connecting mysql remotely via free host,
the remote access still fails.
My PHP script is stored in Xtreemhost and intends to access the MySQL database on FreeMySQL.net. If FreeMySQL.net supports remote accesss to MySQL databases (which is true), there should be no problem with my PHP script run on another free web hosting server.
Can anyone tell me what is wrong with my remote access to my MySQL database?
Thank you.
<?php
// $server = "example.com";
$server = "sql.example.com";
$username = "tinfanide";
$password = "MyPassword";
$database_name = "tinfanide";
$connect = mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database_name,$connect);
mysql_close($connect);
?>
The crux of the problem of mine is that I thought I had a SQL server which allows external connection but actually not. Now I've applied for a SQL server that supports the feature and the problem with the PHP script is resolved.

PHP Localhost Application connect to server database

I want to ask about how to connect my localhost application (C:xampp/htdocs/myproject) to database at my server host (www.someweb.somedomain)?
Am I possible to do that? If it is, how to connect it at my php config? Right now my config (server.php) is:
<?php
$host = "my web IP public:3306";
$user = "root";
$pass = "";
$db = "dispatcherDB";
$conn = mysql_connect($host, $user, $pass) or die ("Cant connect to mySQL");
mysql_select_db($db);
?>
what I got:
Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\XMS\server.php on line 7
So, what must I filled for $host? I'm trying using website IP, it still can't connect. Maybe there's someone here have experience at this problem?
Sorry for my bad English
If you have cPanel access to the remote server then you need to mention that from which ip addresses it should allow access to MySQL..
In cPanel you will get Remote MySQL under heading Databases:
Clicking Remote MySQL will give you the option to add hosts from where you want to allow connections to your MySQL server:
Also, you can check localhost without specifying port number as value of $host..
Probably Mysql server is not allowed root access from remote servers.
you could check this post

Categories