纵有疾风起
人生不言弃

shell脚本——mysql

很期待,学习shell脚本,减少重复工作

自动安装配置mysql脚本:

#/bin/bashLOG_FILE=/home/hadoop1/log/installmysql.logfunction RpmMysql(){key=$1r=$(rpm -qa $key)if [ $r ];thenreturn 1else return 0fi}mysql="mysql-server"RpmMysql mysqlr=$?if [ $r -eq 1 ] ;thenecho "$mysql was installed"#mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bakservice mysqld startelseecho "$mysql was not installed"yum install mysql mysql-server mysql-develservice mysqld startfichown -R mysql:mysql /var/lib/mysqlecho "install and start mysql success"#add user to mysql databaseuser1=rootpassword1=123456host=192.168.0.200user2=hadooppassword2=123456echo "INFORM:Enter database root password"#mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak#mysql -uroot -p </root/addroothadoop.sql> user1=rootpassword1=123456host=192.168.0.200user2=hadooppassword2=123456echo "INFORM:Enter database root password"mysql -uroot  -e "use mysql;update user set password =password('$password1') where user='$user1';insert ignore into mysql.user(User,Password)values('$user2','password2');grant all privileges on *.* to $user2@'%' identified by '$password2';flush privileges;"echo "configure done"

shell操作mysql 增删改查

mysql  -hhostname -Pport -uusername -ppassword  -e  相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作mysql的方法。#!/bin/bashHOSTNAME="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} (  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}"

  

 

  

文章转载于:https://www.cnblogs.com/kxdblog/p/4414025.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » shell脚本——mysql

分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录   注册

切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活