Solved

Two domains using the same MySql database?

Posted on 2011-02-19
8
408 Views
Last Modified: 2012-05-11
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
Comment
Question by:Erie_Laker
8 Comments
 
LVL 4

Accepted Solution

by:
andreizz earned 72 total points
Comment Utility
It depends on your host, normally it would be possible.
0
 
LVL 2

Assisted Solution

by:PhSyChoTiK
PhSyChoTiK earned 72 total points
Comment Utility
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
 
LVL 5

Assisted Solution

by:onemadeye
onemadeye earned 71 total points
Comment Utility
I think I once tried it works successfully on a Linux Hosting with CPanel.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 71 total points
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Erie_Laker
Comment Utility
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
 
LVL 11

Assisted Solution

by:VanHackman
VanHackman earned 71 total points
Comment Utility
>> 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
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 71 total points
Comment Utility
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
 
LVL 5

Assisted Solution

by:RizyDeWino
RizyDeWino earned 72 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now