linux - 这个shell脚本连接和操作数据库应该怎么写?
本文介绍了linux - 这个shell脚本连接和操作数据库应该怎么写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
例如:有个连接数据库和创建一个数据库的操作,在命令行下按步骤手动可以这样写:
[root@iZ2896n381rZ ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 958
Server version: 10.2.3-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE example;
现在我想把它写成一个.sh
的shell脚本,如下:
database.sh
#!/bin/bash
mysql -u root -p
//这一行如果密码为空应该怎么写?
CREATE DATABASE example;
问题:
1、像上面那样把命令从上罗列到下就行吗?
1、输入密码那里,如果密码为空应该怎么写?
解决方案
mysql -u root -p password -e "CREATE DATABASE example;"
如果命令为空,直接不要-p参数:
mysql -u root -e "CREATE DATABASE example;"
这篇关于linux - 这个shell脚本连接和操作数据库应该怎么写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文