shell 远程mysql数据库_使用shell脚本将数据插入到远程MySQL数据库

bd96500e110b49cbb3cd949968f18be7.png

I've been trying to get a shell(bash) script to insert a row into a REMOTE database, but i've been having some trouble :(

The script is meant to upload a file to a server, get a URL,HASH, and a filesize, connect to a remote mysql database, and insert the data into an existing table. I've gotten it working until the remote MYSQL database bit.

It looks like this:

#!/bin/bash

zxw=randomtext

description=randomtext2

for file in "$@"

do

echo -n *****

ident= *****

data= ****

size=` ****

hash=`****

mysql --host=randomhost --user=randomuser --password=randompass randomdb

insert into table (field1,field2,field3) values('http://www.site.com/$hash','$file','$size');

echo "done"

done

I'm a total noob at programming so yeh :P

Anyway, I added the \ to escape the brackets as I was getting errors. As it is right now, the script is works fine until connects to the mysql database. It just connects to the mysql database and doesn't do the insert command (and i dont even know if the insert command would work in bash).

PS: I've tried both the mysql commands from the command line one by one, and they worked, though I defined the hash/file/size and didn't have the escaping "\".

Anyway, what do you guys think? Is what im trying to do even possible? If so how?

Any help would be appreciated :)

解决方案

The insert statement has to be sent to mysql, not another line in the shell script, so you need to make it a "here document".

mysql --host=randomhost --user=randomuser --password=randompass randomdb << EOF

insert into table (field1,field2,field3) values('http://www.site.com/$hash','$file','$size');

EOF

The << EOF means take everything before the next line that contains nothing but EOF (no whitespace at the beginning) as standard input to the program.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值