How do I change the database name using MySQL? [duplicate] - mysql

This question already has answers here:
How do I rename a MySQL database (change schema name)?
(46 answers)
Closed 9 days ago.
How can I change the database name of my database?
I tried to use the rename database command, but on the documents about it it is said that it is dangerous to use. Then what should I need to do to rename my database name?
For example, if I want to rename my database to this.
database1 -> database2?

Follow bellow steps:
shell> mysqldump -hlocalhost -uroot -p database1 > dump.sql
mysql> CREATE DATABASE database2;
shell> mysql -hlocalhost -uroot -p database2 < dump.sql
If you want to drop database1 otherwise leave it.
mysql> DROP DATABASE database1;
Note : shell> denote command prompt and mysql> denote mysql prompt.

I don't think it's possible.
You can use mysqldump to dump the data and then create a schema with your new name and then dump the data into that new database.

Unfortunately, MySQL does not explicitly support that (except for dumping and reloading database again).
From http://dev.mysql.com/doc/refman/5.1/en/rename-database.html:
13.1.32. RENAME DATABASE Syntax
RENAME {DATABASE | SCHEMA} db_name TO new_db_name;
This statement was added in MySQL 5.1.7 but was found to be dangerous and was removed in MySQL 5.1.23. ... Use of this statement could result in loss of database contents, which is why it was removed. Do not use RENAME DATABASE in earlier versions in which it is present.

"As long as two databases are on the same file system, you can use RENAME TABLE to move a table from one database to another"
-- ensure the char set and collate match the existing database.
SHOW VARIABLES LIKE 'character_set_database';
SHOW VARIABLES LIKE 'collation_database';
CREATE DATABASE `database2` DEFAULT CHARACTER SET = `utf8` DEFAULT COLLATE = `utf8_general_ci`;
RENAME TABLE `database1`.`table1` TO `database2`.`table1`;
RENAME TABLE `database1`.`table2` TO `database2`.`table2`;
RENAME TABLE `database1`.`table3` TO `database2`.`table3`;
http://dev.mysql.com/doc/refman/5.7/en/rename-table.html

You can change the database name using MySQL interface.
Go to http://www.hostname.com/phpmyadmin
Go to database which you want to rename. Next, go to the operation tab. There you will find the input field to rename the database.

InnoDB supports RENAME TABLE statement to move table from one database to another. To use it programmatically and rename database with large number of tables, I wrote a couple of procedures to get the job done.
You can check it out here - SQL script #Gist
To use it simply call the renameDatabase procedure.
CALL renameDatabase('old_name', 'new_name');
Tested on MariaDB and should work ideally on all RDBMS using InnoDB transactional engine.

I agree with above answers and tips but there is a way to change database name with phpmyadmin
Renaming the Database
From cPanel, click on phpMyAdmin. (It should open in a new tab.)
Click on the database you wish to rename in the left hand column.
Click on the Operations tab.
Where it says "Rename database to:" enter the new database name.
Click the Go button.
When it asks you to want to create the new database and drop the old database, click OK to proceed. (This is a good time to make sure you spelled the new name correctly.)
Once the operation is complete, click OK when asked if you want to reload the database.
here's the video tutorial:
http://support.hostgator.com/articles/specialized-help/technical/phpmyadmin/how-to-rename-a-database-in-phpmyadmin

Another way to rename the database or taking an image of the database is by using the reverse engineering option in the database tab. It will create an ER diagram for the database. Rename the schema there.
After that, go to the File menu and go to export and forward engineer the database.
Then you can import the database.

Sequel Ace database client have a rename database functionality. Select the database you would like to edit and click Database in the menu and then click Rename Database from the dropdown. Rename the database and ckick rename. Done!

After much aggravation this is what I have found to work"simply".
First thing, I am using MYSQL Workbench and the import would not work as it should, as the import dump file would always revert to the original schema name.
I spent several hours trying every thing to no avail,all for a spelling error.
I solved the issue by opening one of the .sql dump files in notebook and hand editing the typo's of the schema name, take care to rename all instances schema name has three in the beginning, save the file and then import. this worked perfectly for me and hope that it will help others looking for the simple answer to changing database names/schema names.
One more tip that I have found true, when programs do not do as they should go to the "source" literally find the source code.
Hope this helps someone
Low rep so they wont let me comment on the prior/post answer(it keeps changing rank or position), so I added it here. reverse engineering will work fine as long as there is no data in the sever table. if data exists and you try to update the server after the name change it will either pull an error or just create a new database/schema with no data, I know I tried ten times to no avail.
The above works simply and avoids headaches, as one can review the SQL code for other errors if any or change table names or creation data.
the .sql file is just a compiled SQL code so in theory one could copy and add it through PHP or the script console of the database management tool.

You can use below command
alter database Testing modify name=LearningSQL;
Old Database Name = Testing,
New Database Name = LearningSQL

Go to data directory and try this:
mv database1 database2
It works for me on a 900 MB database size.

Try:
RENAME database1 TO database2;

Related

How to create a dump file when using mysqldump

I'm new to sql and trying to get to grips with some of the basics.
Say I've created a database as follows:
create database mydatabase;
and I want to back this up to a dump file. My confusion comes in here - what is this dump file? Does this automatically generate when I run the mysqldump command? Do I have to create it beforehand? If so, how? Sorry if this comes off stupid but I'm just lost here.
I know the final command would look as follows:
mysqldump -u -p mydatabase > SOMETHING;
but I don't know what to actually insert as the something
The dump file is just a file usually with a bunch of scripts you can run to recreate something in the database. Different database system have their own way of generating these, but you could effectively take a dump from an mysql database and into another rdbms as long as the scripts are compatible.
There are different options;
You could create a dump of just the schema. So that you can recreate that database later on without the data.
Create the dump with schema and data. So that you can recreate the scheme as well as the data within it.
Create the dump with just the data. So you can run those insert commands against another database of the same schema.
Basically dumps are just a way to get back to a previous state. They come in handy for different situation. Such as backing up data, replicating a data from one database to another.
As for the commands I have not used mysql in a while but looks like you are on the right track.

Opening huge mySQL database

I want to open a Huge SQL file (20 GB) on my system i tried phpmyadmin and bigdump but it seems bigdump dose not support more than 1 GB SQL files is there any script or software that i can use to open,view,search and edit it.
MySQL Workbench should work fine, works well for large DB's, and is very useful...
https://www.mysql.com/products/workbench/
Install, then basically you just create a new connection, and then double click it on the home screen to get access to the DB. Right click on a table and click Select 1000 for quick view of table data.
More info http://mysqlworkbench.org/2009/11/mysql-workbench-5-2-beta-quick-start-tutorial/
Try using mysql command line to do basic SELECT queries.
$ mysql -u myusername -p
>>> show DATABASES; // shows you a list of databases
>>> use databasename; //selects database to query
>>> show TABLES; // displays tables in database
>>> SELECT * FROM tablename WHERE column = 'somevalue';
It totally depends on the structure of the database, one way of handling this is by exporting each table in a seperate sql file, as for editing the file, you're limited to opening the raw sql files in notepad or any other text editor. But you probably already knew that.
What are the settings that were used to export the database? People often forget that there's also an option to turn on comments, for big databases it makes sense to turn that off.
To get a more detailed answer have you tried asking at https://dba.stackexchange.com/?

Rename mysql database? [duplicate]

This question already has answers here:
How do I rename a MySQL database (change schema name)?
(46 answers)
Closed 10 days ago.
How can I rename the MySQL database name using query?
I tried rename database via phpmyadmin and getting the following error,
SQL query:
RENAME DATABASE test TO test_bkp
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DATABASE test To test_bkp' at line 1
In phpmyadmin you can just click on your database, then go to the Operations tab which lets you rename it.
It says here that:
This statement was added in MySQL 5.1.7 but was found to be dangerous
and was removed in MySQL 5.1.23. It was intended to enable upgrading
pre-5.1 databases to use the encoding implemented in 5.1 for mapping
database names to database directory names (see Section 9.2.3,
“Mapping of Identifiers to File Names”). However, use of this
statement could result in loss of database contents, which is why it
was removed. Do not use RENAME DATABASE in earlier versions in which
it is present.
You can import all data into a new database then delete the old one.
It is also possible to use the RENAME TABLE statement using same from/to table name but different from/to database. There are a few catches.
CREATE DATABASE `test_bkp`;
RENAME TABLE
`test`.`table1` TO `test_bkp`.`table1`,
`test`.`table2` TO `test_bkp`.`table2`,
`test`.`table3` TO `test_bkp`.`table3`;
It's been disabled by the developers. See http://dev.mysql.com/doc/refman/5.1/en/rename-database.html
Adding to this, I'd use a copy (then delete) rather than a rename. Have a look at Cloning a MySQL database on the same MySql instance
You can do this in MySQL < 5.1.23
with
RENAME {DATABASE | SCHEMA} db_name TO new_db_name;
In later versions have a look at
ALTER {DATABASE | SCHEMA} [db_name]
Ok, so if the rename option doesn't show, here a quick way of cloning a database using phpMyAdmin:
Open the database you want to copy
Click the Operations tab.
where it says "Copy database to:" type in the name of the new database.
Select "structure and data" to copy everything or "Structure only"
Check the box "CREATE DATABASE before copying" to create a new database.
Check the box "Add AUTO_INCREMENT value."
Click on the Go button to proceed.
NB before deleting the old database, make sure that passwords have also copied over.
in wamp server
click on databases
then choose your database
from header menu choose operations
make the new name
it will rename the database
but actually it create a new database copy all the data then drop the old data base
Export current database.
create a new database with a name you want.
import old database file on the new database.
Well, it's possible to rename your database but here's the logic behind the rename option.
1. Xampp creates a new database with the name
2. Xampp copies all table and data in it from the previous database
3. Xampp migrates data to the new database table and drops the previous database
Steps to rename Database
1. Click the Operations option in the nav pane
2. Find the ' Rename database to ' Spot, and input new name in the input field
3. Tick the ' Adjust privileges ' checkbox
4. Click go
I hope this works! :)
you can export your databse table, create a new one and import table to the new database!
also you can rename your data base using phpmyadmin by selecting your database and click on operations tab and rename your database!
the last and not recommended way is create a new databse and use this, but you may lost your data!
CREATE DATABASE new_db_name / DROP DATABASE old_db_name

question about MySQL database migration

If I have a MySQL database with several tables on a live server, now I would like to migrate this database to another server. Of course, the migration I mean here involves some database tables, for example: add some new columns to several tables, add some new tables etc..
Now, the only method I can think of is to use some php/python(two scripts I know) script, connect two databases, dump the data from the old database, and then write into the new database. However, this method is not efficient at all. For example: in old database, table A has 28 columns; in new database, table A has 29 columns, but the extra column will have default value 0 for all the old rows. My script still needs to dump the data row by row and insert each row into the new database.
Using MySQLDump etc.. won't work. Here is the detail. For example: I have FOUR old databases, I can name them as 'DB_a', 'DB_b', 'DB_c', 'DB_d'. Now the old table A has 28 columns, I want to add each row in table A into the new database with a new column ID 'DB_x' (x to indicate which database it comes from). If I can't differentiate the database ID by the row's content, the only way I can identify them is going through some user input parameters.
Is there any tools or a better method than writing a script yourself? Here, I dont need to worry about multithread writing problems etc.., I mean the old database will be down (not open to public usage etc.., only for upgrade ) for a while.
Thanks!!
I don't entirely understand your situation with the columns (wouldn't it be more sensible to add any new columns after migration?), but one of the arguably fastest methods to copy a database across servers is mysqlhotcopy. It can copy myISAM only and has a number of other requirements, but it's awfully fast because it skips the create dump / import dump step completely.
Generally when you migrate a database to new servers, you don't apply a bunch of schema changes at the same time, for the reasons that you're running into right now.
MySQL has a dump tool called mysqldump that can be used to easily take a snapshot/backup of a database. The snapshot can then be copied to a new server and installed.
You should figure out all the changes that have been done to your "new" database, and write out a script of all the SQL commands needed to "upgrade" the old database to the new version that you're using (e.g. ALTER TABLE a ADD COLUMN x, etc). After you're sure it's working, take a dump of the old one, copy it over, install it, and then apply your change script.
Use mysqldump to dump the data, then echo output.txt > msyql. Now the old data is on the new server. Manipulate as necessary.
Sure there are tools that can help you achieving what you're trying to do. Mysqldump is a premier example of such tools. Just take a glance here:
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
What you could do is:
1) You make a dump of the current db, using mysqldump (with the --no-data option) to fetch the schema only
2) You alter the schema you have dumped, adding new columns
3) You create your new schema (mysql < dump.sql - just google for mysql backup restore for more help on the syntax)
4) Dump your data using the mysqldump complete-insert option (see link above)
5) Import your data, using mysql < data.sql
This should do the job for you, good luck!
Adding extra rows can be done on a live database:
ALTER TABLE [table-name] ADD [row-name] MEDIUMINT(8) default 0;
MySql will default all existing rows to the default value.
So here is what I would do:
make a copy of you're old database with MySql dump command.
run the resulting SQL file against you're new database, now you have an exact copy.
write a migration.sql file that will modify you're database with modify table commands and for complex conversions some temporary MySql procedures.
test you're script (when fail, go to (2)).
If all OK, then goto (1) and go live with you're new database.
These are all valid approaches, but I believe you want to write a sql statement that writes other insert statements that support the new columns you have.

MySQL database - backup problem

Hi I need to backup MySQL database and then deploy it on another MySQL server.
The problem is, I need it backup without data , just script which creates database, tables, procedures, users, resets autoincrements etc. ...
I tried MySQL administrator tool (Windows) and UNchecked "complete inserts check box", but it still created it ...
Thanks in advance
use mysqldump with option -d or --no-data
don't forget option -R to get the procedures
this page could help you: http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html
From within phpMyAdmin you can export the structure, with or without the data. The only thing I'm not sure of, is wether it exports users as well. If you like, I can test that tomorrow morning. It exports users too. You can check all sorts of options.
(source: obviousmatter.com)
According to the page, there isn't a good way to dump the routines and have them easily able to be recreated.
What they suggest is to dump the mysql.proc table directly. Including all the data.
Then use your myback.sql to restore the structure. Then restore the mysql.proc table with all of its data.
"... If you require routines to be re-created with their original timestamp attributes, do not use --routines. Instead, dump and reload the contents of the mysql.proc table directly, using a MySQL account that has appropriate privileges for the mysql database. ..."