MySQL用户管理
- 首先登录到MySQL
mysql -u root -p
- 创建用户
mysql> create user 'class'@'%'identified by 'class';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to 'class'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql>
- 刷新用户信息,立刻生效
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
- 查看数据表的状态
[user1@lear ~]$ service iptables status
iptables: Only usable by root. [警告]
[user1@lear ~]$ su root
密码:
[root@lear user1]# service iptables status
表格:filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
[root@lear user1]#
其中的22号端口表示ssh连接
数据库的端口是3306
- 修改端口,开放3306的权限
vim /etc/sysconfig/iptables
原始文件
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
修改后的文件
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
- 重启服务
service iptables restart
[root@lear user1]# service iptables restart
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]
[root@lear user1]# service iptables status
表格:filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3306
6 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
[root@lear user1]#
- 远程登录服务
mysql -h 主机地址 -u 用户名 -p 用户密码
[root@lear user1]# mysql -h10.211.55.16 -P3306 -uclass -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
数据库
- 查看数据库
show databases ;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql>
- 查看当前的数据库
select database();
- 进入数据库
use `mysql`;
如果不使用数据库,则当前的数据库为空NULL,use之后,数据库不为空。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
mysql>
- 创建数据库
create database [if not exists] `database_name`;
如果数据库存在,创建会报错,加一个判断,当数据库不存在的时候再创建
mysql> create database if not exists `mydb`;
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql>
- 删除数据库
drop database if exists `database_name`;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database if not exists temp;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| temp |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> drop database if exists temp;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql>
数据表
- 显示数据库表
show tables;
默认显示当前数据库的表show tables from `mysql`;
查看指定数据库的表
mysql> use mydb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| tb1 |
+----------------+
1 row in set (0.00 sec)
mysql> show tables from mysql;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
23 rows in set (0.00 sec)
mysql>
- 创建数据表
INT 整数类型
VARCHAR 变长字符,参数为最大字符限制
mysql> create table `tb1`(
-> `id` INT,
-> `name` VARCHAR(20)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql>
删除数据表
drop table (table_name);
查看表信息
show create table (table_name) [\G]
mysql> show create table `tb1` \G
*************************** 1. row ***************************
Table: tb1
Create Table: CREATE TABLE `tb1` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql>
-- 查看表结构 describe (table_name);
或者 show columns from tb1;
mysql> describe tb1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql>
- 修改表结构
alter table `tb1` add `age` INT;
mysql> alter table `tb1` add `age` INT;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc `tb1`;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
修改表名称alter table `table1` rename to `teacher`;
修改列名称alter table `table1` change `name` `user_name` varchar(10) not null;
指定位置添加
mysql> alter table `tb1` add `gender` char(1) after `name`;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc `tb1`;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
添加多列
mysql> alter table `tb1` add(`aaa` int, `bb` int, `cc` int);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| aaa | int(11) | YES | | NULL | |
| bb | int(11) | YES | | NULL | |
| cc | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql>
- 删除数据表结构
alter table (table_name) drop '(column_name)';
mysql> alter table `tb1` drop `aa`;
ERROR 1091 (42000): Can't DROP 'aa'; check that column/key exists
mysql> alter table `tb1` drop `aaa`;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc `tb1`;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| bb | int(11) | YES | | NULL | |
| cc | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql>
删除多列alter table `(table_name)` drop `bb`,drop `cc`;
mysql> alter table `tb1` drop `bb`,drop `cc`;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
- 向数据表中添加数据
insert [INTO] (table_name) (column1,column2) values (value1,value2)
mysql> insert into `tb1` values(1,'rose','m',18);
Query OK, 1 row affected (0.00 sec)
一次添加多行数据
mysql> insert into `tb1` (`id`,`name`) values (2,'zhangsan'),(3,'lisi');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from `tb1`;
+------+----------+--------+------+
| id | name | gender | age |
+------+----------+--------+------+
| 1 | rose | m | 18 |
| 2 | zhangsan | NULL | NULL |
| 3 | lisi | NULL | NULL |
+------+----------+--------+------+
3 rows in set (0.00 sec)
mysql>
通过set添加数据
insert into `tb1` name='zhangsan',age=30;
更新数据
update tb1 set age=age+1 [where name='rose']
删除数据
delete from tb1 where name='rose';
查看数据表中的数据
select * from `tb1`;
- 查询指定列
select name from students;
- 给查询取别名 as是可选
select name as 姓名,s_id 学号 from students;
- 排序
-- 升序select * from stu_detail order by age;
-- 降序select * from stu_detail order by age desc;
- 限制显示的数量
-- 显示前2个select * from stu_detail limit 2;
-- 显示第3个开始的4个数据select * from stu_detail limit 2,4;
- 数据分组
select d_id,count(d_id) from students group by d_id;
-- 条件分组select d_id,count(d_id) from students group by d_id having d_id > 3;
也可以使用别名select d_id as 学院ID,count(d_id) from students group by d_id having 学院ID > 3;
- 查询结果处理
-- 取最大值select max(age) from stu_detail;
-- 去平均值select avg(age) from stu_detail;
-- 去最小值select min(age) from stu_detail;
- 子查询
select * from stu_detail where age > (select avg(age) from stu_detail);
- 关联查询
select * from students,department where d_id=id;
select * from students inner join department where d_id=id;
select * from students inner join department on d_id=id;
-- 左连接
students的数据都能显示
student d_id没有department对应的,也能显示 不能用where 必需用onselect * from students left join department on d_id = id;
-- 右连接
department的数据都能显示select * from students right join department on d_id = id;
事务
关联的SQL语句一起执行
- start transaction 开始事务
- commit 提交事务
- rollback 回滚事务,放弃修改
1. SQL语句分类
- DDL(Data Definition Language):数据定义语言,用来定义数据库对象:库、表、列等。功能:创建、删除、修改库和表结构。
- DML(Data Manipulation Language):数据操作语言,用来定义数据库记录:增、删、改表记录。
- DCL(Data Control Language):数据控制语言,用来定义访问权限和安全级别。
- DQL(Data Query Language):数据查询语言,用来查询记录。也是本章学习的重点。
2. SQL数据中的属性类型
- TINYINT:1字节,小整数值。
- SMALLINT:2字节,大整数值。
- MEDIUMINT:3字节,大整数值。
- INT或INTEGER:4字节,整型,大整数值。
- FLOAT:单精度浮点数值。
- DOUBLE(5,2):双精度浮点型数值,参数表示该浮点型数值最多有5位,其中必须有2位小数。
- DECIMAL(M,D):小数值,参数表示该数值最多有M位,其中必须有D位小数。
- CHAR:字符型,固定长度字符串类型:char(255)。你存入一个a字符,虽然a只占一个字符,但是它会自动给你加254个空格凑成255个长度。即数据的长度不足指定长度,它会补足到指定长度。
- VARCHAR:可变长度字符串类型:- varchar(65535),你存入的数据多长它就是多长。它会抽出几个字节来记录数据的长度。
- TEXT(CLOB):mysql独有的数据类型,字符串类型。
- BLOB:字节类型。
- YEAR:年份值,格式为:YYYY
- DATA:日期类型,格式为:yyyy-MM-dd。
- TIME:时间类型,格式为:hh:mm:ss。
- TIMESTAMP:时间戳类型,格式为上面二者的综合。
- DATETIME:混合日期和时间值,格式为:YYYYMMDD HHMMSS.
3. SQL语句详解
当然首先需要再命令行中输入mysql -uroot -p来进入mysql。注意:1.MySQL语法不区分大小写,但是建议在写关键字时用大写。2.每一条语句后面以分号结尾。
3.1DDL(数据定义语言)语法
该语言用来对数据库和表结构进行操作。
对数据库的操作:
查看所有数据库:SHOW DATABASES;
使用数据库:USE 数据库名;
创建数据库并指定编码:CREATE DATABASE [IF NOT EXISTS] 数据库名 [DEFAULT CHARACTER SET UTF8];
删除数据库:DROP DATABASE 数据库名;
修改数据库的编码:ALTER DATABASE 数据库名 CHARACTER SET UTF-8;
对表结构的操作
创建表:
CREATE TABLE (IF NOT EXISTS)表名(
列名 列类型,
...,
列名 列类型,
)
- 查看当前数据库中所有表:SHOW TABLES;
- 查看表结构:DESC 表名;
- 删除表:DROP 表名;
- 修改表:修改表有5个操作,但前缀都是一样的:ALTER TABLE 表名...
- 修改表之添加列:ALTER TABLE 表名 add (列名 列类型,...,列名 列类型);
- 修改表之修改列类型:ALTER TABLE 表名 MODIFY 列名 列的新类型;
- 修改表之列名称列类型一起修改:ALTER TABLE 表名 CHANGE 原列名 新列名 列名类型;
- 修改表之删除列:ALTER TABLE 表名 DROP 列名;
- 修改表之修改表名:ALTER TABLE 表名 RENAME TO 新表名
3.2DML(数据操作语言)语法
该语言用来对表记录操作(增、删、改)。
3.2.1插入数据(一次插入就是插入一行)
insert into 表名 (列名1,列名2,列名3) values (列值1,列值2,列值3)
说明:1.在数据库中所有的字符串类型,必须使用单引号。2.(列名1,列名2,列名3)可省略,表示按照表中的顺序插入。但不建议采取这种写法,因为降低了程序的可读性。3.在命令行插入记录不要写中文,否则会出现乱码(解决控制台的乱码问题后便可插入中文,至于如何解决乱码问题请参考我的下篇文章)。
3.2.2修改记录(不会修改一行)
修改某列的全部值:update 表名 set 列名1=列值1(,列名2=列值2);
修改(某行或者多行记录的)列的指定值:update 表名 set 列名1=列值1 where 列名2=列值2 or 列名3=列值3;
运算符:=、!=、<>、<、>、>=、<=、between...and、in(…)、is null、not、or、and,其中in(…)的用法表示集合。例如:update 表名 set 列名1=列值1 where 列名2=列值2 or 列名2=列值22用in(…)写成update 表名 set 列名1=列值1 where 列名2 in(列值2,列值3)
3.2.3删除数据(删除整行)
delete from 表名 (where 条件);不加where条件时会删除表中所有的记录,所以为了防止这种失误操作,很多数据库往往都会有备份。
3.3DCL(数据控制语言)语法
该语言用来定义访问权限,理解即可,以后不会多用。需要记住的是,一个项目创建一个用户,一个项目对应的数据库只有一个。这个用户只能对这个数据库有权限,其它数据库该用户就操作不了。
3.3.1创建用户
用户只能在指定ip地址上登录mysql:create user 用户名@IP地址 identified by ‘密码’;
用户可以在任意ip地址上登录:create user 用户名@‘%’ identified by ‘密码’;
3.3.2给用户授权
语法:grant 权限1,…,权限n on 数据库.* to 用户名@IP地址;其中权限1、2、n可以直接用all关键字代替。权限例如:create,alter,drop,insert,update,delete,select。
3.3.3撤销授权
语法:revoke 权限1,…,权限n on 数据库.* from 用户名@ ip地址;撤销指定用户在指定数据库上的指定权限。撤销例如:revoke create,delete on mydb1.* form user@localhost;表示的意思是撤消user用户在数据库mydb1伤的create、alter权限。
3.3.4查看权限
查看指定用户的权限:show grants for 用户名@ip地址;
3.4DQL(数据查询语言)语法
重点,该语言用来查询记录,不会修改数据库和表结构。
3.4.1基本查询(后缀都是统一为from 表名)
- 1.字段(列)控制1.查询所有列:select * from 表名;
其中*表示查询所有列,而不是所有行的意思。 - 2.查询指定列:select 列1,列2,列n from 表名;
- 3.完全重复的记录只显示一次:在查询的列之前添加distinct:select distinct $ from 表名;
缺省值为all。 - 4.列运算a.数量类型的列可以做加、减、乘、除:SELECT sal*5 from 表名;
说明:
1.遇到null加任何值都等于null的情况,需要用到ifnull()函数。
2.将字符串做加减乘除运算,会把字符串当作0。b.字符串累类型可以做连续运算(需要用到concat()函数):select concat(列名1,列名2) from 表名;其中列名的类型要为字符串。c. 给列名起别名:select 列名1 (as) 别名1,列名2 (as) 别名2 from 表名;
2.条件控制
1.条件查询。在后面添加where指定条件:select * from 表名 where 列名=指定值;
2.模糊查询:当你想查询所有姓张的记录。用到关键字like。eg:select * from 表名 where 列名 like ‘张_’;
(代表匹配任意一个字符,%代表匹配0~n个任意字符)。
3.4.2排序(所谓升序和降序都是从上往下排列)
1.升序:select * form 表名 order by 列名 (ASC );
()里面的内容为缺省值;
2.降序:select * from 表名 order by 列名 DESC;
3.使用多列作为排序条件: 当第一列排序条件相同时,根据第二列排序条件排序(当第二列依旧相同时可视情况根据第三例条件排序)。eg:select * from 表名 order by 列名1 ASC, 列名2 DESC;
意思是当列名1的值相同时按照列名2的值降序排。
3.4.3聚合函数
1.count:select count(列名) from 表名;
,纪录有效行数。
2.max:select count(列名) from 表名;
,列中最大值。
3.min:select sum(列名) from 表名;
,列中最小值。
4.sum:select sum(列名) from 表名;
,求列的总值,null 和字符串默认为0。
5.avg:select avg(列名) from 表名;
,一列的平均值。
3.4.4分组查询
分组查询的信息都是组的信息,不能查到个人的信息,其中查询组的信息是通过聚合函数得到的。
语法:select 分组列名,聚合函数1,聚合函数2 from 表名 group by 该分组列名;
其中分组列名需要的条件是该列名中有重复的信息。
查询的结果只能为:作为分组条件的列和聚合函数;查处的信息都是组的信息。
分组查询前,还可以通过关键字where先把满足条件的人分出来,再分组。语法为:select 分组列,聚合函数 from 表名 where 条件 group by 分组列;
分组查询后,也可以通过关键字having把组信息中满足条件的组再细分出来。语法为:select 分组列,聚合函数 from 表名 where 条件 group by 分组列 having 聚合函数或列名(条件);
3.4.5LIMIT子句(mysql中独有的语法)
LIMIT用来限定查询结果的起始行,以及总行数。
例如:select * from 表名 limit 4,3;
表示起始行为第5行,一共查询3行记录。
4.总结
学过的关键字:select,from,where,group by,having ,order by。
当一条查询语句中都包含所有这些关键字时它们的优先级是select>from>where>group by>having>order by