使用shell操作mysql数据库

作者:网络医生 发布于:2012-4-27 20:14 Friday 分类:shell

     本文只是简单介绍一下如何用shell来操作数据库,自己可以根据需要写出更复杂的功能,这里只是提供一个思路,抛压引玉。

例:把appache的访问日志ip存入mysql数据库中

创建存放IP地址的数据库和表:

create database  zhangyan;

use zhangyan;

create table ipinfo

 (

 id int(8) auto_increment,

address char(100) null,

primary key(id)

);

 

shell脚本文件insertip.sh

#!/bin/sh
for i in `awk -F "-" '{print $1}' /var/log/httpd/access_log.1`
do
echo $i
       mysql -u root -p123456  -e  "insert into zhangyan.ipinfo(address) values('$i')"

done

#注意红色部分,最后不要有分号,$i这个变量两边用单引号,不能用双引号。

查询数据库结果:

mysql> select * from ipinfo;
+-----+-----------------+
| id  | address         |
+-----+-----------------+
| 127 | 94.23.45.14     |
| 128 | 61.235.0.163    |
| 129 | 221.0.111.99    |
| 130 | 123.139.154.126 |
| 131 | 61.19.86.228    |
| 132 | 221.8.31.27     |
| 133 | 221.8.31.27     |
| 134 | 221.8.31.27     |
| 135 | 221.8.31.27     |
| 136 | 221.8.31.27     |

 

标签: mysql shell

发表评论:

  • 0
  • 1
  • 3
  • 9
  • 5

Powered by emlog