Create A New Database From Another Database

Allie Syadiqin :

I am using CodeIgniter 3 and MySQL 5.7 and I need to find a way to create a new database on-the-fly for every new client that signs up for an account.

So basically, I have a skeleton or source database called db_source which contains all the tables and some initial data. I need to be able to create a new database, eg. db_client_xxx, every time a new user signs up for an account. I would prefer to create the new database from the source database if possible.

If anyone needs more information, please let me know. Thanks.

Regards, Allie

Alex7 :

A question similar to what you need has been answered here: how to import .sql file in mysql database using php

You can dump the db_source into a file. The correct answers explains how to run import a dump into an existing database. What you have to add to that script is to create database before that.

Please keep in mind that the database creation may take some time and may not be a very good experience for the user.

If I may suggest you another option is to create in advance the databases, e.g.: database_1, database_2 etc. and when a new user creates an account assign one database that is not used to that account. This may be easier and faster for the user. You can create a cron job to keep creating database so you always have a number of X databases available. This approach also may be easier because you can use shell instead of PHP to dump and create new databases.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=346621&siteId=1
Recommended