为什么80%的码农都做不了架构师?>>>
▲ MySQL创建一个普通用户并授权
说明 默认有一个root是超级管理员,不可能是所有的开发人员或者是某个网站.假如我们现在要安装一个程序,做一个网站,那么这个网站肯定是要去连接MySQL, 连接MySQL就需要用户名和密码,MySQL里面可以跑多个库,每个库里面有很多个表,所以需要单独给用户做一些授权,只需要对某个数据库有授权,或者是对数据库的某个表有权限.
●授权命令如下:
mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.05 sec)说明 grant 是授权的意思;all 表示所有的权限(读、写、查询、删除); *.* 前者表示所有的数据库,后者表示所有的表;identified by后面跟密码,用单引号括起来;user1用户指定是127.0.0.1 IP的user1.
● user1 用户登录mysql
[root@root-02 ~]# mysql -uuser1 -p123456 -h127.0.0.1
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.35 MySQL Community Server (GPL)Copyright (c) 2000, 2016, 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 grants for user1@'127.0.0.1';
常用sql语句
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for user1@127.0.0.1 |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
● 查询语句
说明 mysql.user表示MySQL库的user表,count(*)表示表中共有多少行.
第一种形式:
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set (0.00 sec)
● select * from mysql.db\G; ---表示查询MySQL库的db表中的所有数据,这种查询方式不建议经常使用,若数据库表行数非常多的话,查询起来会很慢,并且很耗费资源.
第二种形式:
mysql> select * from mysql.db\G;
*************************** 1. row ***************************Host: %Db: testUser: Select_priv: YInsert_priv: YUpdate_priv: YDelete_priv: YCreate_priv: YDrop_priv: YGrant_priv: NReferences_priv: YIndex_priv: YAlter_priv: Y
Create_tmp_table_priv: YLock_tables_priv: YCreate_view_priv: YShow_view_priv: YCreate_routine_priv: YAlter_routine_priv: NExecute_priv: NEvent_priv: YTrigger_priv: Y
*************************** 2. row ***************************Host: %Db: test\_%User: Select_priv: YInsert_priv: YUpdate_priv: YDelete_priv: YCreate_priv: YDrop_priv: YGrant_priv: NReferences_priv: YIndex_priv: YAlter_priv: Y
Create_tmp_table_priv: YLock_tables_priv: YCreate_view_priv: YShow_view_priv: YCreate_routine_priv: YAlter_routine_priv: NExecute_priv: NEvent_priv: YTrigger_priv: Y
2 rows in set (0.00 sec)ERROR:
No query specified
● 查询mysql库的单个字段
查询mysql库的单个字段命令如下:
mysql> select db from mysql.db;
+---------+
| db |
+---------+
| test |
| test\_% |
+---------+
2 rows in set (0.00 sec)
● 查询mysql多个字段
查询mysql两个字段的命令如下:
mysql> select db,user from mysql.db;
+---------+------+
| db | user |
+---------+------+
| test | |
| test\_% | |
+---------+------+
2 rows in set (0.00 sec)
● 插入一行
说明 创建了db1.tl表,t1表里面目前是没有数据的
mysql> desc db1.t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(4) | YES | | NULL | |
| name | char(40) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> select * from db1.t1;
Empty set (0.00 sec)
现在在t1表插入一条数据,再查看t1表就有数据了
mysql> insert into db1.t1 values (1, 'abc');
Query OK, 1 row affected (0.02 sec)mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | abc |
| 1 | quan |
| 1 | dong |
+------+------+
3 rows in set (0.00 sec)
● 更改表的某一行
说明 Mysql表里存放的数据支持更改某个字段.
更改t1表的字段命令如下:
mysql> update db1.t1 set name='anna' where id=1;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | anna |
| 1 | anna |
| 1 | anna |
+------+------+
3 rows in set (0.00 sec)
● 清空某个表的数据
说明 有时候我们只是想清空表的数据,但并不想删除表
清空t1表的数据命令如下:
mysql> truncate table db1.t1;
Query OK, 0 rows affected (0.08 sec)mysql> select * from db1.t1;
Empty set (0.00 sec)
● 删除表
说明 当某个表已经不需要了,就可以删除掉
删除表的命令如下:
mysql> drop table db1.t1;
Query OK, 0 rows affected (0.01 sec)
● 删除数据库
说明 能删除表,当然也能删除数据库
删除数据库的命令如下:
mysql> drop database db1;
MySQL数据库备份恢复
Query OK, 0 rows affected (0.00 sec)
▲ mysqldump 是数据库备份的命令 (可以备份一个库,也可以备份一个表)
● 备份库
说明 /tmp/mysql.sql是mysql库备份文件.
备份mysql库的命令如下:
[root@root-02 ~]# mysqldump -uroot -p123456 mysql >/tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
● 恢复我mysql库数据
[root@root-02 ~]# mysql -uroot -p123456 mysql Warning: Using a password on the command line interface can be insecure.
● 备份表
说明 备份表前面写库名 然后跟表名
备份表命令如下:
[root@root-02 ~]# mysqldump -uroot -p123456 mysql user >/tmp/user.sql;
Warning: Using a password on the command line interface can be insecure.
● 恢复表数据
说明 恢复表数据,不需要加表名,只需库名即可
恢复表数据命令如下:
[root@root-02 ~]# mysql -uroot -p123456 mysql Warning: Using a password on the command line interface can be insecure.
● 备份所有的库
说明 -A选项表示所有的意思
备份所有库的命令如下:
[root@root-02 ~]# mysqldump -uroot -p123456 -A > /tmp/mysql_all.sql
Warning: Using a password on the command line interface can be insecure.
●只备份表的结构
说明 假如我们有个需求,不需要表的数据,只需要创建表的语句. 加上-d选项
备份表结构命令如下:
[root@root-02 ~]# mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
扩展 SQL语句教程 http://blog.51cto.com/zt/206
什么是事务?事务的特性有哪些? http://blog.csdn.net/yenange/article/details/7556094
根据binlog恢复指定时间段的数据 http://www.centoscn.com/mysql/2015/0204/4630.html
mysql字符集调整 http://xjsunjie.blog.51cto.com/999372/1355013
使用xtrabackup备份innodb引擎的数据库 http://www.aminglinux.com/bbs/thread-956-1-1.html
innobackupex 备份 Xtrabackup 增量备份 http://www.aminglinux.com/bbs/thread-1012-1-1.html