最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL數(shù)據(jù)庫改名的三種實現(xiàn)方式

 更新時間:2024年06月13日 14:50:04   作者:程序員達(dá)芬奇  
這篇文章主要介紹了MySQL數(shù)據(jù)庫改名的三種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

如果表是 MyISAM 那么可以直接去到數(shù)據(jù)庫目錄 mv 就可以。

Innodb完全不行,自己測試過,會提示相關(guān)表不存在。

第一種方法

RENAME database olddbname TO newdbname

這個語法在 mysql-5.1.7 中被添加進(jìn)來,到了mysql-5.1.23又去掉了,官方不推薦,會有丟失數(shù)據(jù)的危險!

第二種方法

  • 1.創(chuàng)建需要改成新名的數(shù)據(jù)庫。
  • 2.mysqldum 導(dǎo)出要改名的數(shù)據(jù)庫
  • 3.刪除原來的舊庫(確定是否真的需要)

當(dāng)然這種方法雖然安全,但是如果數(shù)據(jù)量大,會比較耗時。

#創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE IF NOT EXISTS `庫名` default charset utf8mb4 COLLATE utf8mb4_unicode_ci;

# 將db1庫備份到db1.sql文件
mysqldump -u root -p db1 > /tmp/db1.sql;

#Enter password: 
#[root@xxx ~]# 

# 導(dǎo)入備份文件到新庫db2
mysql -u root -p db2 < /tmp/db1.sql;

#Enter password: 
#[root@xxx ~]# 


# 刪除舊庫(如果真的需要)
DROP DATABASE db1;

第三種方法

我這里就用一個腳本,很簡單,相信大家都看的懂

newdatabase.sh

#!/bin/bash
# 假設(shè)將db1數(shù)據(jù)庫名改為db2
# MyISAM直接更改數(shù)據(jù)庫目錄下的文件即可
# 下面腳本是創(chuàng)建新數(shù)據(jù)庫,獲取舊數(shù)據(jù)庫所有表名,將其循環(huán)移動到新數(shù)據(jù)庫

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腳本中把明文密碼暴露出來。。。

[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ù)越多密碼越長
    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
#密碼是字符串加上單引號
  • 執(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ù)庫名改為db2
# MyISAM直接更改數(shù)據(jù)庫目錄下的文件即可
# 下面腳本是創(chuàng)建新數(shù)據(jù)庫,獲取舊數(shù)據(jù)庫所有表名,將其循環(huán)移動到新數(shù)據(jù)庫

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ù)庫名,就會將老數(shù)據(jù)庫的表移動到新的數(shù)據(jù)庫,所以,這種方法即安全,又快速。

番外:所有表是 MyISAM 修改數(shù)據(jù)庫名

1.先停止數(shù)據(jù)庫服務(wù)

systemctl stop mysqld
#service mysql stop

2.到數(shù)據(jù)庫目錄 mv

數(shù)據(jù)庫目錄默認(rèn)在/var/lib/mysql/,可以通過 find / -name mysql 查找

mv /var/lib/mysql/old_database /var/lib/mysql/new_database

3.啟動數(shù)據(jù)庫服務(wù)

systemctl start mysqld
#service mysql start

4.登錄mysql

如果顯示new_database數(shù)據(jù)庫,說明修改成功數(shù)據(jù)庫名了

show databases;

最終結(jié)果目錄改了,數(shù)據(jù)庫沒顯示,操作失敗不知道啥原因,以后再看吧

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

汉寿县| 盘山县| 榆林市| 绵竹市| 永嘉县| 苍南县| 中江县| 城步| 集安市| 保德县| 涪陵区| 萨嘎县| 韶山市| 怀仁县| 唐河县| 浪卡子县| 景泰县| 宜阳县| 靖边县| 益阳市| 邵武市| 蒲城县| 龙井市| 多伦县| 鞍山市| 吉林市| 呼伦贝尔市| 土默特右旗| 岗巴县| 铜鼓县| 临海市| 紫云| 灵丘县| 井研县| 泸州市| 淮安市| 理塘县| 大安市| 嘉祥县| 新巴尔虎左旗| 元阳县|