I am having the weirdest error of all.
Sometimes, when creating or altering tables, I get the 'table already exists' error. However, DROP TABLE returns '#1051 - unknown table'. So I got a table I cannot create, cannot drop.
When I try to drop the database, mysqld crashes. Sometimes it helps to create another db with different name, sometimes it does not.
I use a DB with ~50 tables, all InnoDB. This problem occurs with different tables.
I experienced this on Windows, Fedora and Ubuntu, MySQL 5.1 and 5.5. Same behaviour, when using PDO, PHPMyAdmin or commandline. I use MySQL Workbench to manage my schema - I saw some related errors (endlines and stuff), however none of them were relevant for me.
No, it is not a view, it is a table. All names are lowercase.
I tried everything I could google - flushing tables, moving .frm files from db to db, reading mysql log, nothing helped but reinstalling the whole damn thing.
'Show tables' reveals nothing, 'describe' table says 'table doesn't exist,' there is no .frm file, yet 'create table' still ends with an error (and so does 'create table if not exists') and dropping database crashes mysql
Related, yet unhelpful questions:
Mysql 1050 Error "Table already exists" when in fact, it does not
MySQL Table does not exist error, but it does exist
Edit:
mysql> use askyou;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table users_has_friends (id int primary key);
ERROR 1050 (42S01): Table '`askyou`.`users_has_friends`' already exists
mysql> drop table users_has_friends;
ERROR 1051 (42S02): Unknown table 'users_has_friends'
And such, all the same: table doesn't exist, yet cannot be created;
mysql> drop database askyou;
ERROR 2013 (HY000): Lost connection to MySQL server during query
Names change, this is not the only table / database I've run into problems with
I've seen this issue when the data file is missing in the data directory but the table definition file exists or vise-versa. If you're using innodb_file_per_table, check the data directory to make sure you have both an .frm file and .ibd file for the table in question. If it's MYISAM, there should be a .frm, .MYI and a .MYD file.
The problem can usually be resolved by deleting the orphaned file manually.
Going on a wild guess here, but it seems like innodb still has an entry for your tables in a tablespace, probably in ibdata. If you really don't need any of the data, or if you have backups, try the following:
Delete all schemas (excluding mysql)
shut down the database
Make sure that all folders in your data directory have been removed properly (again, excluding mysql)
delete ibdata and log files
restart the database. It should recreate the tablespace and logs from scratch.
The fix turns out to be easy; at least what I worked out, worked for me.
Create a table "zzz" on another MySQL instance, where zzz is the problem table name.
(i.e. if the table is called schrodinger, substitute that for zzz whever written.)
It does not matter what the definition of the table is. It's a temporary dummy;
Copy the zzz.frm file to the database directory on server where the table should be,
making sure file ownership and permissions are still correct on the file.
On MySQL, you can now do "show tables;", and the table zzz will be there.
mysql> drop table zzz;
...should now works. Clear any zzz.MYD or ZZZ.MYI files in the directory if necessary.
I doubt this is a direct answer to the question case here, but here is how I solved this exact perceived problem on my OS X Lion system.
I frequently create/drop tables for some analytics jobs I have scheduled. At some point, I started getting table already exists errors half-way through my script. A server restart typically solved the issue, but that was too annoying of a solution.
Then I noticed in the local error log file this particular line:
[Warning] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
This gave me the idea that maybe if my tables contained capital letters, MySQL would be fooled into thinking they are still there even after I had dropped them. That turned out to be the case and switching to using only lowercase letters for table names made the problem go away.
It is likely the result of some misconfiguration in my case, but hopefully this error case will help someone waste less time trying to find a solution.
This is an old question but I just hit the same issue and an answer in one of the related issues linked at the top was just what I needed and far less drastic than deleting files, tables, shutting down the server etc.
mysqladmin -uxxxxxx -pyyyyy flush-tables
If will are stock with this error 1051 and you only want to delete the database and import this again do this steps and all gonna be just fine....
in Unix envoriment AS root:
rm -rf /var/lib/mysql/YOUR_DATABASE;
OPTIONAL -> mysql_upgrade --force
mysqlcheck -uUSER -pPASS YOUR_DATABASE
mysqladmin -uUSER -pPASS drop YOUR_DATABASE
mysqladmin -uUSER -pPASS create YOUR_DATABASE
mysql -uUSER -pPASS YOUR_DATABASE < IMPORT_FILE
Regards,
Christus
In my case the problem was solved by changing the ownership of the mysql data directory to the user that ran the application. (In my case it was a Java application running Jetty webserver.)
Even though mysql was running and other apps could use it properly, this app had a problem with that. After changing the data directory ownership and resetting the user's password, everything worked properly.
I was having this problem with one particular table. Reading the possible solutions i've did some steps like:
Search for orphan files: didn't exist anyone;
execute: show full tables in database;: didn't see the problematic one;
execute: describe table;: returned table doesn't exist;
execute: SELECT * FROM information_schema.TABLES WHERE TABLE_NAME='table';: returned Empty set;
Search by the phpMyAdmin manually the query above: didn't exist;
And, after those steps, i check again with the show tables; and... vualá! the problematic table was gone. I could create it and drop it with the same problematic name with no problem, and i didn't have even to restart the server! Weird...
I had this problem and hoped deleting the IBD file would help as posted above but it made no difference . MySQL only recreated a new IBD file . In my case, there are actually similar tables in other databases in the same MySQL instance . Since the FRM file was missing , I copied the FRM file from the similar table in another database , restarted MySQL and the table worked correctly .
I ran into this error after I created a table and deleted it, then wanted to create it again.
In my case, I had a self-contained dump file so I dropped my schema, recreated it and imported tables and data using the dump file.
It happens at our site (but rarely) usually when an "event" happens while running certain scripts that do a lot of rebuilding. Events include network outages or power problems.
What I do for this on the very rare occasions it happens - I use the heavy-handed approach:
I needed to simply get rid-of-and-rebuild the particular table. I'm usually in a position that this is OK since the table is being built. (Your situation may be different if you need to recover data)
As an admin, go into the mysql installation (on windows its may be "...program files/mysql/MySQL Server xx/data/<schemaname>
Find the offending file with the table name in the <schemaname> folder - and delete it.
Check for orphaned temporary files and delete them too. #...frm files if they happen to be there.
MySQL will let you CREATE the table again
I've had this problem on a couple of different databases over a long time (years). It was a stumper because the contradicting messages. The first time I did a variation of the deleting/rebuilding/renaming database as described in the other answers and managed to get things going, but it definitely takes longer that way. Lucky for me it's always happened to reference tables that are being rebuilt - DROP'd and CREATEd - typically in the morning. Rarely got the problem but came to recognize it as a special quirky case. (I'll restate : if you need to recover the data look to the other solutions.)
it isn't a table belonging to another user, or in another database
it isn't the upper/lower case issue, I use all lower-case, but that was an interesting issue!
it was extra extra frustrating seeing responses with variations of "it definitely was <there/not-there/some-other-user-table-case> and you're just not doing it right" :)
the table didn't show on "show tables"
the table was (always was/had been) an INNODB table.
trying to DROP the table gave the error message that the table doesn't exist.
but trying to CREATE the table gave the error message that the table already exists.
using mysql 5.0 or 5.1
REPAIR is ineffective for this problem
Related
I have an situation when I've restored databases( with flag --all-databases) 2 weeks ago, but now I need newer data in few DB, so my question is can I just mysqldump < newbackup with already installed databases or I need to remove all data? Please provide me simplest or fastest way to restore new instance.
In sqldump I have sentences like CREATE DATABASE /*!32312 IF NOT EXISTS*/
By default, your dump file will include DROP TABLE and CREATE TABLE statements, so any tables that exist and the data in them will be dropped first. Then the tables as they exist in your dump will be restored.
Note this might even mean that the tables themselves might not have the same columns and indexes. They'll be recreated as they are defined in your dump file. Any alterations made since the dump was created will be lost when the table is dropped.
Any tables that exist in your current database that aren't in the dump file won't be touched. That is, if you created another table since your dump file was created, then there's no DROP & CREATE in the dump file. So restoring the dump won't do anything to the newer tables. This might lead to some inconsistencies, if the newer tables reference data in the restored tables.
It's possible that the DROP TABLE and CREATE TABLE statements will be missing from the dump file. There are options for mysqldump that make the dump omit these statements (refer to documentation or mysqldump --help). But by default, these statements are present.
This involves MySQL 5.7 running on Windows Server 2016.
I'm working with a TRUNCATE statement in MySql to reduce the size of a large Log file (named "mySite.log"), which resides in:
ProgramData/MySQL/MySQL Server 5.7/Data/
I have researched and implemented the following:
mysql> SET GLOBAL general_log=OFF;
and this was successful.
However, I am trying to ascertain that the large log file that I see in the directory stated above is in fact the General Query Log File. It carries the name of the database as the prefix of the file name ("MySite.log") just as the other files (.bin's and .err, .pid) in the same directory do.
Is this large log file actually the general_log file? (If using MySQL Workbench, where would the naming of the log file and storage location be set up? I can't seem to locate that.)
Will the following syntax truncate the log file?
mysql> TRUNCATE TABLE mysql.general_log;
Will 'TRUNCATE TABLE' be used, even if the log is stored in a file, rather than database table?
Will 'mysql.general_log' need to be renamed to 'myDatabase.mysite' to match the name of my "MySite.log" file, from above?
Thanks for any leads.
Some interesting manual entries to read about this:
5.4.1 Selecting General Query and Slow Query Log Output Destinations
5.4.3 The General Query Log
You can check how your server is configured with
SHOW GLOBAL VARIABLES LIKE '%log%';
Then look for the value of log-output. This shows if you're logging to FILE, TABLE or both.
When it's FILE, check for the value of general_log_file. This is where the log file is in your file system. You can simply remove it, and create a new file (in case you ever want to enable general_log again). Then execute FLUSH LOGS; afterwards.
When it's TABLE then your TRUNCATE TABLE mysql.general_log; statement is correct.
Regarding your second question, just never mess with the tables in the mysql schema. Just don't (if you don't know what you're doing). Also I don't even get how you got to that thought.
I am having the weirdest error of all.
Sometimes, when creating or altering tables, I get the 'table already exists' error. However, DROP TABLE returns '#1051 - unknown table'. So I got a table I cannot create, cannot drop.
When I try to drop the database, mysqld crashes. Sometimes it helps to create another db with different name, sometimes it does not.
I use a DB with ~50 tables, all InnoDB. This problem occurs with different tables.
I experienced this on Windows, Fedora and Ubuntu, MySQL 5.1 and 5.5. Same behaviour, when using PDO, PHPMyAdmin or commandline. I use MySQL Workbench to manage my schema - I saw some related errors (endlines and stuff), however none of them were relevant for me.
No, it is not a view, it is a table. All names are lowercase.
I tried everything I could google - flushing tables, moving .frm files from db to db, reading mysql log, nothing helped but reinstalling the whole damn thing.
'Show tables' reveals nothing, 'describe' table says 'table doesn't exist,' there is no .frm file, yet 'create table' still ends with an error (and so does 'create table if not exists') and dropping database crashes mysql
Related, yet unhelpful questions:
Mysql 1050 Error "Table already exists" when in fact, it does not
MySQL Table does not exist error, but it does exist
Edit:
mysql> use askyou;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table users_has_friends (id int primary key);
ERROR 1050 (42S01): Table '`askyou`.`users_has_friends`' already exists
mysql> drop table users_has_friends;
ERROR 1051 (42S02): Unknown table 'users_has_friends'
And such, all the same: table doesn't exist, yet cannot be created;
mysql> drop database askyou;
ERROR 2013 (HY000): Lost connection to MySQL server during query
Names change, this is not the only table / database I've run into problems with
I've seen this issue when the data file is missing in the data directory but the table definition file exists or vise-versa. If you're using innodb_file_per_table, check the data directory to make sure you have both an .frm file and .ibd file for the table in question. If it's MYISAM, there should be a .frm, .MYI and a .MYD file.
The problem can usually be resolved by deleting the orphaned file manually.
Going on a wild guess here, but it seems like innodb still has an entry for your tables in a tablespace, probably in ibdata. If you really don't need any of the data, or if you have backups, try the following:
Delete all schemas (excluding mysql)
shut down the database
Make sure that all folders in your data directory have been removed properly (again, excluding mysql)
delete ibdata and log files
restart the database. It should recreate the tablespace and logs from scratch.
The fix turns out to be easy; at least what I worked out, worked for me.
Create a table "zzz" on another MySQL instance, where zzz is the problem table name.
(i.e. if the table is called schrodinger, substitute that for zzz whever written.)
It does not matter what the definition of the table is. It's a temporary dummy;
Copy the zzz.frm file to the database directory on server where the table should be,
making sure file ownership and permissions are still correct on the file.
On MySQL, you can now do "show tables;", and the table zzz will be there.
mysql> drop table zzz;
...should now works. Clear any zzz.MYD or ZZZ.MYI files in the directory if necessary.
I doubt this is a direct answer to the question case here, but here is how I solved this exact perceived problem on my OS X Lion system.
I frequently create/drop tables for some analytics jobs I have scheduled. At some point, I started getting table already exists errors half-way through my script. A server restart typically solved the issue, but that was too annoying of a solution.
Then I noticed in the local error log file this particular line:
[Warning] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
This gave me the idea that maybe if my tables contained capital letters, MySQL would be fooled into thinking they are still there even after I had dropped them. That turned out to be the case and switching to using only lowercase letters for table names made the problem go away.
It is likely the result of some misconfiguration in my case, but hopefully this error case will help someone waste less time trying to find a solution.
This is an old question but I just hit the same issue and an answer in one of the related issues linked at the top was just what I needed and far less drastic than deleting files, tables, shutting down the server etc.
mysqladmin -uxxxxxx -pyyyyy flush-tables
If will are stock with this error 1051 and you only want to delete the database and import this again do this steps and all gonna be just fine....
in Unix envoriment AS root:
rm -rf /var/lib/mysql/YOUR_DATABASE;
OPTIONAL -> mysql_upgrade --force
mysqlcheck -uUSER -pPASS YOUR_DATABASE
mysqladmin -uUSER -pPASS drop YOUR_DATABASE
mysqladmin -uUSER -pPASS create YOUR_DATABASE
mysql -uUSER -pPASS YOUR_DATABASE < IMPORT_FILE
Regards,
Christus
In my case the problem was solved by changing the ownership of the mysql data directory to the user that ran the application. (In my case it was a Java application running Jetty webserver.)
Even though mysql was running and other apps could use it properly, this app had a problem with that. After changing the data directory ownership and resetting the user's password, everything worked properly.
I was having this problem with one particular table. Reading the possible solutions i've did some steps like:
Search for orphan files: didn't exist anyone;
execute: show full tables in database;: didn't see the problematic one;
execute: describe table;: returned table doesn't exist;
execute: SELECT * FROM information_schema.TABLES WHERE TABLE_NAME='table';: returned Empty set;
Search by the phpMyAdmin manually the query above: didn't exist;
And, after those steps, i check again with the show tables; and... vualá! the problematic table was gone. I could create it and drop it with the same problematic name with no problem, and i didn't have even to restart the server! Weird...
I had this problem and hoped deleting the IBD file would help as posted above but it made no difference . MySQL only recreated a new IBD file . In my case, there are actually similar tables in other databases in the same MySQL instance . Since the FRM file was missing , I copied the FRM file from the similar table in another database , restarted MySQL and the table worked correctly .
I ran into this error after I created a table and deleted it, then wanted to create it again.
In my case, I had a self-contained dump file so I dropped my schema, recreated it and imported tables and data using the dump file.
It happens at our site (but rarely) usually when an "event" happens while running certain scripts that do a lot of rebuilding. Events include network outages or power problems.
What I do for this on the very rare occasions it happens - I use the heavy-handed approach:
I needed to simply get rid-of-and-rebuild the particular table. I'm usually in a position that this is OK since the table is being built. (Your situation may be different if you need to recover data)
As an admin, go into the mysql installation (on windows its may be "...program files/mysql/MySQL Server xx/data/<schemaname>
Find the offending file with the table name in the <schemaname> folder - and delete it.
Check for orphaned temporary files and delete them too. #...frm files if they happen to be there.
MySQL will let you CREATE the table again
I've had this problem on a couple of different databases over a long time (years). It was a stumper because the contradicting messages. The first time I did a variation of the deleting/rebuilding/renaming database as described in the other answers and managed to get things going, but it definitely takes longer that way. Lucky for me it's always happened to reference tables that are being rebuilt - DROP'd and CREATEd - typically in the morning. Rarely got the problem but came to recognize it as a special quirky case. (I'll restate : if you need to recover the data look to the other solutions.)
it isn't a table belonging to another user, or in another database
it isn't the upper/lower case issue, I use all lower-case, but that was an interesting issue!
it was extra extra frustrating seeing responses with variations of "it definitely was <there/not-there/some-other-user-table-case> and you're just not doing it right" :)
the table didn't show on "show tables"
the table was (always was/had been) an INNODB table.
trying to DROP the table gave the error message that the table doesn't exist.
but trying to CREATE the table gave the error message that the table already exists.
using mysql 5.0 or 5.1
REPAIR is ineffective for this problem
I am having the weirdest error of all.
Sometimes, when creating or altering tables, I get the 'table already exists' error. However, DROP TABLE returns '#1051 - unknown table'. So I got a table I cannot create, cannot drop.
When I try to drop the database, mysqld crashes. Sometimes it helps to create another db with different name, sometimes it does not.
I use a DB with ~50 tables, all InnoDB. This problem occurs with different tables.
I experienced this on Windows, Fedora and Ubuntu, MySQL 5.1 and 5.5. Same behaviour, when using PDO, PHPMyAdmin or commandline. I use MySQL Workbench to manage my schema - I saw some related errors (endlines and stuff), however none of them were relevant for me.
No, it is not a view, it is a table. All names are lowercase.
I tried everything I could google - flushing tables, moving .frm files from db to db, reading mysql log, nothing helped but reinstalling the whole damn thing.
'Show tables' reveals nothing, 'describe' table says 'table doesn't exist,' there is no .frm file, yet 'create table' still ends with an error (and so does 'create table if not exists') and dropping database crashes mysql
Related, yet unhelpful questions:
Mysql 1050 Error "Table already exists" when in fact, it does not
MySQL Table does not exist error, but it does exist
Edit:
mysql> use askyou;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table users_has_friends (id int primary key);
ERROR 1050 (42S01): Table '`askyou`.`users_has_friends`' already exists
mysql> drop table users_has_friends;
ERROR 1051 (42S02): Unknown table 'users_has_friends'
And such, all the same: table doesn't exist, yet cannot be created;
mysql> drop database askyou;
ERROR 2013 (HY000): Lost connection to MySQL server during query
Names change, this is not the only table / database I've run into problems with
I've seen this issue when the data file is missing in the data directory but the table definition file exists or vise-versa. If you're using innodb_file_per_table, check the data directory to make sure you have both an .frm file and .ibd file for the table in question. If it's MYISAM, there should be a .frm, .MYI and a .MYD file.
The problem can usually be resolved by deleting the orphaned file manually.
Going on a wild guess here, but it seems like innodb still has an entry for your tables in a tablespace, probably in ibdata. If you really don't need any of the data, or if you have backups, try the following:
Delete all schemas (excluding mysql)
shut down the database
Make sure that all folders in your data directory have been removed properly (again, excluding mysql)
delete ibdata and log files
restart the database. It should recreate the tablespace and logs from scratch.
The fix turns out to be easy; at least what I worked out, worked for me.
Create a table "zzz" on another MySQL instance, where zzz is the problem table name.
(i.e. if the table is called schrodinger, substitute that for zzz whever written.)
It does not matter what the definition of the table is. It's a temporary dummy;
Copy the zzz.frm file to the database directory on server where the table should be,
making sure file ownership and permissions are still correct on the file.
On MySQL, you can now do "show tables;", and the table zzz will be there.
mysql> drop table zzz;
...should now works. Clear any zzz.MYD or ZZZ.MYI files in the directory if necessary.
I doubt this is a direct answer to the question case here, but here is how I solved this exact perceived problem on my OS X Lion system.
I frequently create/drop tables for some analytics jobs I have scheduled. At some point, I started getting table already exists errors half-way through my script. A server restart typically solved the issue, but that was too annoying of a solution.
Then I noticed in the local error log file this particular line:
[Warning] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
This gave me the idea that maybe if my tables contained capital letters, MySQL would be fooled into thinking they are still there even after I had dropped them. That turned out to be the case and switching to using only lowercase letters for table names made the problem go away.
It is likely the result of some misconfiguration in my case, but hopefully this error case will help someone waste less time trying to find a solution.
This is an old question but I just hit the same issue and an answer in one of the related issues linked at the top was just what I needed and far less drastic than deleting files, tables, shutting down the server etc.
mysqladmin -uxxxxxx -pyyyyy flush-tables
If will are stock with this error 1051 and you only want to delete the database and import this again do this steps and all gonna be just fine....
in Unix envoriment AS root:
rm -rf /var/lib/mysql/YOUR_DATABASE;
OPTIONAL -> mysql_upgrade --force
mysqlcheck -uUSER -pPASS YOUR_DATABASE
mysqladmin -uUSER -pPASS drop YOUR_DATABASE
mysqladmin -uUSER -pPASS create YOUR_DATABASE
mysql -uUSER -pPASS YOUR_DATABASE < IMPORT_FILE
Regards,
Christus
In my case the problem was solved by changing the ownership of the mysql data directory to the user that ran the application. (In my case it was a Java application running Jetty webserver.)
Even though mysql was running and other apps could use it properly, this app had a problem with that. After changing the data directory ownership and resetting the user's password, everything worked properly.
I was having this problem with one particular table. Reading the possible solutions i've did some steps like:
Search for orphan files: didn't exist anyone;
execute: show full tables in database;: didn't see the problematic one;
execute: describe table;: returned table doesn't exist;
execute: SELECT * FROM information_schema.TABLES WHERE TABLE_NAME='table';: returned Empty set;
Search by the phpMyAdmin manually the query above: didn't exist;
And, after those steps, i check again with the show tables; and... vualá! the problematic table was gone. I could create it and drop it with the same problematic name with no problem, and i didn't have even to restart the server! Weird...
I had this problem and hoped deleting the IBD file would help as posted above but it made no difference . MySQL only recreated a new IBD file . In my case, there are actually similar tables in other databases in the same MySQL instance . Since the FRM file was missing , I copied the FRM file from the similar table in another database , restarted MySQL and the table worked correctly .
I ran into this error after I created a table and deleted it, then wanted to create it again.
In my case, I had a self-contained dump file so I dropped my schema, recreated it and imported tables and data using the dump file.
It happens at our site (but rarely) usually when an "event" happens while running certain scripts that do a lot of rebuilding. Events include network outages or power problems.
What I do for this on the very rare occasions it happens - I use the heavy-handed approach:
I needed to simply get rid-of-and-rebuild the particular table. I'm usually in a position that this is OK since the table is being built. (Your situation may be different if you need to recover data)
As an admin, go into the mysql installation (on windows its may be "...program files/mysql/MySQL Server xx/data/<schemaname>
Find the offending file with the table name in the <schemaname> folder - and delete it.
Check for orphaned temporary files and delete them too. #...frm files if they happen to be there.
MySQL will let you CREATE the table again
I've had this problem on a couple of different databases over a long time (years). It was a stumper because the contradicting messages. The first time I did a variation of the deleting/rebuilding/renaming database as described in the other answers and managed to get things going, but it definitely takes longer that way. Lucky for me it's always happened to reference tables that are being rebuilt - DROP'd and CREATEd - typically in the morning. Rarely got the problem but came to recognize it as a special quirky case. (I'll restate : if you need to recover the data look to the other solutions.)
it isn't a table belonging to another user, or in another database
it isn't the upper/lower case issue, I use all lower-case, but that was an interesting issue!
it was extra extra frustrating seeing responses with variations of "it definitely was <there/not-there/some-other-user-table-case> and you're just not doing it right" :)
the table didn't show on "show tables"
the table was (always was/had been) an INNODB table.
trying to DROP the table gave the error message that the table doesn't exist.
but trying to CREATE the table gave the error message that the table already exists.
using mysql 5.0 or 5.1
REPAIR is ineffective for this problem
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;