基本步驟如下:
一、創建包含sql命令的sql腳本文件
文件中包含一系列的sql語句,每條語句最後以;結尾,假設文件名為ss.sql,文件內容示例如下(使用「--」進行注釋):--創建表create table 表名稱(Guid Varchar(38) not null primary key,
Title Varchar(255),
) TYPE=InnoDB;--在表A中增加欄位Status
alter table A add Status TinyInt default '0';--在表A上創建索引
create index XX_TaskId_1 on A(Id_);
--在A表中添加一條記錄
Insert into A (Id,ParentId, Name) values(1,0,'名稱');
--添加、修改、刪除數據後,有可能需要提交事務
Commit;二、執行sql腳本文件方法一 使用cmd命令執行(以windows為例,unix或linux的在其類似的控制台下運行)
格式:【Mysql的bin目錄】\mysql –u用戶名 –p密碼 –D資料庫<【sql腳本文件路徑全名】
示例:D:\mysql\bin\mysql –uroot –p123456 -Dtest<d:\test\ss.sql注意事項:1、如果在sql腳本文件中使用了use 資料庫,則-D資料庫選項可以忽略
2、如果【Mysql的bin目錄】中包含空格,則需要使用「」包含,如:
「C:\Program Files\mysql\bin\mysql」 –u用戶名 –p密碼 –D資料庫<【sql腳本文件路徑全名】
3、如果需要將執行結果輸出到文件,可以採用以下模式
D:\mysql\bin\mysql –uroot –p123456 -Dtest<d:\test\ss.sql>d:\dd.txt
方法二 進入mysql的控制台後,使用source命令執行
1、進入mysql控制台
D:\mysql\bin\mysql –uroot –p123456 -Dtest
2、執行sql腳本文件
② 如何執行一個mysql的sql腳本文件
基本步驟如下: 1、創建包含sql命令的sql腳本文件 文件中包含一些列的sql語句,每條語句最後以;結尾,文件內容示例如下: --創建表,使用「--」進行注釋 create table 表名稱(Guid Varchar(38) not null primary key, Title Varchar(255), ) TYPE=InnoDB; --在表A中增加欄位Status alter table A add Status TinyInt default '0'; --在表A上創建索引 create index XX_TaskId_1 on A(Id_); --在表A中添加一條記錄 Insert into A (Id,ParentId, Name) values(1,0,'名稱'); --添加、修改、刪除數據後,有可能需要提交事務Commit;2、執行sql腳本文件 方法一 使用cmd命令執行(windows下,unix或linux在的其控制台下) 【Mysql的bin目錄】\mysql –u用戶名 –p密碼 –D資料庫<【sql腳本文件路徑全名】,示例: D:\mysql\bin\mysql –uroot –p123456 -Dtest<d:\test\ss.sql注意:A、如果在sql腳本文件中使用了use 資料庫,則-D資料庫選項可以忽略 B、如果【Mysql的bin目錄】中包含空格,則需要使用「」包含,如:「C:\Program Files\mysql\bin\mysql」 –u用戶名 –p密碼 –D資料庫<【sql腳本文件路徑全名】 方法二 進入mysql的控制台後,使用source命令執行
③ linux中怎麼從mysql中導出sql腳本
首先你需要一個mysql的帳號,比如root/root
然後mysqlmp -uroot -proot 資料庫名 > db_name.sql
這樣你的資料庫就導成sql腳本了
如果解決了您的問題請採納!
如果未解決請繼續追問!
④ linux 怎麼執行sql
要看你有沒有設資料庫bin目錄的環境變數 如果設置了就直接可以用,如果沒設置你就:
1.切換工作目錄到mysql(或其他資料庫產品)下,用root用戶執行 sudo bin/mysqld_safe --user root &(這個符號表示從後台啟動) 
2.然後再切換到bin目錄下工作 執行./mysql -u 用戶名 -p
3.終端會提示你輸入密碼
⑤ linux shell腳本執行mysql mysql資料庫在遠程伺服器上
方法一、適合所有格式的mysql資料庫,通過對資料庫導出導進寫個腳本定時執行:
1.導出整個資料庫 mysqlmp -u 用戶名 -p 資料庫名 > 導出的文件名     mysqlmp -u wcnc -p smgp_apps_wcnc > /存放路徑/wcnc.sql
2.導出一個表 mysqlmp -u 用戶名 -p 資料庫名 表名> 導出的文件名 mysqlmp -u wcnc -p smgp_apps_wcnc users> /存放路徑/wcnc_users.sql
3.導出一個資料庫結構  mysqlmp -u wcnc -p -d --add-drop-table smgp_apps_wcnc >/存放路徑/wcnc_db.sql 
定義:
-d 沒有數據
--add-drop-table 在每個create語句之前增加一個drop table 
4.導入資料庫  常用source 命令  進入mysql資料庫控制台:
如mysql -u root -p mysql>use 資料庫
方法二、針對mysql數據表格式為MyISAM的
假如數據文件在/var/lib/mysql
那麼直接寫個腳本
cp -r /var/lib/mysql /備份到的文件夾路徑
隔機備份用rsync增量,或定時完整備份。
⑥ 如何在linux中執行sql文件
如何在linux中執行sql文件
第一種方法:
在命令行下(未連接資料庫),輸入 mysql -h localhost -u root -p123456 < F:\hello world\niuzi.sql (注意路徑不用加引號的!!) 回車即可.
第二種方法:
在命令行下(已連接資料庫,此時的提示符為 mysql> ),輸入 source F:\hello world\niuzi.sql (注意路徑不用加引號的) 
或者 \. F:\hello world\niuzi.sql (注意路徑不用加引號的) 回車即可.
⑦ 求一個linux下的shell腳本執行mysql的sql語句,但老是 不成功....大神幫忙啊...
簡單的看了一下
第一,第六行a= awk 'NR==2{print $1}'  testsss.log 這里是否沒有加``,另外shell下賦值在等號兩側是不允許有空格的,這個不知道是不是你貼在裡面顯示的錯誤
第二,$sqlw取出來的應該就已經是id>3的了,那麼你在for循環里的判斷if [ "$num" -gt "3" ]應該進不去,所以循環里是不會走到if里去的,你可以在if外面列印一下$num的值看看。
⑧ 如何執行一個mysql的sql腳本文件
右鍵以文本文件的形式打開,裡面是記錄著可以在mysql裡面執行的代碼。
要執行sql文件裡面的代碼,需要打開mysql資料庫去執行,新手推薦用軟體的形式執行此sql,軟體如navicat,sqlyog等。
打開軟體並且配置連接資料庫的信息後打開,然後創建一個資料庫,之後右鍵資料庫選中執行sql,接著選擇要執行的sql文件,按提示一步一步走下去即可。
老手推薦使用cmd指令來執行sql,因為速度比較快,首先也是連接上資料庫,創建並選擇好資料庫:
mysql>use
資料庫名;
mysql>set
names
utf8;
--設置傳輸編碼,避免中文亂碼
mysql>source
d:/xxx.sql;
--
source
文件路徑
就這么簡單,
然後就等著sql執行完畢即可.
⑨ linux系統下怎麼在終端運行sql語句
主要有以下幾種方法:
1、將SQL語句直接嵌入到shell腳本文件中
代碼如下:
--演示環境  
[root@SZDB ~]# more /etc/issue  
CentOS release 5.9 (Final)  
Kernel \r on an \m  
 root@localhost[(none)]> show variables like 'version';  
+---------------+------------+  
| Variable_name | Value      |  
+---------------+------------+  
| version       | 5.6.12-log |  
+---------------+------------+  
  
[root@SZDB ~]# more shell_call_sql1.sh   
#!/bin/bash  
# Define log  
TIMESTAMP=`date +%Y%m%d%H%M%S`  
LOG=call_sql_${TIMESTAMP}.log  
echo "Start execute sql statement at `date`." >>${LOG}  
  
# execute sql stat  
mysql -uroot -p123456 -e "  
tee /tmp/temp.log  
drop database if exists tempdb;  
create database tempdb;  
use tempdb  
create table if not exists tb_tmp(id smallint,val varchar(20));  
insert into tb_tmp values (1,'jack'),(2,'robin'),(3,'mark');  
select * from tb_tmp;  
notee  
quit"  
  
echo -e "\n">>${LOG}  
echo "below is output result.">>${LOG}  
cat /tmp/temp.log>>${LOG}  
echo "script executed successful.">>${LOG}  
exit;  
  
[root@SZDB ~]# ./shell_call_sql1.sh   
Logging to file '/tmp/temp.log'  
+------+-------+  
| id   | val   |  
+------+-------+  
|    1 | jack  |  
|    2 | robin |  
|    3 | mark  |  
+------+-------+  
Outfile disabled.  
2、命令行調用單獨的SQL文件
代碼如下:
[root@SZDB ~]# more temp.sql   
tee /tmp/temp.log  
drop database if exists tempdb;  
create database tempdb;  
use tempdb  
create table if not exists tb_tmp(id smallint,val varchar(20));  
insert into tb_tmp values (1,'jack'),(2,'robin'),(3,'mark');  
select * from tb_tmp;  
notee  
[root@SZDB ~]# mysql -uroot -p123456 -e "source /root/temp.sql"  
Logging to file '/tmp/temp.log'  
+------+-------+  
| id   | val   |  
+------+-------+  
|    1 | jack  |  
|    2 | robin |  
|    3 | mark  |  
+------+-------+  
Outfile disabled.  
3、使用管道符調用SQL文件
代碼如下:
[root@SZDB ~]# mysql -uroot -p123456 </root/temp.sql  
Logging to file '/tmp/temp.log'  
id      val  
1       jack  
2       robin  
3       mark  
Outfile disabled.  
 #使用管道符調用SQL文件以及輸出日誌  
[root@SZDB ~]# mysql -uroot -p123456 </root/temp.sql >/tmp/temp.log  
[root@SZDB ~]# more /tmp/temp.log  
Logging to file '/tmp/temp.log'  
id      val  
1       jack  
2       robin  
3       mark  
Outfile disabled.  
4、shell腳本中MySQL提示符下調用SQL
代碼如下:
[root@SZDB ~]# more shell_call_sql2.sh  
#!/bin/bash  
mysql -uroot -p123456 <<EOF  
source /root/temp.sql;  
select current_date();  
delete from tempdb.tb_tmp where id=3;  
select * from tempdb.tb_tmp where id=2;  
EOF  
exit;  
[root@SZDB ~]# ./shell_call_sql2.sh  
Logging to file '/tmp/temp.log'  
id      val  
1       jack  
2       robin  
3       mark  
Outfile disabled.  
current_date()  
2014-10-14  
id      val  
2       robin  
5、shell腳本中變數輸入與輸出
代碼如下:
[root@SZDB ~]# more shell_call_sql3.sh  
#!/bin/bash  
cmd="select count(*) from tempdb.tb_tmp"  
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")  
echo "Current count is : ${cnt}"  
exit   
[root@SZDB ~]# ./shell_call_sql3.sh   
Warning: Using a password on the command line interface can be insecure.  
Current count is : 3  
  
[root@SZDB ~]# echo "select count(*) from tempdb.tb_tmp"|mysql -uroot -p123456 -s  
3  
  
[root@SZDB ~]# more shell_call_sql4.sh  
#!/bin/bash  
id=1  
cmd="select count(*) from tempdb.tb_tmp where id=${id}"  
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")  
echo "Current count is : ${cnt}"  
exit   
  
[root@SZDB ~]# ./shell_call_sql4.sh   
Current count is : 1
