代码改变世界

linux shell命令执行sql(mysql入门)

2015-01-16 14:20  out8  阅读(3247)  评论(0编辑  收藏  举报

今天做个linux下的shell操作数据库的入门例子

1.shell文件内容:

#!/bin/bash
HOSTNAME="11.8.8.87"
PORT="3306"
USERNAME="root"
PASSWORD="root"
DBNAME="testa"
TABLENAME="test"

use_db_sql="use ${DBNAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "${use_db_sql}"

insert_sql="insert into ${TABLENAME} values('7','mike','123456')"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${insert_sql}"

主机地址:11.8.8.87 端口:3306  数据库:testa 表:test

2.test表结构:

mysql> desc test;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| t_id     | int(24)     | NO   | PRI | NULL    | auto_increment |
| username | varchar(32) | YES  |     | NULL    |                |
| password | varchar(32) | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

3.我这里命名shell文件名为:mysqltestshell,在windows上编辑后上传到linux服务器上,

修改权限:chmod +x mysqltestshell

执行./mysqltestshell,

报错:-bash: ./mysqltestshell: /bin/bash^M: bad interpreter: No such file or directory

解决办法:

vi mysqltestshell

然后用命令 :set ff?

可以看到dos或unix的字样. 如果的确是dos格式的, 那么你可以用set ff=unix把它强制为unix格式的, 然后存盘退出. 再运行一遍看.

4.运行结果

请自觉忽略乱码情况(此处只是测试),可以看到新增了一条记录

mysql> select * from test;
+------+----------+----------------------+
| t_id | username | password             |
+------+----------+----------------------+
|    1 | 娴璇     | 娴璇瀵鐮             |
|    2 | ??       | 娴璇瀵鐮             |
|    3 | ??       | 忙碌?猫炉?氓炉?莽聽?         |
|    4 | ??       | ????                 |
|    5 | ??       | ????                 |
|    6 | ??       | ????                 |
|    7 | mike     | 123456               |
+------+----------+----------------------+
7 rows in set (0.00 sec)

----------------------------------------------------------补充----------------------------------------------------------------

mysql  -hhostname -Pport -uusername -ppassword  -e  相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作mysql的方法。

#!/bin/bash

HOSTNAME="192.168.111.84"                                           #数据库信息

PORT="3306"

USERNAME="root"

PASSWORD=""

DBNAME="test_db_test"                                                       #数据库名称

TABLENAME="test_table_test"                                            #数据库中表的名称

#创建数据库

create_db_sql="create database IF NOT EXISTS ${DBNAME}"

mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}"

#创建表

create_table_sql="create table IF NOT EXISTS ${TABLENAME} (  name varchar(20), id int(11) default 0 )"

mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${create_table_sql}"

#插入数据

insert_sql="insert into ${TABLENAME} values('billchen',2)"

mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${insert_sql}"

#查询

select_sql="select * from ${TABLENAME}"

mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"

#更新数据

update_sql="update ${TABLENAME} set id=3"

mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${update_sql}"

mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"

#删除数据

delete_sql="delete from ${TABLENAME}"

mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${delete_sql}"

mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"