Get the App
SLTechnology News&Howtos  ›  Database  › 

Rename mysql5.7 database

Shulou Source: shulou.com Published: 2022-06-01 05:09:52 09月27日 Update

The database version is 5.7.17

Today, I received a demand that I need to change the name of the database from A to B. I checked that there are less than 200 tables under the A database, which is not very big. If you can use import and export, it will still be slow, time is precious, and the less time you use, the better.

There is a rename table command, write a script to mainly use it to implement the requirements

#! / bin/bash

# first determine whether the new database exists, and create it if it does not exist.

# list the tables in the old database, where the N and s parameters play the role of formatting, which is similar to pagesize in sqlplus under Oracle

# use for loop to rename.

Mysql-uroot-pxxxxDB2017#-e'create database if not exists cus_0042'

List_table=$ (mysql-uroot-pxxxxDB2017#-Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='old_databases'")

For table in $list_table

Do

Mysql-uroot-pxxxxDB2017#-e "rename table old_databases.$table to cus_0042.$table"

Done

After the execution is successful, the old_databases is empty, but deleted.

If it is the storage engine of myisam, just mv it under the data directory.

Tags: Data database time if demand valuable large successful next function parameter name command that is engine format version directory script or Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS NVidia Docker Huawei