MySQL 基础学习手册
3 rows in set (0.00 sec)
1.如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句中。
2.你可以使用 AND 或者 OR 指定一个或多个条件。
3.查询语句中你可以使用一个或者多个表,表之间使用逗号, 分割,并使用 WHERE 语句来设定查询条件。
MariaDB [test1]> select * from book where name="水浒";
+--------+-------+------------+
| name | price | date |
+--------+-------+------------+
| 水浒 | 88 | 2020-04-13 |
+--------+-------+------------+
1 row in set (0.00 sec)
MariaDB [test1]> select * from book where price=100 and date="2020-04-13";
+-----------+-------+------------+
| name | price | date |
+-----------+-------+------------+
| 西游记 | 100 | 2020-04-13 |
+-----------+-------+------------+
1 row in set (0.00 sec)
MariaDB [test1]> select * from book where price=100 or date="2020-04-13";
+-----------+-------+------------+
| name | price | date |
+-----------+-------+------------+
| 水浒 | 88 | 2020-04-13 |
| 红楼梦 | 99 | 2020-04-13 |
| 西游记 | 100 | 2020-04-13 |
+-----------+-------+------------+
3 rows in set (0.00 sec)
_**1.你可以同时更新一个或多个字段。
2.你可以在一个单独表中同时更新数据。
3.你可以在 WHERE 子句中指定任何条件。**_
MariaDB [test1]> update book set name="水浒传" where price=88;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [test1]> select * from book;
+-----------+-------+------------+
| name | price | date |
+-----------+-------+------------+
| 水浒传 | 88 | 2020-04-13 |
| 红楼梦 | 99 | 2020-04-13 |
| 西游记 | 100 | 2020-04-13 |
+-----------+-------+------------+
3 rows in set (0.00 sec)
_**1.如果没有指定 WHERE 子句,MySQL 表中的所有记录将被删除。
2.你可以在 WHERE 子句中指定任何条件
3.您可以在单个表中一次性删除记录。
4.delete 和 truncate 仅仅删除表数据,drop 连表数据和表结构一起删除,打个比方,delete 是单杀,truncate 是团灭,drop 是把电脑摔了。**_
MariaDB [a]> select * from book;
+------+-------+------------+
| name | price | date |
+------+-------+------------+
| ? | 66 | 2020-04-13 |
| ? | 88 | 2020-04-13 |
| ? | 99 | 2020-04-13 |
| ? | 100 | 2020-04-13 |
| ? | 88 | 2020-04-13 |
| a | 8 | 2020-04-14 |
+------+-------+------------+
6 rows in set (0.00 sec)
MariaDB [a]> delete from book where price=8;
Query OK, 1 row affected (0.00 sec)
MariaDB [a]> select * from book;
+------+-------+------------+
| name | price | date |
+------+-------+------------+
| ? | 66 | 2020-04-13 |
| ? | 88 | 2020-04-13 |
| ? | 99 | 2020-04-13 |
| ? | 100 | 2020-04-13 |
| ? | 88 | 2020-04-13 |
+------+-------+------------+
5 rows in set (0.00 sec)
MariaDB [a]> delete from book;
Query OK, 5 rows affected (0.00 sec)
MariaDB [a]> select * from book;
Empty set (0.00 sec)
_**1.like 匹配/模糊匹配,会与 % 和 _ 结合使用。
2.在 where like 的条件查询中,SQL 提供了四种匹配方式。
%:表示任意 0 个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。
_:表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句。
[]:表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
[^] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。
查询内容包含通配符时,由于通配符的缘故,导致我们查询特殊字符 “%”、“”、“[” 的语句无法正常实现,而把特殊字符用 “[ ]” 括起便可正常查询。**
MariaDB [test1]> select * from book where name like '水 %';
+-----------+-------+------------+
| name | price | date |
+-----------+-------+------------+
| 水浒传 | 88 | 2020-04-13 |
+-----------+-------+------------+
1 row in set (0.00 sec)
MariaDB [test1]> select * from book where name like '%%';
+-----------+-------+------------+
| name | price | date |
+-----------+-------+------------+
| 水浒传 | 88 | 2020-04-13 |
| 红楼梦 | 99 | 2020-04-13 |
| 西游记 | 100 | 2020-04-13 |
+-----------+-------+------------+
3 rows in set (0.00 sec)
MariaDB [test1]> select * from book where name like '楼';
+-----------+-------+------------+
| name | price | date |
+-----------+-------+------------+
| 红楼梦 | 99 | 2020-04-13 |
+-----------+-------+------------+
1 row in set (0.00 sec)
MariaDB [test1]> select * from book order by price desc;
+-----------+-------+------------+
| name | price | date |
+-----------+-------+------------+
| 西游记 | 100 | 2020-04-13 |
| 红楼梦 | 99 | 2020-04-13 |
| 水浒传 | 88 | 2020-04-13 |
+-----------+-------+------------+
3 rows in set (0.00 sec)
MariaDB [test1]> select * from book order by price asc;
+-----------+-------+------------+
| name | price | date |
+-----------+-------+------------+
| 水浒传 | 88 | 2020-04-13 |
| 红楼梦 | 99 | 2020-04-13 |
| 西游记 | 100 | 2020-04-13 |
+-----------+-------+------------+
3 rows in set (0.00 sec)
_**1.GROUP BY 语句根据一个或多个列对结果集进行分组。
常和 COUNT, SUM, AVG,等函数一起使用。**_
2.GROUP by 的用法很多要多加练习!
3.as 可以对查询的内容进行重命名
4.WITH ROLLUP(rollup 的意思就是归纳!) 可以实现在分组统计数据基础上再进行相同的统计(SUM,AVG,COUNT…)。其中记录 NULL 表示所有人的登录次数。我们可以使用 coalesce 来设置一个可以取代 NUll 的名称 select coalesce(a,b,c);
MariaDB [test1]> select * from book;
+-----------+-------+------------+
| name | price | date |
+-----------+-------+------------+
| 水浒传 | 88 | 2020-04-13 |
| 红楼梦 | 99 | 2020-04-13 |
| 西游记 | 100 | 2020-04-13 |
| 水浒传 | 90 | 2020-01-01 |
+-----------+-------+------------+
4 rows in set (0.00 sec)
MariaDB [test1]> select name,count(*) from book group by name;
+-----------+----------+
| name | count(*) |
+-----------+----------+
| 水浒传 | 2 |
| 红楼梦 | 1 |
| 西游记 | 1 |
+-----------+----------+
3 rows in set (0.00 sec)
MariaDB [test1]> select name,sum(price) as total from book group by name with rollup;
+-----------+-------+
| name | total |
+-----------+-------+
| 水浒传 | 178 |
| 红楼梦 | 99 |
| 西游记 | 100 |
| NULL | 377 |
+-----------+-------+
4 rows in set (0.00 sec)
MariaDB [test1]> select coalesce(name,'total') as name,sum(price) as total from book group by name with rollup;
+-----------+-------+
| name | total |
+-----------+-------+
| 水浒传 | 178 |
| 红楼梦 | 99 |
| 西游记 | 100 |
| total | 377 |
+-----------+-------+
4 rows in set, 1 warning (0.00 sec)
_**1. 以上内容比较简单容易理解,但是在真正的应用中经常需要从多个数据表中读取数据。
2. INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。**_
3.
4.
5.
MariaDB [test1]> select * from book;
+-----------+-------+------------+
| name | price | date |
+-----------+-------+------------+
| 水浒传 | 88 | 2020-04-13 |
| 红楼梦 | 99 | 2020-04-13 |
| 西游记 | 100 | 2020-04-13 |
| 水浒传 | 90 | 2020-01-01 |
+-----------+-------+------------+
4 rows in set (0.00 sec)
MariaDB [test1]> select * from num;
+-----------+----------+
| name | ordernum |
+-----------+----------+
| 水浒传 | 1 |
| 红楼梦 | 3 |
| 西游记 | 2 |
+-----------+----------+
3 rows in set (0.00 sec)
#内连接
MariaDB [test1]> select book.name,book.price,book.date,num.ordernum from book inner join num where book.name=num.name;
+-----------+-------+------------+----------+
| name | price | date | ordernum |
+-----------+-------+------------+----------+
| 水浒传 | 88 | 2020-04-13 | 1 |
| 红楼梦 | 99 | 2020-04-13 | 3 |
| 西游记 | 100 | 2020-04-13 | 2 |
| 水浒传 | 90 | 2020-01-01 | 1 |
+-----------+-------+------------+----------+
4 rows in set (0.00 sec)
#左连接,左表没有对应的项的情况,会显示 null
MariaDB [test1]> select book.name,num.ordernum from book left join num on book.name=num.name;
+--------------+----------+
| name | ordernum |
+--------------+----------+
| 水浒传 | 1 |
| 红楼梦 | 3 |
| 西游记 | 2 |
| 三国演艺 | NULL |
+--------------+----------+
4 rows in set (0.00 sec)
#右连接,以右表为主,右表多出的将不显示
MariaDB [test1]> select book.name,num.ordernum from book right join num on book.name=num.name;
+-----------+----------+
| name | ordernum |
+-----------+----------+
| 水浒传 | 1 |
| 红楼梦 | 3 |
| 西游记 | 2 |
+-----------+----------+
3 rows in set (0.00 sec)
_**1.我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作。
2. IS NULL: 当列的值是 NULL,此运算符返回 true。
IS NOT NULL: 当列的值不为 NULL, 运算符返回 true。
<=>: 比较操作符(不同于 = 运算符),当比较的的两个值相等或者都为 NULL 时返回 true。**_
MariaDB [test1]> create table nulll(name varchar(255) not null, price int);
Query OK, 0 rows affected (0.00 sec)
MariaDB [test1]> insert into nulll values("吴承恩","max"),("罗贯中","maxx"),("雪芹",null),("施耐庵",null);
Query OK, 4 rows affected, 2 warnings (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 2
MariaDB [test1]> select * from nulll;
+-----------+-------+
| name | price |
+-----------+-------+
| 吴承恩 | 0 |
| 罗贯中 | 0 |
| 曹雪芹 | NULL |
| 施耐庵 | NULL |
+-----------+-------+
4 rows in set (0.00 sec)
#原先的方式无法查看 null 值
MariaDB [test1]> select * from nulll where price=null;
Empty set (0.00 sec)
#采用 is null 查看空值对应数据
MariaDB [test1]> select * from nulll where price is null;
+-----------+-------+
| name | price |
+-----------+-------+
| 曹雪芹 | NULL |
| 施耐庵 | NULL |
+-----------+-------+
2 rows in set (0.00 sec)
#采用 is not null 查看非空值对应数据
MariaDB [test1]> select * from nulll where price is not null;
+-----------+-------+
| name | price |
+-----------+-------+
| 吴承恩 | 0 |
| 罗贯中 | 0 |
+-----------+-------+
2 rows in set (0.00 sec)
_**1.MySQL 可以通过 LIKE …% 来进行模糊匹配。
2.MySQL 同样也支持其他正则表达式的匹配, MySQL 中使用 REGEXP 操作符来进行正则表达式匹配。**_
#查看 name 字段以水开头的所有数据
MariaDB [test1]> SELECT name FROM book WHERE name REGEXP '^水';
+-----------+
| name |
+-----------+
| 水浒传 |
+-----------+
1 row in set (0.00 sec)
#查看 name 字段所有以记结尾的数据
MariaDB [test1]> SELECT name FROM book WHERE name REGEXP '记 $';
+-----------+
| name |
+-----------+
| 西游记 |
+-----------+
1 row in set (0.00 sec)
#查看 name 字段以水开头或者结尾以记结尾的所有数据
MariaDB [test1]> SELECT name FROM book WHERE name REGEXP '^水|记 $';
+-----------+
| name |
+-----------+
| 水浒传 |
| 西游记 |
+-----------+
2 rows in set (0.00 sec)
==========================================================================
1.MySQL 事务主要用于处理操作量大,复杂度高的数据。
2.一般来说,事务是必须满足 4 个条件(ACID)::原子性(Atomicity,或称不可分割性)、一致性(Consistency)、隔离性(Isolation,又称独立性)、持久性(Durability)。
原子性:一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节。事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务从来没有执行过一样。
一致性:在事务开始之前和事务结束以后,数据库的完整性没有被破坏。这表示写入的资料必须完全符合所有的预设规则,这包含资料的精确度、串联性以及后续数据库可以自发性地完成预定的工作。
隔离性:数据库允许多个并发事务同时对其数据进行读写和修改的能力,隔离性可以防止多个事务并发执行时由于交叉执行而导致数据的不一致。事务隔离分为不同级别,包括读未提交(Read uncommitted)、读提交(read committed)、可重复读(repeatable read)和串行化(Serializable)。
持久性:事务处理结束后,对数据的修改就是永久的,即便系统故障也不会丢失。
3.MYSQL 事务处理主要有两种方法:
1、用 BEGIN, ROLLBACK, COMMIT 来实现
BEGIN 开始一个事务
ROLLBACK 事务回滚
COMMIT 事务确认
2、直接用 SET 来改变 MySQL 的自动提交模式:
SET AUTOCOMMIT=0 禁止自动提交
SET AUTOCOMMIT=1 开启自动提交
mysql> use RUNOOB;
Database changed
mysql> CREATE TABLE runoob_transaction_test( id int(5)) engine=innodb; # 创建数据表
Query OK, 0 rows affected (0.04 sec)
mysql> select * from runoob_transaction_test;
Empty set (0.01 sec)
mysql> begin; # 开始事务
Query OK, 0 rows affected (0.00 sec)
mysql> insert into runoob_transaction_test value(5);
Query OK, 1 rows affected (0.01 sec)
mysql> insert into runoob_transaction_test value(6);
Query OK, 1 rows affected (0.00 sec)
mysql> commit; # 提交事务
Query OK, 0 rows affected (0.01 sec)
mysql> select * from runoob_transaction_test;
+------+
| id |
+------+
| 5 |
| 6 |
+------+
2 rows in set (0.01 sec)
mysql> begin; # 开始事务
Query OK, 0 rows affected (0.00 sec)
mysql> insert into runoob_transaction_test values(7);
Query OK, 1 rows affected (0.00 sec)
mysql> rollback; # 回滚
Query OK, 0 rows affected (0.00 sec)
mysql> select * from runoob_transaction_test; # 因为回滚所以数据没有插入
+------+
| id |
+------+
| 5 |
| 6 |
+------+
2 rows in set (0.01 sec)
1.删除,添加或修改表字段
MariaDB [test1]> alter table book add a int;
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
#查看字段信息
MariaDB [test1]> show columns from book;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(255) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| date | date | YES | | NULL | |
| a | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
#用 add 添加字段
MariaDB [test1]> alter table book add b int;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [test1]> show columns from book;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(255) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| date | date | YES | | NULL | |
| b | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
#使用 drop 删除字段
MariaDB [test1]> alter table book drop b;
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [test1]> show columns from book;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(255) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| date | date | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
2.修改字段类型及名称
(1).如果需要修改字段类型及名称, 你可以在 ALTER 命令中使用 MODIFY 或 CHANGE 子句 。
(2).使用 CHANGE 子句, 语法有很大的不同。 在 CHANGE 关键字之后,紧跟着的是你要修改的字段名,然后指定新字段名及类型
#查看字段信息
MariaDB [test1]> show columns from book;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(255) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| date | date | YES | | NULL | |
| a | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
#使用 change 命令修改字段和字段类型
MariaDB [test1]> alter table book change a b varchar(255);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [test1]> show columns from book;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(255) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| date | date | YES | | NULL | |
| b | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
#使用 modify 命令修改字段和字段类型
MariaDB [test1]> alter table book modify b int;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [test1]> show columns from book;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(255) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| date | date | YES | | NULL | |
| b | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
3.更改表名
MariaDB [test1]> alter table book rename to books;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test1]> select * from books;
+--------------+-------+------------+------+
| name | price | date | b |
+--------------+-------+------------+------+
| 水浒传 | 88 | 2020-04-13 | NULL |
| 红楼梦 | 99 | 2020-04-13 | NULL |
| 西游记 | 100 | 2020-04-13 | NULL |
| 三国演艺 | 91 | 2020-04-14 | NULL |
+--------------+-------+------------+------+
4 rows in set (0.00 sec)
4.修改字段默认值
MariaDB [test1]> desc nulll;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(255) | NO | | NULL | |
| price | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
#修改默认值
MariaDB [test1]> alter table nulll alter price set default 1000;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test1]> desc nulll;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(255) | NO | | NULL | |
| price | int(11) | YES | | 1000 | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
#删除默认值
MariaDB [test1]> alter table nulll alter price drop default;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test1]> desc nulll;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(255) | NO | | NULL | |
| price | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
(1)看你的 mysql 现在已提供知什么存储引擎:
MariaDB [test1]> show engines;
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | Non-transactional engine with good performance and small data footprint | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| CSV | YES | Stores tables as CSV files | NO | NO | NO |
| ARCHIVE | YES | gzip-compresses tables for a low storage footprint | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | YES | Allows to access tables on other MariaDB servers, supports transactions and more | YES | NO | YES |
| Aria | YES | Crash-safe tables with MyISAM heritage | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.00 sec)
(2)看你的 mysql 当前默认的存储引擎
MariaDB [test1]> show variables like '%storage_engine%';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
| storage_engine | InnoDB |
+------------------------+--------+
2 rows in set (0.00 sec)
(3)看某个表用了什么引擎
MariaDB [test1]> show variables like '%storage_engine%';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
| storage_engine | InnoDB |
+------------------------+--------+
2 rows in set (0.00 sec)
MariaDB [test1]> show create table books;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| books | CREATE TABLE books
(
name
varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
price
int(11) DEFAULT NULL,
date
date DEFAULT NULL,
b
int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1.导出
[root@a ~]# mysqldump -uroot -p test1 > test1.sql
Enter password:
[root@a ~]# ll
total 8
-rw-------. 1 root root 1260 Jan 30 17:12 anaconda-ks.cfg
-rw-r--r--. 1 root root 4062 Apr 14 00:17 test1.sql
2.导入
MariaDB [(none)]> create database b;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use b
Database changed
MariaDB [b]> source /root/test1.sql;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
....此处省略...
MariaDB [b]> show tables;
+-------------+
| Tables_in_b |
+-------------+
| books |
| nulll |
| num |
| xs |
+-------------+
4 rows in set (0.00 sec)
3.备份所有数据库
[root@a ~]# mysqldump -uroot -p --all-databases > a.sql
Enter password:
[root@a ~]# ll
total 520
-rw-------. 1 root root 1260 Jan 30 17:12 anaconda-ks.cfg
-rw-r--r--. 1 root root 520847 Apr 14 00:25 a.sql
-rw-r--r--. 1 root root 4062 Apr 14 00:17 test1.sql
1.添加用户
1、本地环境
[root@a shell]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
2、以 root 用户登录 Mysql
[root@a ~]# mysql -uroot -pa
3、切换到 mysql 数据库
MariaDB [(none)]> use mysql
4、添加用户
//只允许指定 ip 连接
create user '新用户名'@'localhost' identified by '密码';
//允许所有 ip 连接(用通配符 %表示)
create user '新用户名'@'%' identified by '密码';
实例:
MariaDB [mysql]> create user 'Jack'@'localhost' identified by 'a';
Query OK, 0 rows affected (0.00 sec)
2.删除用户
删除用户
DROP USER username@localhost;
3.给用户授权
为新用户授权
//基本格式如下
grant all privileges on 数据库名.表名 to '新用户名'@'指定 ip' identified by '新用户密码' ;
//示例
//允许访问所有数据库下的所有表
grant all privileges on . to '新用户名'@'指定 ip' identified by '新用户密码' ;
//指定数据库下的指定表
grant all privileges on test.test to '新用户名'@'指定 ip' identified by '新用户密码' ;
6、设置用户操作权限
//设置用户拥有所有权限也就是管理员
grant all privileges on . to '新用户名'@'指定 ip' identified by '新用户密码' WITH GRANT OPTION;
//拥有查询权限
grant select on . to '新用户名'@'指定 ip' identified by '新用户密码' WITH GRANT OPTION;
//其它操作权限说明,select 查询 insert 插入 delete 删除 update 修改
//设置用户拥有查询插入的权限
grant select,insert on . to '新用户名'@'指定 ip' identified by '新用户密码' WITH GRANT OPTION;
//取消用户查询的查询权限
REVOKE select ON what FROM '新用户名';
实例:
mysql> grant all privileges on . to 'yangxin'@'%' identified by 'yangxin123456' with grant option;
all privileges:表示将所有权限授予给用户。也可指定具体的权限,如:SELECT、CREATE、DROP 等。
on:表示这些权限对哪些数据库和表生效,格式:数据库名.表名,这里写“*”表示所有数据库,所有表。如果我要指定将权限应用到 test 库的 user 表中,可以这么写:test.user
to:将权限授予哪个用户。格式:”用户名”@”登录 IP 或域名”。%表示没有限制,在任何主机都可以登录。比如:”yangxin”@”192.168.0.%”,表示 yangxin 这个用户只能在 192.168.0IP 段登录
identified by:指定用户的登录密码
with grant option:表示允许用户将自己的权限授权给其它用户
可以使用 GRANT 给用户添加权限,权限会自动叠加,不会覆盖之前授予的权限,比如你先给用户添加一个 SELECT 权限,后来又给用户添加了一个 INSERT 权限,那么该用户就同时拥有了 SELECT 和 INSERT 权限。
修改后刷新权限 FLUSH PRIVILEGES;未完待续…
评论