• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

Two domains using the same MySql database?

We have one website up and running in shared hosting environment and recently we obtained second domain name (for the site with completely different content). Both sites will be hosted by the same hosting company. Now, the second domain is what they call "addon domain" - as far as I understand, it will be set up inside public_html folder of the first site, but will have completely separate files, databases etc.

Is it possible for these two sites to somehow use the same database?  The new site needs to use some data from MySql database associated with the existing site.

Thanks.  
0
Erie_Laker
Asked:
Erie_Laker
7 Solutions
 
andreizzCommented:
It depends on your host, normally it would be possible.
0
 
PhSyChoTiKCommented:
Yes it is possible, unless there is a restriction imposed on the MySQL account that prohibits access from other domains aside from the one it was set up for (and that isn't normally the case). And in that case, you can always create the access scripts on the first domain and poll them from the second.
0
 
onemadeyeCommented:
I think I once tried it works successfully on a Linux Hosting with CPanel.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Ray PaseurCommented:
In my experience with addon domains, where the root directory contains more than one web site, I have been able to share data bases.  You might run this script on both and see if it works.
<?php // RAY_mysql_example.php
error_reporting(E_ALL);


// IMPORTANT PAGES FROM THE MANUALS
// MAN PAGE: http://php.net/manual/en/ref.mysql.php
// MAN PAGE: http://php.net/manual/en/mysql.installation.php
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php


// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES

Open in new window

0
 
Erie_LakerAuthor Commented:
Thank you all.  Ray, I will try your script as soon as the new website becomes active (propagates) and will post results back here.
0
 
VanHackmanCommented:
>> Two domains using the same MySql database?

Yes, of course, it's completely possible!!. there is not reason for the opposite.

You should understand that under a shared hosting environment, even if you have 2, 3 or and X number of domains, all of them still being "physically" hosted on the same server, so... from your sites' point of view all of them will connect to the "localhost" and because of that, will be able to use any DB for which the DB user that you have specified in your connection script, have the permmision to use.

It's something completely different when you try to use DBs that are in a different "physicall" servers, because even when it still being possible there is some little differences in the method to use them.

So, the real question here is... Would your sites be able to share a DB without screw up the DB coherence?

Thats something that you should evaluate from your code...

Hope this helps you.

All the best,
VanHackman

0
 
Dave BaldwinFixer of ProblemsCommented:
Hosting that puts their databases on other servers seem to often have restrictions on how you can access them.  If you are hosted on Godaddy, you have a choice when you set up a database to allow or prevent external access.  If you allow it then other sites can use that database.  If you don't allow it, then only the original site will be allowed to connect to it.

Hostek appears to have something similar but I don't know the details because it was a client's account, not mine.  All of their databases are on their internal network and only some can be accessed externally.  1and1 wouldn't let me access a database externally but I wasn't sure about access from multiple sites there.
0
 
RizyDeWinoCommented:
From you post , it look like you are hosted on cPanel server. And with hosting addon domain means its not only possible, its standard. The physical paths for domains would be :

Main Domain :  /home/CPANMEUSER/public_html/
Addon Domain : /home/CPANELUSER/public_html/addondomain
( though addon can also be setup at /home/CPANELUSER/addondomain )

For the DBs they are created as   CPANELUSER_dbname , for all main and addondomains. So any domain hosted user the same CPANELUSER can access all DB under this user.

Again for cPanel servers its Standard , you will not need to do anything extra.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now