#!/bin/bash hostname=192.168.1.224 #数据库server信息 port=3306 username=root password=123456 dbname=test_db_name #要创建的数据库的库名称 tablename=test_table_name #要创建的数据库的表的名称 mysql_cmd=mysql -h${hostname} -p${port} -u${userna
#!/bin/bash
hostname=192.168.1.224 #数据库server信息
port=3306
username=root
password=123456
dbname=test_db_name #要创建的数据库的库名称
tablename=test_table_name #要创建的数据库的表的名称
mysql_cmd=mysql -h${hostname} -p${port} -u${username} -p${password}
echo ${mysql_cmd}
echo create database ${dbname}
create_db_sql=create database if not exists ${dbname}
echo ${create_db_sql} | ${mysql_cmd} #创建数据库
if [ $? -ne 0 ] #判断是否创建成功
then
echo create databases ${dbname} failed ...
exit 1
fi
echo create table ${tablename}
create_table_sql=create table ${tablename}(
name char(6) not null,
id int default 0
)engine=myisam default charset=latin1
echo ${create_table_sql} | ${mysql_cmd} ${dbname} #在给定的db上,创建表
if [ $? -ne 0 ] #判断是否创建成功
then
echo create table ${dbname}.${tablename} fail ...
fi
http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201141142057473/
或者如下操作
#创建数据库
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}
http://blog.csdn.net/xiaoxinghehe/article/details/8663085