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
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.
Related
So I have an old website which was coded over an extended period of time but has been inactive for 3 or so years. I have the full PHP source to the site, but the problem is I do not have a backup of the database any longer. I'm wondering what the best solution to recreating the database would be? It is a large site so manually going through each PHP file and trying to keep track of which tables are referenced is no small task. I've tried googling for the answer but have had no luck. Does anyone know of any tools that are available to help extract this information from the PHP and at least give me the basis of a database skeleton? Otherwise, has anyone ever had to do this? Any tips to help me along and possibly speed up the process? It is a mySQL database I'm trying to use.
The way I would do it:
Write a subset of SQLi or whatever interface was used to access the DB to intercept all DB accesses.
Replace all DB accesses with the dummy version of yours.
The basic idea is to emulate the DB so that the PHP code runs long enough to activate the various DB accesses, which in turn will allow you to analyze the way the DB is built and used.
From within these dummy functions:
print the SQL code used
regenerate just enough dummy results to let the rest of the code run, based on the tables and fields mentioned in the query parameters and the PHP code that retrieves them (you won't learn much from a SELECT *, but you can see what fields the PHP code expects to get from it)
once you have understood enough of the DB structure, recreate the tables and let the original code work on them little by little
have the previous designer flogged to death for not having provided a way to recreate the DB programatically
There are currently two answers based on the information you provided.
1) you can't do this
PHP is a typeless language. you could check you sql statements for finding field and table names. but it will not complete. if there is a select * from table, you can't see the fields. so you need to check there php accesses the fields. maybe by name or by index. you could be happy if this is done by name, because you can extract the name of the fields. finally the data types will missing. also missing: where are is an index on, what are primary keys, constrains etc.
2) easy, yes you can!
because your php is using a modern framework with contains a orm. this created the database for you. a meta information are included in the php classes/design.
just check the manual how to recreate the database.
Reference: Copy Database Sructure of Mysql Database
Here's my problem... I have a site that I use PPC to drive traffic to. I track the visitors' keyword, PPC source, ad versions, etc. Currently I store this data in a MySQL DB (InnoDB) named visits. However, when this PPC campaign is running full throttle it generates a lot of data. Every so often my site crashes because this DB fills up and stops responding. (And because I forget to manually do a copy and empty...)
So now I want to create a PHP or Ruby script that runs once a week/month to put the gathered data into an archive DB and empty the DB used for data collection. I assume the fastest way is to rename the existing DB visits to something with a date stamp in the name like visits_010113_020113 for the month of Jan 2013. Then copy create a new visits with only the structure. The primary key is 32 char hash generated by PHP's md5 function so duplicate keys due to auto-increment is not an issue.
(I chose a DB to store the data in because I'm familiar with DBs and I wanted to be able to parse data for custom reporting. I am open to suggestions of a different architecture but I don't want to be spending the next 3 weeks coding up new classes and such for a new architecture right now.)
I ran a Google search on copying the structure of a DB to a new DB (the first result is the one I referenced above and most of the rest of the first page were very similar). However, the solutions all use mysqldump through the CLI. I want to do everything via PHP or Ruby. I could use an SSH class I have for PHP to execute the CLI but that seems like a hack.
I was hoping there was a simple SQL statement I could pass to do the renaming and copying. My preferred solution would be entirely in PHP. I use PHP 5.3.10-1ubuntu3.6 with Suhosin-Patch, mysql 5.5.29-0ubuntu0.12.04.2, and Ubuntu 12.04 server. I also use PHP's PDO object to interface with MySQL.
Thanks
So this would require you to have a list of the tables that need to be copied, but I like
CREATE TABLE cur_db.tbl_name LIKE old_db.tbl_name
So your script could rename the DB, create the new db, then run this in a loop over your table names.
I'm developing a new version for my web application with some redesigned database structure. However, the old application is still working onine with customers. Is there any solutions for easing this deployment?
Thanks and best regards.
Edited: My question is about how to merge the old database with the new database with new redesigned structure. The old database had many new records when I developed new application with new database.
Just make a new database, and include the version in the name for example. You can have multiple databases on the same server, and even use multiple databases in the same application.
I believe there are two choices;
Either force all users to the new system with a bit of downtime, which as long as your site has some quite time on traffic you can schedule it then.
Alternatively upload both and run concurrently pointing everyone at the new site and give a time-frame to your users for taking down the old site.
These are some steps you can follow.
First you have to get a backup database dump from the your existing database. Eventhough you made some mistake you are in the safe side.
Then you can create a new database using the old dump.
Then you have to figure out what are the changes you did in the structure.
Then you have to map old data to the new tables which are changed using ALTER TABLE commands. For this you can first create necessary new tables using sql commands and then read the old data and insert in to new ones.
If you are using mysql you can use "Transactions" to make sure your changes are persistent. You can refer my blog post to learn more on "Transactions" http://coders-view.blogspot.com/2012/03/how-to-use-mysql-transactions-with-php.html
I have a windows program which generates PGP forms which will be filled in later.
Those PHP forms will populate a database. It looks very much like MySql, but I can't be certain, so let's call it ODBC.
And, yes, it does have to be a windows program.
There will also be PHP forms which query the database - examine which tables and fields it contains and then generates forms which can be used to search the database (e.g, it finds a table with fields "employee_name", etc and generates a form which lets you search based on employee name.
Let's call that design time and run time.
At design time, some manager or IT guy or similar gets to define the nature of the database and at runtime 1) a worker fills in the form daily and 2) management can extract reports.
Here's my question: given that the database is defined at "design time" (and populated at run time), where and how is best to do so?
1 I could use an ODBC interface from the windows program, but I am having difficulty finding something good to work with Delphi. Things like ADO and firebird tend to expect you to already have a database and allow you to manipulate it, but I can find no code example of how to create a database and some tables, so ...
2 I could used DOS commands from Delphi in my windows program. I just tried and got a response to MySql --version, but am not sure if MySql etc are more interactive. That is, can I use a script file or a very long stacked command with semicolons and returns separating? e.g 'CREATE DATABASE db; CREATE TABLE t1;'
3) Since the best way to work with databases seems to be PHP, perhaps my windows program could spit out a PHP page which would, when run in a browser, create the database.
I have tried to make this as uncomplicated as I can, but please feel free to ask questions. It may be that there are several valid ways, but there is probably one 'better' solution in terms of ease of implementation or maintenance.
Better scratch option 3. What if the user later wants to come back and have the windows program change the input form? It needs to update the database too.
Creating a database is usually a database administrator task. Unless it is a local database, maybe an embedded one, the user would need to know where and how create the database on the remote server, and she can have no clue about it. Where to store the database files? Which disks are available? And there could be many more parameters to set (memoery buffers size, etc.), users to be created and so on. And also you need very elevate privileges to be able to create a database, not something you give to average users or applications.
Thereby usually you ask the database administrator to create your database/schema, he will give you the credentials you need to connect, and then your application (or its setup) will create and initialize the needed objects (tables, etc.). Creating table (and other object) is usually as simple as running "CREATE TABLE...." statements. Just remember SQL takes one command only, if you need to run several commands you have to send them one after another yourself, although there are Delphi components which are able to split a script in commands and run one after another.
I have just taken over a project for a client and the database schema is in a total mess. I would like to rename a load of fields make it a relationship database.
But doing this will be a painstaking process as they have an API running of it also. So the idea would be to create a new database and start re-writing the code to use this instead. But I need a way to keep these tables in sync during this process.
Would you agree that I should use MYSQL EVENT's to keep updating the new table on Inserts / updates & deletes??
Or can you suggest a better way??
Hope you can advise !!
thanks for any input I get
I had the same problem in my project. I did the same thing like you - writing the whole database new. We developed the new database and the fitting code and after finishing that work we made a migration script (small application) which did the migration from old to new database.
Hope this gives you some ideas...
I had the same problem as well, and went by the way of duplicating data at the point of user input (basically saved to both databases at once, since the original schema lacked all the required information). After several months of development the customer realized that he is going to move to a completely new database and considered it too risky (and I agree with him).
Thus, I would advice to utter clearly to your customer that you are going to implement a new database instead of iterative refactoring of the current one.