MySQL數(shù)據(jù)庫(kù)改名的三種實(shí)現(xiàn)方式
如果表是 MyISAM 那么可以直接去到數(shù)據(jù)庫(kù)目錄 mv 就可以。
Innodb完全不行,自己測(cè)試過(guò),會(huì)提示相關(guān)表不存在。
第一種方法
RENAME database olddbname TO newdbname
這個(gè)語(yǔ)法在 mysql-5.1.7 中被添加進(jìn)來(lái),到了mysql-5.1.23又去掉了,官方不推薦,會(huì)有丟失數(shù)據(jù)的危險(xiǎn)!
第二種方法
- 1.創(chuàng)建需要改成新名的數(shù)據(jù)庫(kù)。
- 2.mysqldum 導(dǎo)出要改名的數(shù)據(jù)庫(kù)
- 3.刪除原來(lái)的舊庫(kù)(確定是否真的需要)
當(dāng)然這種方法雖然安全,但是如果數(shù)據(jù)量大,會(huì)比較耗時(shí)。
#創(chuàng)建數(shù)據(jù)庫(kù) CREATE DATABASE IF NOT EXISTS `庫(kù)名` default charset utf8mb4 COLLATE utf8mb4_unicode_ci; # 將db1庫(kù)備份到db1.sql文件 mysqldump -u root -p db1 > /tmp/db1.sql; #Enter password: #[root@xxx ~]# # 導(dǎo)入備份文件到新庫(kù)db2 mysql -u root -p db2 < /tmp/db1.sql; #Enter password: #[root@xxx ~]# # 刪除舊庫(kù)(如果真的需要) DROP DATABASE db1;
第三種方法
我這里就用一個(gè)腳本,很簡(jiǎn)單,相信大家都看的懂
newdatabase.sh
#!/bin/bash # 假設(shè)將db1數(shù)據(jù)庫(kù)名改為db2 # MyISAM直接更改數(shù)據(jù)庫(kù)目錄下的文件即可 # 下面腳本是創(chuàng)建新數(shù)據(jù)庫(kù),獲取舊數(shù)據(jù)庫(kù)所有表名,將其循環(huán)移動(dòng)到新數(shù)據(jù)庫(kù) mysql -uroot -p123456 -e 'create database if not exists db2' list_table=$(mysql -uroot -p123456 -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='db1'") for table in $list_table do mysql -uroot -p123456 -e "rename table db1.$table to db2.$table" done
-p123456腳本中把明文密碼暴露出來(lái)。。。
[root@xxx script]# mysql --version mysql Ver 8.0.35 for Linux on x86_64 (MySQL Community Server - GPL) [root@xxx script]# bash newdatabase.sh mysql: [Warning] Using a password on the command line interface can be insecure. mysql: [Warning] Using a password on the command line interface can be insecure. mysql: [Warning] Using a password on the command line interface can be insecure. mysql: [Warning] Using a password on the command line interface can be insecure. #mysql:[警告]在命令行界面使用密碼可能是不安全的。
執(zhí)行成功了但不安全,我們做下調(diào)整:
- 生成密文 sginpwd.sh
#!/bin/bash function b64Code() { #參數(shù)1: 需要加密的串 passauth=$1 #參數(shù)2: 加密的次數(shù),次數(shù)越多密碼越長(zhǎng) for i in `seq 1 $2` do #python2 passauth=`echo $passauth |python -c "import base64;s=raw_input();print(base64.b64encode(s))"` #python3 #passauth=`echo $passauth |python -c "import base64;s=input(); print(base64.b64encode(s.encode()).decode());"` echo "[+]第${i}次加密結(jié)果:$passauth" done } #執(zhí)行 b64Code 加密方法,使用明文密碼加密10次 b64Code 123456 10 #密碼是字符串加上單引號(hào)
- 執(zhí)行sginpwd.sh
[root@xxx script]# bash sginpwd.sh [+]第1次加密結(jié)果:MTIzNDU2 [+]第2次加密結(jié)果:TVRJek5EVTI= [+]第3次加密結(jié)果:VFZSSmVrNUVWVEk9 [+]第4次加密結(jié)果:VkZaU1NtVnJOVVZXVkVrOQ== [+]第5次加密結(jié)果:VmtaYVUxTnRWbkpPVlZaWFZrVnJPUT09 [+]第6次加密結(jié)果:Vm10YVlWVXhUblJXYmtwUFZsWmFXRlpyVm5KUFVUMDk= [+]第7次加密結(jié)果:Vm0xMFlWbFdWWGhVYmxKWFltdHdVRlpzV21GWFJscHlWbTVLVUZWVU1Eaz0= [+]第8次加密結(jié)果:Vm0weE1GbFdiRmRXV0doVllteEtXRmx0ZEhkVlJscHpWMjFHV0ZKc2NIbFdiVFZMVlVaV1ZVMUVhejA9 [+]第9次加密結(jié)果:Vm0wd2VFMUdiRmRpUm1SWFYwZG9WbGx0ZUV0WFJteDBaRWhrVmxKc2NIcFdNakZIVjBaS2MyTkliRmRpVkZaTVZsVmFWMVpWTVVWaGVqQTk= [+]第10次加密結(jié)果:Vm0wd2QyVkZNVWRpUm1ScFVtMVNXRll3Wkc5V2JHeDBaVVYwV0ZKdGVEQmFSV2hyVm14S2MyTkljRmROYWtaSVZqQmFTMk15VGtsaVJtUnBWa1phVFZac1ZtRldNVnBXVFZWV2FHVnFRVGs9
- newdatabase_beff.sh
#!/bin/bash # 假設(shè)將db1數(shù)據(jù)庫(kù)名改為db2 # MyISAM直接更改數(shù)據(jù)庫(kù)目錄下的文件即可 # 下面腳本是創(chuàng)建新數(shù)據(jù)庫(kù),獲取舊數(shù)據(jù)庫(kù)所有表名,將其循環(huán)移動(dòng)到新數(shù)據(jù)庫(kù) function deCode() { #參數(shù)1: 加密串內(nèi)容 passDstr=$1 #參數(shù)2: 密碼解析次數(shù) for i in `seq 1 $2` do passDstr=`echo $passDstr |python -c "import base64;s=input(); print(base64.b64decode(s.encode()).decode());"` done } #b64Code方法得到的密文放在這里 mysqlPass="Vm0wd2QyVkZNVWRpUm1ScFVtMVNXRll3Wkc5V2JHeDBaVVYwV0ZKdGVEQmFSV2hyVm14S2MyTkljRmROYWtaSVZqQmFTMk15VGtsaVJtUnBWa1phVFZac1ZtRldNVnBXVFZWV2FHVnFRVGs9" #使用加密10次的密文 解密 deCode $mysqlPass 10 mysql -uroot -p"${passDstr}" -e "show databases;" mysql -uroot -p"${passDstr}" -e 'create database if not exists db2' list_table=$(mysql -uroot -p"${passDstr}" -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='db1'") for table in $list_table do mysql -uroot -p"${passDstr}" -e "rename table db1.$table to db2.$table" done function deCode() { #參數(shù)1: 加密串內(nèi)容 passDstr=$1 #參數(shù)2: 密碼解析次數(shù) for i in `seq 1 $2` do #python2 passDstr=`echo $passDstr |python -c "import base64;s=raw_input();print(base64.b64decode(s))"` #python3 #passDstr=`echo $passDstr |python -c "import base64;s=input(); print(base64.b64decode(s.encode()).decode());"` done } #b64Code方法得到的密文放在這里 mysqlPass="Vm0wd2QyVkZNVWRpUm1ScFVtMVNXRll3Wkc5V2JHeDBaVVYwV0ZKdGVEQmFSV2hyVm14S2MyTkljRmROYWtaSVZqQmFTMk15VGtsaVJtUnBWa1phVFZac1ZtRldNVnBXVFZWV2FHVnFRVGs9" #使用加密10次的密文 解密 deCode $mysqlPass 10 mysql -uroot -p"${passDstr}" -e "show databases;" mysql -uroot -p"${passDstr}" -e 'create database if not exists db2' list_table=$(mysql -uroot -p"${passDstr}" -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='db1'") for table in $list_table do mysql -uroot -p"${passDstr}" -e "rename table db1.$table to db2.$table" done
[root@xxx script]# bash newdatabase_beff.sh mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | db1 | | db2 | +--------------------+ mysql: [Warning] Using a password on the command line interface can be insecure. mysql: [Warning] Using a password on the command line interface can be insecure. mysql: [Warning] Using a password on the command line interface can be insecure. mysql: [Warning] Using a password on the command line interface can be insecure. #登錄mysql,可以看到db1數(shù)據(jù)表遷移到db2了 mysql> use db1; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +---------------+ | Tables_in_db1 | +---------------+ | active_log | | new | +---------------+ 2 rows in set (0.00 sec) mysql> show tables; Empty set (0.00 sec) mysql> use db2; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +---------------+ | Tables_in_db2 | +---------------+ | active_log | | new | +---------------+ 2 rows in set (0.00 sec)
這里用到了rename table,改表名的命令,但是如果新表名后面加數(shù)據(jù)庫(kù)名,就會(huì)將老數(shù)據(jù)庫(kù)的表移動(dòng)到新的數(shù)據(jù)庫(kù),所以,這種方法即安全,又快速。
番外:所有表是 MyISAM 修改數(shù)據(jù)庫(kù)名
1.先停止數(shù)據(jù)庫(kù)服務(wù)
systemctl stop mysqld #service mysql stop
2.到數(shù)據(jù)庫(kù)目錄 mv
數(shù)據(jù)庫(kù)目錄默認(rèn)在/var/lib/mysql/,可以通過(guò) find / -name mysql
查找
mv /var/lib/mysql/old_database /var/lib/mysql/new_database
3.啟動(dòng)數(shù)據(jù)庫(kù)服務(wù)
systemctl start mysqld #service mysql start
4.登錄mysql
如果顯示new_database數(shù)據(jù)庫(kù),說(shuō)明修改成功數(shù)據(jù)庫(kù)名了
show databases;
最終結(jié)果目錄改了,數(shù)據(jù)庫(kù)沒(méi)顯示,操作失敗不知道啥原因,以后再看吧
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mysql?InnoDB引擎中頁(yè)目錄和槽的查找過(guò)程
這篇文章主要為大家介紹了Mysql?InnoDB引擎中頁(yè)目錄和槽的查找記錄過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05解說(shuō)mysql之binlog日志以及利用binlog日志恢復(fù)數(shù)據(jù)的方法
下面小編就為大家?guī)?lái)一篇解說(shuō)mysql之binlog日志以及利用binlog日志恢復(fù)數(shù)據(jù)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03mysql+shardingSphere的分庫(kù)分表實(shí)現(xiàn)示例
分庫(kù)分表是一種場(chǎng)景解決方案,它的出現(xiàn)是為了解決一些場(chǎng)景問(wèn)題的,本文主要介紹了mysql+shardingSphere的分庫(kù)分表實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以2024-04-04mysql查詢(xún)FIND_IN_SET?REGEXP實(shí)踐示例
這篇文章主要為大家介紹了mysql查詢(xún)FIND_IN_SET?REGEXP實(shí)踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05