目录
〔1〕数据导入导出
- MySQL服务安装时,自动创建导出导出目录
- 查看目录所在路径(mysql命令行)
- show variables like "secure_file_priv"
- 可自定义导入导出目录
- 创建目录并修改权限为mysql
- mkdir /myload
- chown mysql /myload
- 修改配置文件/etc/my.cnf
- 添加一行:secure_file_priv=/myload
- 重启服务
- systemctl restart mysqld
- 创建目录并修改权限为mysql
- 查看目录所在路径(mysql命令行)
- 数据导入
- 默认只有root用户有数据导入权限
- 概念:把系统文件的内容存储到表里
- 步骤: 建表,导入数据
- 命令格式
- load data infile "目录名/文件名" into table 库名.表名 fields terminated by "分隔符" lines terminated by "\n";
- 字段分隔符与导入文件一致
- 表字段类型和字段个数要与文件一致
- 导入数据时指定文件的绝对路径
- 例子:将/etc/passwd 文件内容导入到db3库,user表
- create database db3; use db3;
- create table user(name char(50), password char(1), uid int, gid int, comment char(100), homedir char(100), shell char(100));
- load data infile "/myload/passwd" fields terminated by ":" lines terminated by "\n";
- alter table user add id int primary key auto_increment first;
- 数据导出
- 概念:把表记录保存到系统文件里
- 命令格式
- 格式1: SQL查询命令 into outfile "目录名/文件名"; //默认分隔符为空格,换行符为\n
- select * from db3.user where id<=5 into outfile "/myload/user.txt";
- 格式2: SQL查询命令 into outfile "目录名/文件名" fields terminated by "分隔符"; //指定分隔符
- 格式3: SQL查询命令 into outfile "目录名/文件名" fields terminated by "分隔符" lines terminated by "换行符";
- 格式1: SQL查询命令 into outfile "目录名/文件名"; //默认分隔符为空格,换行符为\n
〔2〕管理表记录
- 增加表记录
- 格式1: 给所有字段,添加1条记录
- insert into 表名 values(字段值列表);
- 格式2: 给所有字段,添加N条记录
- insert into 表名 values(字段值列表),(字段值列表),...;
- 格式3: 给指定字段,添加1条记录
- insert into 表名(字段名列表) values(字段值列表);
- 格式4: 给指定字段,添加N条记录
- insert into 表名(字段名列表) values(字段值列表),(字段值列表),...;
- 注意
- 没有赋值的字段使用默认值或自增长值
- 格式1: 给所有字段,添加1条记录
- 查询表记录
- 格式1: 查看所有记录
- select 字段1,...,字段N from 库名.表名;
- select * from 库名.表名;
- 格式2: 条件查询
- select 字段1,...,字段N from 库名.表名 where 条件表达式;
- 格式1: 查看所有记录
- 更新表记录
- 格式1: 批量更新
- update 库名.表名 set 字段名=值,字段名=值,...;
- 格式2: 条件匹配更新
- update 库名.表名 set 字段名=值,字段名=值,... where 条件表达式;
- 格式1: 批量更新
- 删除表记录
- 格式1: 删除所有记录
- delete from 库名.表名;
- 格式2: 条件匹配删除
- delete from 库名.表名 where 条件表达式;
- 格式1: 删除所有记录
〔3〕匹配条件
- 基本匹配条件
- 数值比较
- 符号: =, >, >=, <, <=, !=
- 例: 查找uid=3的用户
- select * from user where uid=3;
- 字符比较
- 符号: =, !=, is null(为空), is not null(不为空)
- 例: 查找shell字段为空的记录
- select name,shell from user where shell is null;
- 数据库中的空指null或NULL
- 逻辑匹配
- 多个判断条件时使用
- 逻辑或,只要一个条件成立则为真
- 符号: or 或者 ||
- 逻辑与,所有条件均成立则为真
- 符号: and 或者 &&
- 逻辑非,取反
- 符号: ! 或者 not
- 提高优先级
- 符号: ()
- 例: (uid+gid)/2
- 范围内匹配
- 匹配范围内的任意一个值即可
- 在...里
- in (值列表)
- 例: 列出name值是adm,sync,mysql的记录
- select name from user where name in ("adm","sync","mysql");
- 不在...里
- not in (值列表)
- 例: 列出shell解释器不是/bin/bash,/sbin/nologin的shell记录
- select shell from user where shell not in ("/bin/bash","/sbin/nologin");
- 在...之间
- between 数字 and 数字
- 例: 列出用户id在10到20之间的用户记录
- select name,uid from user where uid between 10 and 20;
- 去重显示
- distinct 字段名
- 例: 列出user表中有哪些不同的shell
- select distinct shell from user;
- 数值比较
- 高级匹配条件
- 模糊查询
- 格式: where 字段名 like "通配符"
- 通配符
- 任意单个字符匹配: _
- 0~n个字符匹配:%
- 例1: 列出name值为4个字符的记录
- select name from user where name like "____";
- 例2: 列出name值至少为4个字符的记录
- select name from user where name like "____%";
- 例3: 列出name值包含字符a的记录
- select name from user where name like "%a%";
- 正则表达式
- 格式: where 字段名 regexp '正则表达式'
- 正则元字符: ^ $ . [] * |
- 例1: 列出name值以j开头或者以y结尾的记录
- select name from user where name regexp '^j | y$';
- 例2: 列出name值以r开头,t结尾的记录
- select name from user where name regexp '^a.*t$';
- 四则运算
- 运算操作,字段必须是数值类型
- 符号: +, -, *, /, %取余, ()提供优先级
- 例1: 查看root用户的出生年份,表中只有年龄字段age
- select name,age, 2019-age birth from user where name="root";
- 例2: 列出uid为偶数的用户记录
- select name,uid from user where uid%2=0;
- 例3: 将表中uid值小于等于10的记录值加1
- update user set uid=uid+1 where uid <=10;
- 模糊查询
- 操作查询结果
- 聚集函数:MySQL内置数据统计函数,字段为数值类型
- 统计字段平均值: avg(字段名)
- 统计字段值之和: sum(字段名)
- 统计字段最小值: min(字段名)
- 统计字段最大值: max(字段名)
- 统计字段值个数: count(字段名)
- 例
- select avg(uid) from user;
- select count(*) from user where shell not in ("/bin/bash","/sbin/nologin");
- 查询结果排序
- 用法: SQL查询 order by 字段名 [asc | desc]
- 例
- select name,uid from user where uid >=10 and uid <=1000 order by uid desc;
- 查询结果分组
- 用法: SQL查询 group by 字段名;
- 例
- select shell from user where uid <=500 group by shell;
- 查询结果过滤
- 用法: SQL查询 having 条件表达式;
- 例
- select name from user where shell !="/bin/bash" having name="mysql";
- 查询结果限制行数显示
- 用法1: 显示查询结果的前n条记录
- 格式: SQL查询 limit 数字;
- 例
- select id,name from user where uid <=15 limit 3;
- 用法2: 显示查询结果指定范围内的记录
- 格式: SQL查询 limit 数字1, 数字2;
- 数字1指起始行,0表示第一行
- 数字2表示总行数
- 例
- select id,name from user where uid <=15 limit 3,3;
- 格式: SQL查询 limit 数字1, 数字2;
- 用法1: 显示查询结果的前n条记录
- 聚集函数:MySQL内置数据统计函数,字段为数值类型
〔4〕管理工具
- 管理方式
- MySQL命令行
- 图形化的管理软件
- web界面方式
- 常见工具
类型 | 界面 | 操作系统 | 说明 |
mysql | 命令行 | 跨平台 | MySQL官方bundle包自带 |
MySQL-Workbench | 图形 | 跨平台 | MySQL官方提供 |
MySQL-Front | 图形 | Windows | 开源,轻量级客户端软件 |
Navicat | 图形 | Windows | 专业,功能强大,收费 |
phpMyAdmin | 浏览器 | 跨平台 | 开源,需要如LAMP平台支持 |
〔5〕部署LAMP+phpMyAdmin
- yum -y install httpd php php-mysql //安装web服务,php服务,连接数据库服务
- systemctl start httpd //启动web服务
- systemctl enable httpd //开机自启服务
- tar -xf phpMyAdmin-2.11.11-all-languages.tar.gz //解压管理工具包
- mv phpMyAdmin-2.11.11-all-languages /var/www/html/phpmyadmin //移动到网站目录下,重命名为phpmyadmin
- cd /var/www/html/phpmyadmin
- cp config.sample.inc.php config.inc.php //目录下没有配置文件,复制模板改名为config.inc.php
- vim config.inc.php //修改config.inc.php文件
- $cfg['blowfish_secret']='myluo'; //cookies 安全认证
- $cfg['Server'][$i]['host']='localhost'; //设置数据库服务器所在IP地址,这里是本机
- firefox 192.168.4.50/phpmyadmin //访问测试,输入数据库管理员账户和密码登录