MySQL 匯出匯入資料庫

2021-10-16 22:04:36 字數 2872 閱讀 2920

匯出,匯入資料庫mysqldump

使用mysql提供的mysqldump工具來匯入匯出資料庫,可以實現資料庫的備份和還原。

匯出資料庫 (備份資料庫)

匯出資料庫:mysqldump -u 使用者名稱 -p 資料庫名 > 匯出的檔名

匯入資料庫 (還原資料庫)

匯入資料庫

匯入前先模擬將要還原的資料庫刪除

匯入資料庫(還原資料庫)

或者登陸 mysql 建立資料庫

匯入進入到mysql資料庫,新增sql檔案,推薦使用此方法,安全

mysql> source /root/ha.sql #sql指令碼的路徑

擴充套件知識

上面匯出資料庫不能匯出資料庫結構,需要我們匯入資料庫時新建資料庫,可以使用-b引數在匯出資料庫時,同時匯出資料庫結構,這樣我們在匯入資料庫時不用先建立要匯入的資料庫的庫結構。

mysqldump -uroot -p123456 -b 庫名》檔案.sql

-b : 匯出整個庫包含建庫語句

-a:匯出全部資料庫

把乙個select的結果匯出到文字

語法:select * into outfile 『/tmp/123.txt』 from ha.students; 此處有個檔案訪問許可權問題,mysql使用者是可以訪問/tmp路徑的,所以這裡放到tmp下

舉例:查詢ha資料庫下students表的資料匯出到/tmp目錄下

mysql> select * into outfile 『/tmp/123.txt』 from ha.students;

error 1290 (hy000): the mysql server is running with the --secure-file-priv option so it cannot execute this statement

報錯:5.7版本匯出報錯,error 1290 (hy000): the mysql server is running with the --secure-file-priv option so it cannot execute this statement

是因為secure-file-priv 這個選項規定了匯出的目錄,檢視匯出目錄。

secure_file_priv 為 null 時,表示限制mysqld不允許匯入或匯出。

secure_file_priv 為 /tmp 時,表示限制mysqld只能在/tmp目錄中執行匯入匯出,其他目錄不能執行。

secure_file_priv 沒有值時,表示不限制mysqld在任意目錄的匯入匯出。

解決方法

開啟my.cnf在檔案末尾加入以下語句後重啟mysql。

[root@cong11 ~]# vim /etc/my.cnf

secure_file_priv=』』

重啟mysql服務

[root@cong11 ~]# /etc/init.d/mysqld restart

檢視修改結果

重新執行

mysql> select * into outfile 『/tmp/123.txt』 from ha.students;

query ok, 9 rows affected (0.02 sec)

mysql-sql語句高階

檢視字段型別:

desc 表名

邏輯運算子:

and or not

and 與

or 或

not 非

算術運算子:

= 等於

<> 不等於 !=

「> 大於」

< 小於

「>= 大於等於」

「<= 小於等於」

in 運算子

in 運算子用於 where 表示式中,以列表項的形式支援多個選擇,語法如下:

where column in (value1,value2,…)

where column not in (value1,value2,…)

not in 與in相反

當 in 前面加上 not 運算子時,表示與 in 相反的意思,即不在這些列表項內選擇。

排序公升序與降序

公升序:order by 「排序的字段」 asc 預設

降序:oredr by 「排序的字段」 desc

範圍運算:

between …and…

查詢**在30到60之間(包含30和60)的書名和**

mysql> select bname,price from books where price between 30 and 60;

between and 可以使用》=和<=的方式來代替,並且使用大於小於意義表述更明確。

mysql> select bname,price from books where price >=30 and price <=60;

查詢**不在30到60之間的書名和**

模糊匹配查詢:

欄位名 [not]like 『萬用字元』 ----》% 任意多個字元

查詢書名中包括"程式"字樣記錄

mysql> select bname from books where bname like 『%程式%』;

查詢書名中不包括"程式"字樣記錄

mysql> select bname from books where bname not like 『%程式%』;

mysql匯出 mysql資料庫匯入匯出

window下 1.匯出整個資料庫 mysqldump u 使用者名稱 p 資料庫名 匯出的檔名 2.匯出乙個表 mysqldump u 使用者名稱 p 資料庫名 表名 匯出的檔名 mysqldump u dbuser p dbname users dbname users.sql 3.匯出乙個資料...

mysql 資料庫匯入匯出

備份mysql資料庫的命令 mysqldump hhostname uusername ppassword databasename backupfile.sql 備份mysql資料庫為帶刪除表的格式 備份mysql資料庫為帶刪除表的格式,能夠讓該備份覆蓋已有資料庫而不需要手動刪除原有資料庫。mys...

mysql 資料庫匯入匯出

1.匯出整個資料庫 mysqldump u 使用者名稱 p 密碼 資料庫名 匯出的檔名 mysqldump u jason p jason roomdatadb roomdatadb.sql 2.匯出乙個表 mysqldump u 使用者名稱 p 密碼 資料庫名 表名 匯出的檔名 mysqldump...