Bootstrap

mysql交互式写shell_shell脚本与mysql交互方法汇总

有大侠知道其他方法的,还请不吝赐教,在此谢过:) shell脚本导入数据文件到mysql内

当然是利用mysqlimport啦

5385ce1c6555438693baeb65cd153360.gif 

示例脚本:

#!/bin/sh

# import test_table.sql into test_database.test_table

# content in test_table.sql like "123,abc,334; N,ddd,999"

### define constant

MYSQL_IP=192.168.0.69

MYSQL_USER=root

MYSQL_PW=root

MYSQL_DATABASE=test_database

MYSQL_TABLE=test_table

function import_from_file()

{

echo "开始导入到 ..."

echo -n -e "\t"

mysqlimport -h ${MYSQL_IP} -u${MYSQL_USER} -p${MYSQL_PW} --fields-terminated-by=',' --lines-terminated-by=';' -L ${MYSQL_DATABASE} ${MYSQL_TABLE}.sql

if [ 0 -eq $? ]; then

echo "导入完成!"

else

echo "导入过程中出现错误,错误码为:$?"

exit

fi

echo

}

# call function

import_from_file

shell脚本中使用mysql命令

方法一:

#!/bin/sh

CONNECT_MYSQL="mysql -h 192.168.0.123 -uroot -p123456 test_database"

SQL="SELECT * FROM TEST_TABLE"

echo "${SQL}" | ${CONNECT_MYSQL}

方法二:

#!/bin/sh

CONNECT_MYSQL="mysql -h 192.168.0.123 -uroot -p123456 test_database"

SQL="SELECT * FROM TEST_TABLE"

echo ${CONNECT_MYSQL} <

${SQL}

MYSQLEOF

;