二十二、创建数据库脚本
安装mysql数据库
可以看之前写过的一篇博文:点击传送
mysql命令
mysql程序使用两种不同类型的命令
- 特殊的MySQL命令
- 标准的SQL语句
mysql程序有自己的一组命令,方便控制环境以及提取关于MySQL服务器的信息。
举例
mysql> \s -------------- mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper Connection id: 3 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.26 MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /tmp/mysql.sock Uptime: 2 hours 14 min 23 sec Threads: 2 Questions: 6 Slow queries: 0 Opens: 108 Flush tables: 1 Open tables: 101 Queries per second avg: 0.000 --------------
MySQL程序支持标准SQL命令。
查看数据库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.34 sec)
查看mysql数据库中的表
mysql会话一次只能连一个数据库
分号代表命令结束
mysql> use mysql; Database changed mysql> show tables; +---------------------------+ | Tables_in_mysql | +---------------------------+ | columns_priv | | db | | engine_cost | | event | | func | | general_log | | gtid_executed | | help_category | [...]
创建数据库
create database name;
mysql> create database mytest; Query OK, 1 row affected (0.00 sec)
创建用户账户
语法格式
grant 权限 on 数据库对象 to 用户
举例
mysql> grant select,insert,delete,update on test.* to test identified by 'test'; Query OK, 0 rows affected, 1 warning (0.00 sec)
grant命令:如果用户账户不存在则创建,赋予权限
indentified by:为新用户设置密码
这条命令意思为:赋予用户test拥有对数据库test其下所有表有查询,插入,删除,修改的权限,该用户的密码为test。
创建数据表
注意创建数据表前先指定数据库
empid的值唯一,不可重复
mysql> use mytest; Database changed mysql> create table employees ( -> empid int not null, -> lastname varchar(30), -> firstname varchar(30), -> salary float, -> primary key (empid)); Query OK, 0 rows affected (0.01 sec)
创建完成后查看
mysql> show tables; +------------------+ | Tables_in_mytest | +------------------+ | employees | +------------------+ 1 row in set (0.00 sec)
mysql的数据类型
数据类型 | 描述 |
char | 定长字符串(固定长度) |
varchar | 变长字符串(可变长度) |
int | 整数值 |
float | 浮点值 |
boolean | 布尔值 |
date | YYYY-MM-DD格式日期 |
time | HH:mm:ss格式时间 |
timestamp | 日期加时间组合 |
text | 较长的字符串值 |
BLOB | 二进制值,如图片,视频 |
插入和删除数据
举例
mysql> insert into employees values (1,'Blum','Rich',25000.00); Query OK, 1 row affected (0.17 sec) mysql> insert into employees values (2,'Blum','Rich',25000.00); Query OK, 1 row affected (0.10 sec)
删除
mysql> delete from employees where empid =2; Query OK, 1 row affected (0.00 sec)
查询数据
举例
mysql> select * from employees; +-------+----------+-----------+--------+ | empid | lastname | firstname | salary | +-------+----------+-----------+--------+ | 1 | Blum | Rich | 25000 | +-------+----------+-----------+--------+ 1 row in set (0.00 sec)
修饰符
- where 显示符合特定条件
- order by 对某列排序
- limit 显示指定行数据
mysql> select * from employees where salary > 4000; +-------+----------+------------+--------+ | empid | lastname | firstname | salary | +-------+----------+------------+--------+ | 1 | Blum | Rich | 25000 | | 3 | Blum | Katie Jane | 34500 | | 4 | Blum | Jessica | 25340 | +-------+----------+------------+--------+ 3 rows in set (0.00 sec)
在脚本中使用数据库
登录到服务器
在$HOME/.my.cnf配置文件中设置mysql登录账户密码
这样该用户就不用输入密码就能登陆
[tz@tzPC ~]$ whoami tz [tz@tzPC ~]$ cat $HOME/.my.cnf [client] password = test [tz@tzPC ~]$ chmod 400 $HOME/.my.cnf [tz@tzPC ~]$ mysql mytest -u test
向服务器发送命令
发送单条命令
[root@tzPC 25Unit]# cat mtest1.sh #!/bin/bash #send a command to the MySQL server MYSQL=$(which mysql) $MYSQL mytest -u test -e 'select * from employees'
效果
[root@tzPC 25Unit]# bash mtest1.sh +-------+----------+------------+--------+ | empid | lastname | firstname | salary | +-------+----------+------------+--------+ | 1 | Blum | Rich | 25000 | | 3 | Blum | Katie Jane | 34500 | | 4 | Blum | Jessica | 25340 | +-------+----------+------------+--------+
发送多条命令
使用EOF分隔符之间的所有内容重定向给mysql命令时,因为数据是重定向过来的所以返回值只包含原始数据,没有格式,有利于提取字段。
[root@tzPC 25Unit]# cat mtest2.sh #/bin/bash #sending multiple commands to MySQL MYSQL=$(which mysql) $MYSQL mytest -u test <<EOF show tables; select * from employees where salary > 4000; EOF
效果
[root@tzPC 25Unit]# bash mtest2.sh Tables_in_mytest #这两行是show tables 命令显示的,因为没有了格式框,第一行是这个表位于哪个数据库 employees #这一行显示的是表名 empid lastname firstname salary 1 Blum Rich 25000 3 Blum Katie Jane 34500 4 Blum Jessica 25340
show tables;
mysql> show tables; +------------------+ | Tables_in_mytest | +------------------+ | employees | +------------------+ 1 row in set (0.00 sec)
脚本主体如下
[root@tzPC 25Unit]# cat mtest3.sh #!/bin/bash #send data to the table in th MySQL database MYSQL=$(which mysql) if [ $# -ne 4 ] #如果输入参数不等于4 then echo "Usage: mtest3 empid lastname firstname salary" #Usage用法 salary薪水 else statement="insert into employees values ($1,'$2','$3','$4')" $MYSQL mytest -u test <<EOF $statement EOF #EOF必须顶格写且只能由EOF结束符,不能有空格制表符等 if [ $? -eq 0 ] then echo "Data successfully added!" else echo "Problem adding data!" fi fi
脚本格式化输出如下
[root@tzPC 25Unit]# cat mtest4.sh #!/bin/bash #redirecting重定向 SQL output to a varible MYSQL=$(which mysql) dbs=$($MYSQL mytest -u test -Bse 'show databases') #dbs中的数据为information_schema mytest中间以空格隔开 for db in $dbs do echo $db done
效果
[root@tzPC 25Unit]# bash mtest4.sh information_schema mytest
因为是重定向到变量dbs,所以没有格式,-B选项指定mysql工作在批处理模式下,-s选项禁止输出列标题,如Database标题就没有输出出来
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mytest | +--------------------+ 2 rows in set (0.00 sec)
mysql程序还支持XML,可扩展标记语言,只需要加上-X选项
[root@tzPC 25Unit]# mysql mytest -u test -X -e 'select * from employees where empid =1' <?xml version="1.0"?> <resultset statement="select * from employees where empid =1 " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <row> <field name="empid">1</field> <field name="lastname">Blum</field> <field name="firstname">Rich</field> <field name="salary">25000</field> </row> </resultset>
学习来自:《Linux命令行与Shell脚本大全 第3版》第25章
今天的学习是为了以后的工作更加的轻松!