I can't connect Joomla! remotely with MySQL - php

I have two hosting (1: IP 192.186.234.192 and 2: IP 74.116.84.169), and I have Joomla website files on hosting 1, Joomla mysql database on hosting 2, both hosting 1 and 2 have Cpanel administration.
I want run Joomla website on host1 by linked it remotely to mysql database on host 2
Steps that I made:
In hosting 2 added IP address for hosting 1 in (Remote MySQL).
In Joomla configuration.php file I have changed database section like:
var $host = '74.116.84.169';
var $user = 'artjour';
var $password = 'abc#123';
var $db = 'apuser';
I also have changed the host name by server name like:
var $host = 'servername.mydomainname.com';
But unfortunately:(, Could not connect to MySQL, please it's very important for me to know what's my wrong here.
Thanks in advance.

Use a small PHP script like this to see the error other then "Could not connect to MySQL":
<?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();
}
?>

in the hope that this may help someone I resolved the same situation.
I could access from server A and from command line mysql but I also could not connect my remote joomla database, the httpd server side needed selinux setsebool httpd_can_network_connect_db=1 to enable apache to connect to the remote database.

You probably have to allow remote SQL connection on server2 with Cpanel :
http://www.liquidweb.com/kb/enable-remote-mysql-connections-in-cpanel/

Related

How to connect to database on freewebhostingarea?

Ok so i am making account system for my website. I am using freewebhostingarea as host. But i dont know what is servername(for connecting to database). Can you help me? Here is my code for connecting:
<?php
$servername = "";
$server = "root";
$password = "my password";
$dbName = "name";
$conn = mysqli_connect($servername, $server, $password, $dbName);
?>
I dont know what is $servername for freewebhoatingarea. Pls reply.
It's all the same actually . I want you to try this 4 steps
After you hit create databaase it will give you an option to name/rename your database so save that name to something like a notepad for your reference
(e.g) freehosting_db_(sampledatabase)
Next to that it that before you hit next on the creation of database kindly remember that password that you are going to make and like the name of the website save it to a notepad so you can remember the password
(e.g) xHsnaAXa123 -> password database
Mostly the server always localhost on SOME freehosting
The servername is automatically generated so look for it. It will be something like
(e.g) freehosting_db_(sampledatabase)_user
And to set that up
$servername = "freehosting_db_(sampledatabase)_user";
$server = "localhost";
$password = "xHsnaAXa123";
$dbName = "freehosting_db_(sampledatabase)";
PS: This is mostly how you setup a connection from your database to your server . But it is always depends on the the hosting that you are going to use . Godadday, Hostgator, 000webhostingapp, they are all alike on how setting it up .

host is not allowed to connect to this server

quick mysql question.
I'm new at php/mysql and followed a tutorial(php/mysql for dummies) so I don't really know what I did wrong or if the tutorial is wrong.
I have a file, "database_connections.inc", that looks like this:
<?php
$user = "username";
$host = "host";
$password = "password";
$database = "database";
?>
With the actual credentials not included for obvious reasons.
Then in another file, login.php, I have:
include("database_connections.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die("Query died: Couldnt connect to server.");
I get an error message with the "or die" text, accompanied by a warning:
host xxxxx.000webhost.com is not allowed to connect to this mysql server in....
Why not? I'm sure my credentials are all correct.
I've read in a few places to run some shell statements...but can't really do that, I'm on Windows.
I'm using phpMyAdmin, so hopefully I can do something from there?
Open "database_connections.inc" and change it to look like this:
<?php
$user = "root";
$host = "localhost";
$password = "";
$database = "test";
?>
MySQL is by default configured to work with localhost (or 127.0.0.1), in order to allow "host xxxxx.000webhost.com" as host, open phpMyAdmin and select "SQL" and execute this query;
GRANT ALL ON your_database_name.* TO your_user#your_host_xx.xxx.xx.xx IDENTIFIED BY 'your_password';
Go into PHPMyAdmin, and edit your user.
Under Login Information, there should be an option for "Host"- try adding xxxxx.000webhost.com.

Can't access database on my server

I am having some problems with my database. I created a user, and a database, but I can't seem to access it with php.
My code is:
$host = 'anapaiva.pt:2082';
$user = 'anapaiva_p1';
$pass = 'xxxx';
$db = 'anapaiva_mcmm1';
#mysql_connect($host, $user, $pass) or die('err: '.mysql_error());
#mysql_select_db($db) or die('err: '.mysql_error());
And on the webpage, appears the following errors:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'anapaiva_p1'#'apollo' (using password: YES) in /home/anapaiva/public_html/mcmm/connection/dbconn.php on line 6
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/anapaiva/public_html/mcmm/connection/dbconn.php on line 7
The same code was working with my other server, hungergamesportugal.com, but I can't seem to upload the sql file to that database (it says I don't have permissions... I don't know why), and on this server it allows me to import the sql file, but I can't seem to access the database.
Can someone please help? :/
You're using the wrong port. 2082 is for cPanel. 3306 is for MySQL (usually).
Generally, you can omit the port entirely:
$host = 'anapaiva.pt';
You also need to assign the connect function to a variable:
$dh = mysql_connect($host, $user, $pass);
So that you can close the connection when you're done with it:
mysql_close($dh);
You can allow permission using cpanel, add permission to remote mysql. I don't remember exact name, but it was something like, remote database. on clicking you can add ip address to allow database permission. This may help you. search remote database at cpanel. May be this help you, http://forums.cpanel.net/f354/enable-remote-access-customer-database-251951.html
Test same user on localhost
$mysql_host = "localhost";
$mysql_database = "db_name";
$mysql_user = "root";
$mysql_password = "password";
$con = mysql_connect($mysql_host,$mysql_user, $mysql_password);
mysql_select_db($mysql_database, $con);
OR
Check user privileges
Using GRANT statement check whether user has rights.
Reference: http://dev.mysql.com/doc/refman/5.0/en/show-grants.html

making connection code dynamic

the connection to database via PHP is as:
<?php
$DbHost = 'localhost';
$DbName = 'root';
$DbUser = 'root';
$DbPwd = '';
$dbConn = mysql_connect ($DbHost, $DbUser, $DbPwd)
or die ('MySQL connect failed. ' .mysql_error());
mysql_select_db($DbName,$dbConn) or die('Cannot select database. ' . mysql_error());
?>
Here I've hardcoded my hostname, username and password. Everytime I take my project to another machine, I've to change it.
How can I make this connection code dynamic?
You can put machine specific information in a settings.php file and use
require_once "settings.php";
to get hold of the machine settings. Do this on all your machines and put the file in a specific place so your project(s) is/are able to find it.
When you move your project to another machine, it will use the corresponding settings.php file. (Assuming you are not copying this file as well ;) )

Can't get my PHP document to connect to my xampp server

I am very new to php, and am sorry if this question turns out to be too vague, but if there is any other information that you need from me let me know.
Anyway, basically what my problem is I have some simple php code that should call die() if it can't connect to the xampp server I have set up, however, even if I put in invalid info for the server or user it prints Successful. I really don't know where to go with this, so if anyone has any suggestions that would be great.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
if ( !mysql_connect($dbhost, $dbuser, $dbpass) )
{
die(mysql_error());
}
else
echo 'Succesful';
?>
Ok for some reason it was just not working with the root user, so I created a new user who required a password and apparently that connected like it was supposed to. Thanks everyone for the answers.
Your code is correct pistolpete333. For your root user issue, that your not able to connect with root user you can check below file for your phpmyadmin configuration
C:\wamp\apps\phpmyadmin3.5.1\config.inc.php.
In above file you can check your host, username and password of phpmyadmin.
I have found that when trying to connect php to an xampp server there are usually only a few things that could cause the issue you are having.
1. mySQL service is not running
2. no specific database is selected to try to access
3. user does not exist in phpmyadmin.
When you pick a user to connect php to mySQL with you should always make sure that a password is set, that is usually the thing people miss. You can use root if you create another instance of it in the user list and require a password, or you can add a password to the root user already in the list, but the best option is to create a new custom user that only has access to the database you want to access with your php and make sure it has a password required. (make sure any user you use can connect with localhost; root can by default and I think most newly created users can as well).
Below is the php I use for websites I design that need php to access a DB.
<?php
// This configures the variables for database access
$dbhost = 'localhost';
$dbuser = '????';
$dbpass = '????';
$dbname = '????';
// This will connect to the server and then the correct database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
This is working because you have specified valid username, host and password to mysql. If you specify wrong password, you get mysql error. This code works and connects to the mysql server though you have not selected any database.
Try this code instead
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link); //foo is your database name
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>

Categories