LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-02-2014, 04:30 AM   #1
circus78
Member
 
Registered: Dec 2011
Posts: 273

Rep: Reputation: Disabled
Renaming mysql database


Hi,
I need to rename a huge MySQL database.
I can't do this through phpmyadmin, as it will take very long time.
Can I simply stop mysql server daemon, rename database directory in /var/lib/mysql, and after restart mysql?
Is it a safe way to rename database?
Thank you
 
Old 10-02-2014, 07:35 AM   #2
carlosinfl
Senior Member
 
Registered: May 2004
Location: Orlando, FL
Distribution: Arch
Posts: 2,905

Rep: Reputation: 77
Quote:
Originally Posted by circus78 View Post
Hi,
I need to rename a huge MySQL database.
I can't do this through phpmyadmin, as it will take very long time.
Can I simply stop mysql server daemon, rename database directory in /var/lib/mysql, and after restart mysql?
Is it a safe way to rename database?
Thank you
This is really bad. DO NOT DO THIS!!!

Your MySQL installation folder is /var/lib/mysql/ - that does not mean 'Database' in the word I think you're trying to define. The database server program should not / never be renamed as this will break MySQL. If you want to rename / change the name of an actual database you or someone has created in MySQL, you need to connect to the database as a database user w/ admin privileges.

To rename a database in MySQL, you do the following from the 'mysql' database:

Code:
use mysql;

RENAME DATABASE old_dbname TO new_dbname;
^
You MUST / SHOULD use SQL statements to alter / make changes correctly. DO NOT MAKE DATABASE CHANGES FROM THE FILE SYSTEM LEVEL. THAT IS VERY VERY BAD and makes pandas cry!

http://dev.mysql.com/doc/refman/5.1/...-database.html
 
Old 10-02-2014, 10:03 AM   #3
circus78
Member
 
Registered: Dec 2011
Posts: 273

Original Poster
Rep: Reputation: Disabled
Hi,

I tried with your command but without success;

Quote:
mysql> use mysql;

mysql> RENAME DATABASE amavis TO no_amavis;
ERROR 1064 (42000): 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 db_amavis amavis' at line 1
mysql>
# mysql -V
mysql Ver 14.14 Distrib 5.1.73, for debian-linux-gnu (x86_64) using readline 6.1
 
Old 10-02-2014, 10:05 AM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Rename Database was removed around that version.

You will probably want Alter Database instead.

Read here for correct syntax: http://dev.mysql.com/doc/refman/5.1/...-database.html

Working on the database is usually reserved for a DB admin or sysadmin,. do you have one available to help you out? That is the best course of action, since the person responsible will make backups and be able to restore and fix it if there are issues.

Last edited by szboardstretcher; 10-02-2014 at 11:03 AM.
 
Old 10-02-2014, 10:26 AM   #5
circus78
Member
 
Registered: Dec 2011
Posts: 273

Original Poster
Rep: Reputation: Disabled
Hi,
I am the sysadmin of this machine
What is the correct syntax for renaming database (using "ALTER DATABASE")?
It seems that the only way is:

1. dump database
2. drop database
3. import database with new name

but, as I said, I would like to avoid this, since it's a big amount of data.
Thank you very much.
 
Old 10-02-2014, 11:02 AM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Cool.

Read here for correct syntax: http://dev.mysql.com/doc/refman/5.1/...-database.html

Last edited by szboardstretcher; 10-02-2014 at 11:03 AM.
 
Old 10-04-2014, 02:18 PM   #7
circus78
Member
 
Registered: Dec 2011
Posts: 273

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Cool.

Read here for correct syntax: http://dev.mysql.com/doc/refman/5.1/...-database.html
No way.



Code:
mysql> create database old_name;
Query OK, 1 row affected (0.00 sec)

mysql> alter database old_name upgrade data directory new_name;
ERROR 1064 (42000): 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 'new_name' at line 1
mysql> alter database old_name upgrade data directory name new_name;
ERROR 1064 (42000): 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 'new_name' at line 1
mysql> alter database old_name upgrade data directory name to new_name;
ERROR 1064 (42000): 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 'to new_name' at line 1
mysql>
 
Old 10-04-2014, 02:45 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by circus78 View Post
No way.

Code:
mysql> create database old_name;
Query OK, 1 row affected (0.00 sec)

mysql> alter database old_name upgrade data directory new_name;
ERROR 1064 (42000): 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 'new_name' at line 1
mysql>...
ALTER DATABASE is not intended to support changing the name of the database - it is for updating the character encoding of the name. From the built-in help (MariaDB)

Quote:
"It is intended to update a database directory name to the current
encoding format if the name contains special characters that need
encoding."
Also be sure to read that page in its entireity - is is also necessary to drop and rebuild all stored procedures to use the new character encodings.

How large is this DB?

A quick search turned up this solution, table by table rename. Of course, you will also need to update access rights and all other things that reference the old DB to use the new DB.

Otherwise I think that you are stuck with dump/restore as the safest path.

Last edited by astrogeek; 10-04-2014 at 03:10 PM.
 
Old 10-05-2014, 01:57 PM   #9
circus78
Member
 
Registered: Dec 2011
Posts: 273

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
ALTER DATABASE is not intended to support changing the name of the database - it is for updating the character encoding of the name.



Quote:
Otherwise I think that you are stuck with dump/restore as the safest path.
Yes, I agree.
I was looking for a quicker solution
Thank you very much
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
postfix +Cyrus SASL authentication problem + pam-mysql+mysql database kibirango Slackware 1 12-25-2012 08:47 AM
[SOLVED] yum install php-mysql create problem to my running mysql database jsaravana87 Linux - Server 1 08-11-2011 03:24 AM
How can i copt the MYSQL Database to a other server (Database) halvorls Linux - Server 3 07-27-2008 09:19 PM
LXer: Visual MySQL Database Design in MySQL Workbench LXer Syndicated Linux News 0 06-12-2008 07:00 PM
Writing an app that uses a mysql database without installing mysql server? QtCoder Programming 4 08-09-2004 02:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration