跟散仙学shell编程(十四)

上篇散仙简单介绍了linux里面各个shell类型的使用,本篇我们来看下如何在shell里面使用数据库mysql,关于mysql的安装可以参考散仙以前的文章:
http://qindongliang.iteye.com/blog/1987199

下面看下如何在脚本里面使用mysql数据库:
首先我们使用which命令,找到mysql的启动路径
[root@h1 ~]# which mysql
/usr/bin/mysql
[root@h1 ~]# 


首先,回忆下如何登陆mysql
[root@h1 ~]# mysql root -u -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
[root@h1 ~]# service mysqld stop
停止 mysqld:                                              [确定]
[root@h1 ~]# service mysqld start
正在启动 mysqld:                                          [确定]
[root@h1 ~]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@h1 ~]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@h1 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hive               |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> exit;
Bye
[root@h1 ~]# 


在脚本里登陆代码如下:
[root@h1 826]# cat login.sh 

MYSQL=`which mysql`


$MYSQL -u root -p
[root@h1 826]# sh login.sh 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 


上面写的shell脚本,虽然可以登陆,但是由于-p命令会导致mysql停止下来输入密码,所以我们需要想一种简便的方法,来完成登陆
[root@h1 826]# cat login.sh 

MYSQL=`which mysql`


$MYSQL -u root -pqin
[root@h1 826]# sh login.sh 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

密码直接写在命令行,很不安全,所以我们用mysql一个特殊的配置文件,来读取它,我们需要在$HOME的根目录下,新建一个隐藏的.my.cnf文件,里面写入密码,然后再次测试登陆发现就不需要使用密码了:

[root@h1 ~]# cat .my.cnf 
[client]
password=qin
[root@h1 ~]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

下面我们使用-e命令向mysql发送一条命令:
[root@h1 826]# cat login.sh 

#获取mysql执行路径信息
MYSQL=`which mysql`

#第一个test是连接的数据库名字
$MYSQL test   -u root -e '  select * from person '
[root@h1 826]# sh login.sh 
+----+--------+------+
| id | name   | sex  |
+----+--------+------+
|  1 | solr   | girl |
|  2 | lucene | boy  |
|  3 | hadoop | boy  |
+----+--------+------+
[root@h1 826]# 


如果我们需要发送多个命令,我们必须使用文件重定向来满足,看下面例子:
[root@h1 826]# cat login.sh 

#获取mysql执行路径信息
MYSQL=`which mysql`

#第一个test是连接的数据库名字
#$MYSQL test   -u root -e '  select * from person '
$MYSQL test   -u root <<EOF

show tables;
select * from person where sex='boy';

EOF



[root@h1 826]# sh login.sh 
Tables_in_test
person
id      name    sex
2       lucene  boy
3       hadoop  boy
[root@h1 826]# 


我们发现使用EOF重定向后,mysql程序改变了,默认的输出风格,因为mysql检测出来输入时重定向过来的,所以它只返回了原始数据,而不是在数据两边加上了ASCII符号框,这非常利于提取单独的元素。

下面看下如何在命令下添加一条数据到MySQL里:
[root@h1 826]# cat insert.sh 

#获取mysql执行路径信息
MYSQL=`which mysql`

#第一个test是连接的数据库名字
#$MYSQL test   -u root -e '  select * from person '


#定义的插入语句
insertSQL="insert into person(name,sex) values ('$1','$2')"

if [ $# -eq 2  ]  ; then


$MYSQL test   -u root <<EOF


$insertSQL


EOF

   if [ $? -eq 0  ] ;then
   echo "插入数据成功!"
   else
   echo "插入失败,请检查代码! "
    
   fi




else

echo "参数小于2,不能添加数据!"

exit;

fi











[root@h1 826]# sh insert.sh spring boy
插入数据成功!
[root@h1 826]# sh insert.sh hibernate
参数小于2,不能添加数据!
[root@h1 826]# 


执行成功后,我们在查看数据结果:
[root@h1 826]# sh login.sh 
Tables_in_test
person
id      name    sex
1       solr    girl
2       lucene  boy
3       hadoop  boy
4       java    girl
5       spring  boy
[root@h1 826]# 


发现我们的插入已经成功了,使用起来非常的简单方便,需要注意的是,我们的插入的SQL数据使用了双引号,所以我们在里面的变量值,要使用单引号括起来,否则shell将不会正确的解析我们的变量数据,最后我们使用$?来测试退出状态码,由此来检测程序是否运行成功。

下面我们在看下如何根据ID删除一条数据:
[root@h1 826]# cat deletebyID.sh 

#获取mysql执行路径信息
MYSQL=`which mysql`

#第一个test是连接的数据库名字
#$MYSQL test   -u root -e '  select * from person '


delSQL=" delete  from  person where id=$1 "



function show {


echo "========================person表所有数据================================="
$MYSQL test   -u root <<EOF

select * from person ;

EOF


}


if [ $# -eq 1  ] ; then

show;

$MYSQL test   -u root <<EOF

$delSQL

EOF






if [ $? -eq 0  ] ; then

echo "删除成功!"
show
else
echo "删除失败!"
fi



else 
 
echo "请输入要删除的ID号!"


fi



[root@h1 826]# sh deletebyID.sh 
请输入要删除的ID号!
[root@h1 826]# sh deletebyID.sh 3
========================person表所有数据=================================
id      name    sex
1       solr    girl
2       lucene  boy
3       hadoop  boy
4       java    girl
5       spring  boy
删除成功!
========================person表所有数据=================================
id      name    sex
1       solr    girl
2       lucene  boy
4       java    girl
5       spring  boy
[root@h1 826]# 




最后我们来看下,如何使用shell变量接受数据库返回结果:
[root@h1 826]# cat r.sh 

#获取mysql执行路径信息
MYSQL=`which mysql`

#第一个test是连接的数据库名字
#$MYSQL test   -u root -e '  select * from person '




  dbs=`$MYSQL test   -u root  -Bse  'show databases'   `





for db in $dbs
do

 echo "$db"



done






[root@h1 826]# sh r.sh 
information_schema
hive
mysql
test
[root@h1 826]# 



说明一下-B参数指定mysql程序工作在批处理的模式下,-s参数,列标题和格式化符号都会被禁掉

下面看下mysql导出格式化的HTML的数据:

[root@h1 826]# cat e.sh 

#获取mysql执行路径信息
MYSQL=`which mysql`

#第一个test是连接的数据库名字
#$MYSQL test   -u root -e '  select * from person '




  $MYSQL test   -u root  -H -e   'select * from person'   

















[root@h1 826]# sh e.sh 
<TABLE BORDER=1><TR><TH>id</TH><TH>name</TH><TH>sex</TH></TR><TR><TD>1</TD><TD>solr</TD><TD>girl</TD></TR><TR><TD>2</TD><TD>lucene</TD><TD>boy</TD></TR><TR><TD>4</TD><TD>java</TD><TD>girl</TD></TR><TR><TD>5</TD><TD>spring</TD><TD>boy</TD></TR></TABLE>[root@h1 826]# 



除此之外,我们还可以导出成XML的格式:
[root@h1 826]# cat x.sh 

#获取mysql执行路径信息
MYSQL=`which mysql`

#第一个test是连接的数据库名字
#$MYSQL test   -u root -e '  select * from person '




  $MYSQL test   -u root  -X -e   'select * from person'   

















[root@h1 826]# sh x.sh 
<?xml version="1.0"?>

<resultset statement="select * from person
" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <row>
        <field name="id">1</field>
        <field name="name">solr</field>
        <field name="sex">girl</field>
  </row>

  <row>
        <field name="id">2</field>
        <field name="name">lucene</field>
        <field name="sex">boy</field>
  </row>

  <row>
        <field name="id">4</field>
        <field name="name">java</field>
        <field name="sex">girl</field>
  </row>

  <row>
        <field name="id">5</field>
        <field name="name">spring</field>
        <field name="sex">boy</field>
  </row>
</resultset>




当然利用shell我们还可以干很多事,比如说,备份表,备份库,导入数据,导出数据等,有兴趣的朋友可以继续学习下。

猜你喜欢

转载自qindongliang.iteye.com/blog/2109319