1. 程式人生 > >shell指令碼的那點小事兒--shell指令碼的資料庫操作(六)

shell指令碼的那點小事兒--shell指令碼的資料庫操作(六)

內容一:shell指令碼的SQL語句

1.安裝SQL

視覺化安裝,直接下載安裝了

下載完成後:

2.開啟資料庫

開啟之後

不用的時候,記得關閉資料庫

連線資料庫

終端輸入:mysql -u root -p

3.退出資料庫

登入資料庫後,檔案目錄為mysql>

mysql>exit

4.顯示資料庫

mysql>show

appstore可以下載視覺化介面

5.進入資料庫

mysql>use xxxx資料庫名;

6.刪除資料庫

mysql>drop database xxxx資料庫名;

7.建立資料庫

mysql>create database xxxx資料庫名;

8.建立資料庫表

mysql>create table tablename (s_id int(4) not null primary key, s_name char(20), gender char(2) not null default '男');

解決中文亂碼問題:用varchar代替char

sql結束,分號前面加入ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8

e.g:

mysql>create table tablename(s_id int(4) not null primary key, s_name char(20), gender char(2) not null default '男')ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

create table t_product(t_product_id varchar(64),t_product_name varchar(64),t_product_price varchar(32),t_product_desc varchar(256),t_product_image varchar(256)) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

9.檢視資料表列表

mysql>show tables;

10.插入資料

mysql>insert into tablename values(xxx, xxx, xxx)

e.g

mysql>insert into student_table values(1, '小明明', '1')

11.查詢資料表

select * from tablename

select * from student_table where s_gender <> 0

12.更新表資料

update xxxx表名 set 更新key=value where condition

e.g:

update student_table set s_name='豬豬' where s_id='1';

13.刪除資料表資料

delete from tablename where condition

e.g:

delete from student_table where s_id='1';

內容二:shell操作資料庫

1.shell指令碼登陸資料庫

指令碼程式碼:

#!/bin/bash
MYSQL=$(which mysql)
$MYSQL -u root -p

2.shell指令碼退出資料庫

指令碼程式碼:

#!/bin/bash
MYSQL=$(which mysql)
$MYSQL -u root -p -e 'exit'

總結:

$MYSQL -u root -p:登入資料庫

-e 'exit':表示執行退出命令

3.shell顯示資料庫列表

指令碼程式碼:

#!/bin/bash

MYSQL=$(which mysql)
$MYSQL -u root -p -e 'show databases'

4.shell語言查詢資料庫表

#!/bin/bash

MYSQL=$(which mysql)
$MYSQL db_temp -u root -p -e 'select * from student_table'

語法:

$MYSQYL tablename -u root -p -e 'query statement'

5.查詢資料庫資料表->開始標記和結束標記,輸入的重定向

EOF->開始標記

code

EOF->結束標記

指令碼程式碼:

#!/bin/bash

MYSQL=$(which mysql)
$MYSQL db_temp -u root -p << EOP
    select * from student_table;
EOP

6.插入表資料

6.1常規操作

#!/bin/bash

MYSQL=$(which mysql)

$MYSQL db_temp -u root -p << EOP
insert into student_table values(2, '小明明', '1');
insert into student_table values(3, '小明', '1');
insert into student_table values(4, '明明', '1');

#查詢驗證插入情況
    select * from student_table;

EOP

6.2動態的傳遞引數,並且獲取sql執行語句

#!/bin/bash

if [ $# -ne 3 ]
then
    echo "param isn't three, you must input three param"
else
    DB=$(which mysql)
    $DB db_temp -u root -p << EOF
        insert into student_table values($1, '$2', $3);

    EOF
    if [ $? -eq 0 ]
    then
        echo 'insert sccuess'
        $DB db_temp -u root -p << EOF
            select * from student_table;
        EOF
    else
        echo 'insert fales'
    fi
fi

7.插入資料和更新資料

#!/bin/bash

# 前三引數是值,第四個是條件 第五個是值
if [ $# -ne 5 ]
then
    echo "param isn't three, you must input five param"
else
    DB=$(which mysql)
    $DB db_temp -u root -p << EOF
        insert into student_table values($1, '$2', $3);
    EOF
    if [ $? -eq 0 ]
    then
        echo 'insert sccuess'
        #更新表的內容
        $DB db_temp -u root -p << EOF
            update student_table set s_gender=$5 where s_id=$4;
        EOF
        if [ $? -eq 0 ]
        then
            echo "update sccuess"
            #查詢結果 打印表資料
            $DB db_temp -u root -p << EOF
                select * from student_table;
            EOF
        else
            echo "update fales"
        fi
    else
        echo 'insert fales'
    fi
fi

8.解決登入密碼輸入問題 (這是網上一些做法,我是沒成功了,這是放出來給大家參考一下)

第一步找到mysql的配置檔案

windows:my.ini

mac OS:my.cnf

mac OS path:/usr/local/etc/my.cnf(新版路徑)

/usr/local/mysql-8.0.12-macos10.13-x86_64/support-files/my-Default.cnf(舊版路徑)

新版的mysql是沒有這個檔案在/usr/local/mysql-8.0.12-macos10.13-x86_64/support-files/中的需要手動chuang jian

第二步將檔案拷貝桌面進行修改,新增預設密碼

在[client]下面配置如下程式碼

password=your password

user=your username

然後替換檔案,重啟伺服器

9.shell指令碼使用無密碼登入

講shell指令碼中的-p去掉,-p是指定密碼

10.批量插入

#!/bin/bash

#定義域分隔符->分割字串
IFS=','
MYSQL=$(which mysql)
while read id name sex
do
    $MYSQL db_20171108 -u root << EOF
        insert into t_student(s_id, s_name, s_sex) values ($id,'$name', $sex);

    EOF
done < ${1}
#結合上一次匯出.csv處理方式

內容三:拓展知識,兩個視窗傳送訊息

1.who指令現實當前使用者

avalanching console Sep 4 09:31

avalanching ttys000 Sep 10 09:39

avalanching ttys001 Sep 10 11:42

分析結果:

param1 使用者名稱

param2 使用者所在的終端

param4 登入時間

who -T指令

avalanching - console Sep 4 09:31

avalanching + ttys000 Sep 10 09:39

avalanching + ttys001 Sep 10 11:42

"-":未開通

"+":開通

2.mesg指令檢視當前訊息許可權

is y已經開啟

is n沒有開啟

4.傳送訊息

1.連線兩個視窗

語法:write 使用者 視窗名

write avalanching ttys000

5.開啟訊息功能

mesg y

注意:以上的縮排是為了方便檢視層級關係,如果在執行程式碼中報錯(not found command),請去除縮排,注意if後的空格