Home > Database > Mysql Tutorial > Shell操作mysql数据库_MySQL

Shell操作mysql数据库_MySQL

WBOY
Release: 2016-06-01 13:32:50
Original
1108 people have browsed it

bitsCN.com

Shell操作mysql数据库

 

mysql  -hhostname -Pport -uusername-ppassword  -e  相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作mysql的方法。

 

#!/bin/bash

 

HOSTNAME="192.168.111.84";                                    #数据库信息

PORT="3306";

USERNAME="root";

PASSWORD="";

DBNAME="test_db_test";                                              #数据库名称

TABLENAME="test_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} (   namevarchar(20), id int(11) default 0 )"

mysql-h${HOSTNAME}  -P${PORT}   -u${USERNAME}-p${PASSWORD} ${DBNAME} -e"${create_table_sql}"

 

#插入数据

insert_sql="insertinto ${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="deletefrom ${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}"

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template