本文目錄一覽:
怎樣在mysql裏面修改數據庫名稱?
被取消的命令MySQL 之前提供了一個 rename database db_old to db_new 的命令來直接對數據庫改名,可能由於實現的功能不完備(比如,這條命令可能是一個超大的事務,或者是由於之前的表很多還是 MyISAM 等),後來的版本直接取消了這條命令。更改數據庫名大致上有以下幾種方案:
一、mysqldump 導入導出要說最簡單的方法,就是直接用 mysqldump 工具,在舊庫導出再往新庫導入(最原始、最慢、最容易想到)的方法:舊庫 yttdb_old 導出(包含的對象:表、視圖、觸發器、事件、存儲過程、存儲函數)
二、改整庫的表名利用 MySQL 更改表名的方法來批量把舊庫的所有表依次遍歷,改名為新庫的表。這種方法比第一種要快很多倍,但是沒有第一步操作起來那麼順滑,不能一步到位。比如,要把數據庫 yttdb_old 改名為 yttdb_new,如果數據庫 yttdb_old 里只有磁盤表,那很簡單,直接改名即可。或者寫個腳本來批量改,非常簡單。但是一般舊庫里不只有磁盤表,還包含其他各種對象。這時候可以先考慮把舊庫的各種對象導出來,完了在逐一改完表名後導進去。
三、歷史方案其實在 MySQL 早期還有一種方法。假設 MySQL 部署好了後,所有的 binlog 都有備份,並且二進制日誌格式還是 statement 的話,那就可以簡單搭建一台從機,讓它慢慢追主機到新的庫名,等確切要更改舊庫的時候,再直接晉陞從機為主機即可。這裡只需要從機配置一個參數來把舊庫指向為新庫:replicate-rewrite-db=yttdb_old-yttdb_new不過這種局限性很大,不具備標準化,不推薦。
總結其實針對 MySQL 本身改庫名,大致就這麼幾種方法:
如果數據量小,推薦第一種;
數據量大,則推薦第二種;
數據量巨大,那就非 MySQL 本身能解決的了。
可通過部署第三方 ETL 工具,通過解析 MySQL 二進制日誌或其他的方式來把舊庫數據直接讀取到新庫達到改名的目的等等。
mysql如何變更數據庫實例名
常見的主要有三種方法:
如果所有表都是MyISAM類型的話,可以直接修改文件夾的名字。關閉mysql→把data目錄中的db_name目錄重命名為new_db_name→開啟mysql
新建數據庫,在新的數據庫里重命名所有舊數據庫中的表,再刪除舊的數據庫。具體操作命令如下:創建新的數據庫→重命名數據表名稱→刪除舊的數據庫。
CREATE DATABASE new_db_name;RENAME TABLE db_name.table1 TO new_db_name.table1,db_name.table2 TO new_db_name.table2;DROP DATABASE db_name;
利用mysqldump命令從舊的數據導出數據,再導入新數據庫。具體操作命令如下:導出數據→創建新的數據庫→導入數據→刪除舊的數據庫。
mysqldump -u root -p -h ip db_name db_name_dump.SQLmysql -u root -p -h ip -e 「CREATE DATABASE new_db_name」mysql -u root -p -h ip new_db_name db_name_dump.SQLmysql -u root -p -h ip -e 「DROP DATABASE db_name」
mysql存儲過程及流程控制?
存儲過程(Stored
Procedure)是一組為了完成特定功能的SQL語句集功能是將常用或複雜的工作,預先用SQL語句寫好並用一個指定名稱存儲起來,
以後需要數據庫提供與已定義好的存儲過程的功能相同的服務時,只需調用
call
存儲過程名字,
即可自動完成命令。存儲過程是由流控制和SQL語句書寫的過程,這個過程經編譯和優化後存儲在數據庫服務器中,可由應用程序通過一個調用來執行,而且允許用戶聲明變量
。同時,存儲過程可以接收和輸出參數、返回執行存儲過程的狀態值,也可以嵌套調用。
mysql 存儲過程
你應該在做統計吧,估計你不會的就是mysql存儲過程的語法 我之前也寫過 很是鬱悶 我給你一段代碼 是我用mysql寫過的一個存儲過程 你看看 主要是了解裏面的語法 看懂了 你所說的需求並不難
有看不懂的地方一起討論 :
begin
declare tikk datetime ;
declare done int default 0;
declare userid int default 0;
declare moduleid int default 0;
declare couid int default 0;
declare mduname varchar(255) ;
declare opsid int default 0;
declare c1 cursor for Select I_userID,I_operationID from space_operation_record where status0 group by I_userID,I_operationID order by createtime desc;
declare continue handler for sqlstate ‘02000’ set done =1;
set tikk = now();
open c1;
repeat
fetch c1 into userid, opsid;
if not done then
select I_moduleID from space_operation where status0 and ID=opsid into moduleid;
if moduleid ” then
select Nvc_identification from space_operation where status0 and ID=opsid into @identiftion;
if moduleid 0 then
Select Nvc_ename from space_module where status0 and ID=moduleid into mduname;
else
set mduname = ‘space’;
end if;
create temporary table if not exists sp_tab1(id bigint(20),Nvc_content MEDIUMTEXT,I_obyuID bigint(20),I_tID bigint(20),createtime datetime);
INSERT INTO sp_tab1 Select ID,Nvc_content,I_objectID,I_tmID,createtime from space_operation_record where status0 and I_operationID=opsid and I_userID=userid ;
select count(*) from sp_tab1 into couid;
set @ihod = 0;
set @listp = ”;
set @listpp = ”;
set @content0p = ”;
set @content0 = ”;
while couid 0 do
select ID,Nvc_content,I_obyuID,createtime,I_tID into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where ID @ihod order by ID asc limit 0,1;
if @iok ” then
if mduname = ‘blog’ then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,tikk);
elseif mduname = ‘team’ then
if(@identiftion = ‘addblog’ || @identiftion = ‘mdyblog’) then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listpp = CONCAT(@listpp,CONCAT(@objiplk,’,’));
set @operarry1p = substring_index(@conuiy,’|’,1);
set @operarry2p = substring_index(@conuiy,’|’,-1);
set @content0p = CONCAT(@content0p,CONCAT(@operarry2p,SPACE(1)));
set @objlistp = substring(@listpp,1,length(@listpp)-1);
end if;
elseif mduname = ‘space’ then
if(@identiftion = ‘headphoto’ || @identiftion = ‘status’) then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listppr = CONCAT(@listppr,CONCAT(@objiplk,’,’));
set @operarry1pr = substring_index(@conuiy,’|’,1);
set @operarry2pr = substring_index(@conuiy,’|’,-1);
set @content0pr = CONCAT(@content0pr,CONCAT(@operarry2pr,SPACE(1)));
set @objlistpr = substring(@listppr,1,length(@listppr)-1);
end if;
else
set @listp = CONCAT(@listp,CONCAT(@objiplk,’,’));
set @operarry1 = substring_index(@conuiy,’|’,1);
set @operarry2 = substring_index(@conuiy,’|’,-1);
set @content0 = CONCAT(@content0,CONCAT(@operarry2,SPACE(1)));
set @objlist = substring(@listp,1,length(@listp)-1);
end if;
set @ihod = @iok;
end if;
set couid = couid -1;
end while;
if @content0 ” then
set @contentp = CONCAT(@operarry1,concat(‘|’,@content0));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej ” then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlist);
end if;
end if;
if @content0p ” then
if @identiftion = ‘addphoto’ then
set @contentp = CONCAT(@operarry1p,CONCAT(‘|’,@content0p));
else
set @contentp = CONCAT(@operarry1p,CONCAT(@content0p,’|’));
end if;
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej ” then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
if @content0pr ” then
set @contentp = CONCAT(@operarry1p,concat(‘|’,@content0pr));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej ” then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
delete from sp_tab1;
end if;
end if;
until done end repeat;
close c1;
drop temporary table if exists sp_tab1 ;
UPDATE space_operation_play SET status=0;
UPDATE space_operation_display SET status=0;
Select createtime into @ptimes from space_operation_stat where status0 order by createtime desc limit 0,1;
if @ptimes ” then
create temporary table if not exists sp_tab2(id bigint(20),Nvc_content MEDIUMTEXT,I_userID bigint(20),I_lyuID bigint(20),D_stattime datetime);
INSERT INTO sp_tab2 Select ID,Nvc_content,I_userID,I_tmID,D_stattime from space_operation_stat where status0 and createtime=@ptimes order by D_stattime desc limit 0,30;
select count(*) from sp_tab2 into @cou1id;
set @uoj = 0;
while @cou1id 0 do
select ID,Nvc_content,I_userID,D_stattime,I_lyuID into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where ID @uoj order by ID asc limit 0,1;
if @io1k ” then
INSERT INTO space_operation_play(I_statID,Nvc_content,D_stattime,I_userID,Createtime,I_tmID) VALUES (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa);
set @uoj = @io1k;
end if;
set @cou1id = @cou1id -1;
end while;
drop temporary table if exists sp_tab2 ;
end if;
end
mysql 存儲過程 DDL 參數
MySQL8.0 開始支持原⼦ DDL(atomic DDL),數據字典的更新,存儲引擎操作,寫⼆進制日誌結合成了一個事務。在沒有原⼦DDL之前,DROP TABLE test1,test2;如遇到server crash,可能會有test1被drop了,test2沒有被drop掉。下面來看下在MySQL8.0之前和MySQL8.0 數據字典的區別
在MySQL8.0 之前,Data Dictionary除了存在與.FRM, .TRG, .OPT ⽂件外,還存在於系統表中(MyISAM ⾮事務引擎表中),在MySQL8.0 ,Data Dictionary 全部存在於Data Dictionary Storage Engine(即 InnoDB表中),這使crash recovery 維持原⼦性成為了可能
存儲引擎⽀持
目前,只有InnoDB存儲引擎⽀持原子DDL,為了實現原子DDL,Innodb要寫DDL logs 到 mysql.innodb_ddl_log 表,這是⼀個隱藏在mysql.ibd 數據字典表空間⾥的數據字典表。要看mysql.innodb_ddl_log 中的內容,需要
SET GLOBAL LOG_ERROR_VERBOSITY=3;(MySQL 8.0 默認為2,error log 記錄Errors and
warnings,不不記錄notes)
SET GLOBAL innodb_print_ddl_logs=1;
CREATE TABLE t1 (c1 INT) ENGINE = InnoDB;
查看error log
[Note] [MY-011066] InnoDB: DDL log insert : [DDL record: DELETE SPACE, id=30,
thread_id=25, space_id=9, old_file_path=./test/t1.ibd]
[Note] [MY-011066] InnoDB: DDL log delete : by id 30
[Note] [MY-011066] InnoDB: DDL log insert : [DDL record: REMOVE CACHE, id=31,
thread_id=25, table_id=1066, new_file_path=test/t1]
[Note] [MY-011066] InnoDB: DDL log delete : by id 31
[Note] [MY-011066] InnoDB: DDL log insert : [DDL record: FREE, id=32, thread_
id=25, space_id=9, index_id=143, page_no=4]
[Note] [MY-011066] InnoDB: DDL log delete : by id 32
[Note] [MY-011066] InnoDB: DDL log post ddl : begin for thread id : 25
[Note] [MY-011066] InnoDB: DDL log post ddl : end for thread id : 25
原子DDL 操作步驟
準備:創建所需的對象並將DDL⽇志寫入 mysql.innodb_ddl_log表中。DDL日誌定義了如何前滾和回滾DDL操作。
執行:執⾏DDL操作。例如,為CREATE TABLE操作執⾏創建。
提交:更新數據字典並提交數據字典事務。
Post-DDL:重播並從mysql.innodb_ddl_log表格中刪除DDL⽇志。為確保回滾可以安全執⾏⽽不引⼊不⼀致性,在此最後階段執⾏⽂件操作(如重命名或刪除數據文件)。這一階段還從 mysql.innodb_dynamic_metadata的數據字典表刪除的動態元數據為了DROP TABLE,TRUNCATE和其它重建表的DDL操作。
⽆論事務是提交還是回滾,DDL日誌都會mysql.innodb_ddl_log在Post-DDL階段重播並從表中刪除 。mysql.innodb_ddl_log如果服務器在DDL操作期間暫停,DDL⽇志應該只保留在表中。在這種情況下,DDL⽇志會在恢復後重播並刪除。
在恢復情況下,當服務器重新啟動時,可能會提交或回退DDL事務。如果在重做⽇志和⼆進制日誌中存在DDL操作的提交階段期間執⾏的數據字典事務,則該操作被認為是成功的並且被前滾。否則,在InnoDB重放數據字典重做日誌時回滾不完整的數據字典事務 ,並且回滾DDL事務。
原⼦DDL ⽀持類型
• DROP TABLES , all tables dropped or none
• DROP SCHEMA, all entities in the schema are dropped, or none
• Note that atomic DDL statements will be rolled back or committed even in case of crash, e.g. RENAME TABLES
• CREATE TABLE would be successfully committed or rolled back (no orphan ibd left)
• TRUNCATE TABLE (including InnoDB tables with FTS AUX tables) would be successfully committed or rolled back
• RENAME TABLES, all or none
• ALTER TABLE successful or not done
示例
請點擊輸入圖片描述
請點擊輸入圖片描述
結論
在MySQL8.0之前,alter table 操作在server crash的情況下,會遺留.frm,.ibd文件。MySQL8.0 能實現原⼦DDL(包括 DROP TABLE, DROP SCHEMA, CREATE TABLE, TRUNCATE TABLE, ALTER TABLE),alter table 操作,在server crash的情況下,不會遺留.frm,.ibd臨時文件。讓我們⼀起期待MySQL8.0 GA的到來吧!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/237626.html