Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Three methods of renaming mysql Database

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

When I went to the interview not long ago, I was asked how to change the database name of the table of the Innodb engine. At that time, I only answered how to change the MyISAM. I was defeated by some detailed questions.

If it means MyISAM, then you can go directly to the database directory mv.

Innodb does not work at all. If you have tested it yourself, you will be prompted that the relevant table does not exist.

The first method:

RENAME database olddbname TO newdbname

This is available in versions 5.1.7 to 5.1.23, but it is not officially recommended and there is a risk of data loss.

The second method:

1. Create a database that needs to be changed to a new name.

2.mysqldum exports the database to be renamed

3. Delete the original old library (determine if it is really needed)

Of course, this method is safe, but if the amount of data is large, it will be time-consuming. Alas, I didn't even think of this method at that time, and I really wanted to die.

The third method:

I'm going to use a script here. It's very simple. I'm sure everyone can understand it.

Copy the code

#! / bin/bash# assume that you can change the name of the sakila database to new_sakila# MyISAM and directly change the files in the database directory mysql-uroot-p123456-e 'create database if not exists new_sakila'list_table=$ (mysql-uroot-p123456-Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='sakila'") for table in $list_tabledo mysql-uroot-p123456-e "rename table sakila.$table to new_sakila.$table" done

Rename table, the command to change the table name, is used here, but if the new table name is followed by a database name, the table from the old database will be moved to the new database, so this method is safe and fast.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report