Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-24-2012, 11:46 PM   PM User | #1
hunter1611
New Coder

 
Join Date: Jan 2011
Posts: 26
Thanks: 11
Thanked 0 Times in 0 Posts
hunter1611 is an unknown quantity at this point
Script to Create a MySQL Database

I was hoping someone could point me in the right direction. I've made a PHP script that relies on a MySQL database, and I wanted to also make a script to create the required database for my PHP script for when I give a copy of my script to someone else. (Basically, so they can run the script to create the database, and then be able to use the PHP script that I've given them).

SO, what language/tool do I use to make a database creation script? Are there security conflicts that I'm going to run into? Because the script will be creating a MySQL database on a web hosting account...

Last edited by hunter1611; 12-25-2012 at 04:32 AM..
hunter1611 is offline   Reply With Quote
Old 12-25-2012, 03:00 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Do you want to clone the existing database AND ALL ITS CONTENTS and have the other person install that?

Or do you want to just clone the existing tables but with no data in any tables?

Or some mix of the two?

The first option--some database and all its contents--is actually the easiest. MySQL provides a command line tool that does this:
Code:
mysqldump -uUSERNAME -pPASSWORD nameofdatabase > anyNameYouChoose.sql
Then, from the command line, it just as easy to restore (or re-create) that database:
Code:
mysql -uUSERNAME -pPASSWORD < anyNameYouChoose.sql
Again, these are issued from the command line. From the LINUX command line or from at DOS window command line on Windows. The name of the dump file doesn't have to end in ".sql" but it's traditional to do so.

********

If you want a mix, some tables with data and some without, the easiest way might be to dump the entire database, import (re-create) it on another machine, issue TRUNCATE TABLE xxx commands to clear the data out of the tables that are to be blank, and then re-dump from that second machine.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
hunter1611 (12-26-2012)
Old 12-25-2012, 03:02 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If the web hosting account doesn't have access to the MySQL command line tools, then *probably* you could just use mysqlphpadmin to import the same ".sql" file.

If not it would be trivial to create your own PHP program to do so: It would simply read one line at a time from the ".sql" text file and issue that command to the MySQL DB.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-25-2012, 04:32 AM   PM User | #4
hunter1611
New Coder

 
Join Date: Jan 2011
Posts: 26
Thanks: 11
Thanked 0 Times in 0 Posts
hunter1611 is an unknown quantity at this point
Hmmm...OK. Thanks.
hunter1611 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:38 AM.


Advertisement
Log in to turn off these ads.