shell 执行mysql操作

方法一:

mysql -hhostname -Pport -uusername -ppassword -e 相关mysql的sql语句。

不用在mysql的提示符下运行mysql,即可以在shell中操作mysql的方法。

#!/bin/bash

HOSTNAME="127.0.0.1" #数据库信息

PORT="3306"

USERNAME="root"

PASSWORD="root"

DBNAME="db_test" #数据库名称

TABLENAME="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}"

方法二:

#!/bin/sh

echo "use MHFCDB;

insert into \`robot_action\` (ActionId, RobotAction) VALUES('0', '随机');

insert into \`robot_action\` (ActionId, RobotAction) VALUES('1', '移动');

insert into \`robot_action\` (ActionId, RobotAction) VALUES('2', '喊话');

insert into \`robot_action\` (ActionId, RobotAction) VALUES('3', '技能');

insert into \`robot_action\` (ActionId, RobotAction) VALUES('4', '副本');

INSERT INTO \`login_position\` (PostionName, PostionCoord, PostionOrit) VALUES('出生点', '609.30*758.43*148.25@1', '0*0*0');

INSERT INTO \`login_position\` (PostionName, PostionCoord, PostionOrit) VALUES('主城酒馆二楼', '661.90*759.38*150.51@1', '0*0*0');

" > sqlfile

mysql -u root < sqlfile

rm sqlfile

echo "robot_action init succesed!!"

===========================================================================================

1.直接管道过去

echo "show databases" | mysql -uroot -p'123456'

Database

information_schema

mysql

sakila

test

2.从文件

[root@kenthy sakila-db]# cat file

show databases;

[root@kenthy sakila-db]# mysql -uroot -p'123456' < file

Database

information_schema

mysql

sakila

test

3.块

[root@kenthy sakila-db]# mysql -uroot -p'123456' << SQL

> show databases;

> SQL

Database

information_schema

mysql

sakila

test

===========================================================================================

Bash#!/usr/bin/perl

use DBI;

$db = DBI->connect('dbi:mysql:test', 'vpsee', 'password');

$query = "select * from test_mark";

$cursor = $db->prepare($query);

$cursor->execute;

while (@row = $cursor->fetchrow_array) {

print "@row\n";

}#!/bin/bash

mysql -uvpsee -ppassword test <

select * from test_mark;

EOFMYSQL

如果需要复杂的数据库操作的话不建议用 shell 脚本,用 Perl/Python/PHP 操作数据库很方便,分别通过 Perl DBI/Python MySQLdb/PHP MySQL Module 接口来操作数据库。这里再给出这三种不同语言连接、查询数据库的简单例子(为了简单和减少篇幅删除一些不必要的代码):

Perl#!/usr/bin/perl

$db = DBI->connect('dbi:mysql:test', 'vpsee', 'password');

$query = "select * from test_mark";

$cursor = $db->prepare($query);

$cursor->execute;

while (@row = $cursor->fetchrow_array) {

print "@row\n";

}

Python#!/usr/bin/python

import MySQLdb

db = MySQLdb.Connect("localhost", "vpsee", "password", "test")

cursor = db.cursor()

query = "SELECT * FROM test_mark"

cursor.execute(query)

while (1):

row = cursor.fetchone()

if row == None:

break

print "%s, %s, %s, %s" % (row[0], row[1], row[2], row[3])

#!/usr/bin/python

db = MySQLdb.Connect("localhost", "vpsee", "password", "test")

cursor = db.cursor()

query = "SELECT * FROM test_mark"

while (1):

if row == None:

break

print "%s, %s, %s, %s" % (row[0], row[1], row[2], row[3])

PHP

#!/usr/bin/php

?php

$db = mysql_connect("localhost", "vpsee", "password");

mysql_select_db("test");

$result = mysql_query("SELECT * FROM test_mark");

while ($row = mysql_fetch_array($result)) {

print "$row[0] $row[1] $row[2] $row[3]\n";

}

?>#!/usr/bin/php

?php

$db = mysql_connect("localhost", "vpsee", "password");

mysql_select_db("test");

$result = mysql_query("SELECT * FROM test_mark");

while ($row = mysql_fetch_array($result)) {

print "$row[0] $row[1] $row[2] $row[3]\n";

}

?>

你可能感兴趣的:(mysql)