博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL常用操作(下)
阅读量:6983 次
发布时间:2019-06-27

本文共 11053 字,大约阅读时间需要 36 分钟。

MySQL用户管理

MySQL常用操作(下)

1.创建用户:grant命令

mysql> grant all on *.* to 'user1'@'127.0.0.1'  identified by '123456a';Query OK, 0 rows affected (0.00 sec)

测试

[root@weix-01 mysql]# mysql -uuser1 -p123456a -h127.0.0.1Warning: 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 3Server version: 5.6.36 MySQL Community Server (GPL)Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all on *.* to 'user1'@'localhost'  identified by '123456a';Query OK, 0 rows affected (0.00 sec)mysql> quitBye[root@weix-01 mysql]# mysql -uuser1 -p123456aWarning: 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 8Server version: 5.6.36 MySQL Community Server (GPL)Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

2.查看授权:

mysql> show grants;+-----------------------------------------------------------------------------------------------------------------------+| Grants for user1@localhost                                                                                            |+-----------------------------------------------------------------------------------------------------------------------+| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'localhost' IDENTIFIED BY PASSWORD '*B012E8731FF1DF44F3D8B26837708985278C3CED' |+-----------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)mysql>
mysql> show grants for user1@127.0.0.1    -> ;+-----------------------------------------------------------------------------------------------------------------------+| Grants for user1@127.0.0.1                                                                                            |+-----------------------------------------------------------------------------------------------------------------------+| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*B012E8731FF1DF44F3D8B26837708985278C3CED' |+-----------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)

常用mysql语句

MySQL常用操作(下)

1.查看一个表有多少行:

mysql> select count(*) from mysql.user;+----------+| count(*) |+----------+|        8 |+----------+1 row in set (0.01 sec)

2.查看字段:

mysql> select * from mysql.db\G;*************************** 1. row ***************************                 Host: %                   Db: test                 User:          Select_priv: Y          Insert_priv: Y          Update_priv: Y          Delete_priv: Y          Create_priv: Y            Drop_priv: Y           Grant_priv: N      References_priv: Y           Index_priv: Y           Alter_priv: YCreate_tmp_table_priv: Y     Lock_tables_priv: Y     Create_view_priv: Y       Show_view_priv: Y  Create_routine_priv: Y   Alter_routine_priv: N         Execute_priv: N           Event_priv: Y         Trigger_priv: Y*************************** 2. row ***************************                 Host: %                   Db: test\_%                 User:          Select_priv: Y          Insert_priv: Y          Update_priv: Y          Delete_priv: Y          Create_priv: Y            Drop_priv: Y           Grant_priv: N      References_priv: Y           Index_priv: Y           Alter_priv: YCreate_tmp_table_priv: Y     Lock_tables_priv: Y     Create_view_priv: Y       Show_view_priv: Y  Create_routine_priv: Y   Alter_routine_priv: N         Execute_priv: N           Event_priv: Y         Trigger_priv: Y2 rows in set (0.00 sec)ERROR:No query specified

3.查看固定字段:

mysql> select db from mysql.db;+---------+| db      |+---------+| test    || test\_% |+---------+2 rows in set (0.01 sec)mysql>
mysql> select db,user from mysql.db;+---------+------+| db      | user |+---------+------+| test    |      || test\_% |      |+---------+------+2 rows in set (0.01 sec)

4.模糊查询:ip来源

mysql> select * from mysql.db where host like '127.0.%'\G;Empty set (0.00 sec)ERROR:No query specifiedmysql> select * from mysql.db where host like '%'\G;*************************** 1. row ***************************                 Host: %                   Db: test                 User:          Select_priv: Y          Insert_priv: Y          Update_priv: Y          Delete_priv: Y          Create_priv: Y            Drop_priv: Y           Grant_priv: N      References_priv: Y           Index_priv: Y           Alter_priv: YCreate_tmp_table_priv: Y     Lock_tables_priv: Y     Create_view_priv: Y       Show_view_priv: Y  Create_routine_priv: Y   Alter_routine_priv: N         Execute_priv: N           Event_priv: Y         Trigger_priv: Y*************************** 2. row ***************************                 Host: %                   Db: test\_%                 User:          Select_priv: Y          Insert_priv: Y          Update_priv: Y          Delete_priv: Y          Create_priv: Y            Drop_priv: Y           Grant_priv: N      References_priv: Y           Index_priv: Y           Alter_priv: YCreate_tmp_table_priv: Y     Lock_tables_priv: Y     Create_view_priv: Y       Show_view_priv: Y  Create_routine_priv: Y   Alter_routine_priv: N         Execute_priv: N           Event_priv: Y         Trigger_priv: Y2 rows in set (0.00 sec)ERROR:No query specified

5.插入数据:

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.01 sec)mysql> select * from db1.t1;Empty set (0.01 sec)mysql> insert into db1.t1 values (1,'abc');Query OK, 1 row affected (0.01 sec)mysql> select * from db1.t1;+------+------+| id   | name |+------+------+|    1 | abc  |+------+------+1 row in set (0.00 sec)

6.update:可用来修改

mysql> update db1.t1 set name='aaa' where id=1;Query OK, 1 row affected (0.01 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from db1.t1;+------+------+| id   | name |+------+------+|    1 | aaa  |+------+------+1 row in set (0.00 sec)

7.清空一个表:结构不动

mysql> truncate db1.t1;Query OK, 0 rows affected (0.02 sec)mysql> select * from db1.t1;Empty set (0.00 sec)

8.把结构也删除:

mysql> drop table t1;Query OK, 0 rows affected (0.01 sec)mysql> select * from db1.t1;ERROR 1146 (42S02): Table 'db1.t1' doesn't existmysql> drop database db1;Query OK, 0 rows affected (0.01 sec)

mysql数据库备份恢复

MySQL常用操作(下)

1.备份库:

[root@weix-01 mysql]# mysqldump -uroot -pwei914 mysql > /tmp/mysqlbak.sqlWarning: Using a password on the command line interface can be insecure.

2.恢复数据库:可以恢复到指定库

[root@weix-01 mysql]# mysql -uroot -pwei914 -e "create database mysql2"Warning: Using a password on the command line interface can be insecure.[root@weix-01 mysql]# mysql -uroot -pwei914 mysql2 < /tmp/mysqlbak.sqlWarning: Using a password on the command line interface can be insecure.[root@weix-01 mysql]# mysql -uroot -pwei914 mysql2Warning: Using a password on the command line interface can be insecure.Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -AWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 15Server version: 5.6.36 MySQL Community Server (GPL)Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> select database();+------------+| database() |+------------+| mysql2     |+------------+1 row in set (0.01 sec)mysql> show tables;+---------------------------+| Tables_in_mysql2          |+---------------------------+| columns_priv              || db                        || event                     || func                      || general_log               || help_category             || help_keyword              || help_relation             || help_topic                || innodb_index_stats        || innodb_table_stats        || ndb_binlog_index          || plugin                    || proc                      || procs_priv                || proxies_priv              || servers                   || slave_master_info         || slave_relay_log_info      || slave_worker_info         || slow_log                  || tables_priv               || time_zone                 || time_zone_leap_second     || time_zone_name            || time_zone_transition      || time_zone_transition_type || user                      |+---------------------------+28 rows in set (0.00 sec)mysql> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+---------------------------+| Tables_in_mysql           |+---------------------------+| columns_priv              || db                        || event                     || func                      || general_log               || help_category             || help_keyword              || help_relation             || help_topic                || innodb_index_stats        || innodb_table_stats        || ndb_binlog_index          || plugin                    || proc                      || procs_priv                || proxies_priv              || servers                   || slave_master_info         || slave_relay_log_info      || slave_worker_info         || slow_log                  || tables_priv               || time_zone                 || time_zone_leap_second     || time_zone_name            || time_zone_transition      || time_zone_transition_type || user                      |+---------------------------+28 rows in set (0.00 sec)

3.备份表:

[root@weix-01 mysql]# mysqldump -uroot -pwei914 mysql user > /tmp/user.sqlWarning: Using a password on the command line interface can be insecure.

4.恢复表:恢复表时不用加表名字

[root@weix-01 mysql]# mysql -uroot -pwei914 mysql2 < /tmp/user.sqlWarning: Using a password on the command line interface can be insecure.

5.备份所有库:-A

[root@weix-01 mysql]# mysqldump -uroot -pwei914 -A > /tmp/mysql_all.sqlWarning: Using a password on the command line interface can be insecure.

6.只备份表结构:-d

[root@weix-01 mysql]# mysqldump -uroot -pwei914 -d mysql2 > /tmp/mysql2.sqlWarning: Using a password on the command line interface can be insecure.

转载于:https://blog.51cto.com/13517254/2090360

你可能感兴趣的文章
经典测试用例
查看>>
3月16日学习内容整理:metaclass
查看>>
Vue和其他框架的区别
查看>>
【iPhone5概念机最新效果图曝光】
查看>>
深入浅出:5G和HTTP
查看>>
ES6-let和const命令
查看>>
java反射,代码优化
查看>>
python中获取当前所有的logger
查看>>
关于BDD100k数据输入处理mask变为56*56
查看>>
Reveal使用心法
查看>>
Directx11教程(18) D3D11管线(7)
查看>>
JVM系列二:GC策略&内存申请、对象衰老
查看>>
正则的[]与()
查看>>
OCP换题库了,052新加的考题及答案整理-第16题
查看>>
OCP新题,2019题库出现大量新题,062-第22题
查看>>
MySQL查询in操作 查询结果按in集合顺序显示(转)
查看>>
JavaScript中map函数和filter的简单举例
查看>>
RocketMq消息队列使用
查看>>
Dynamics CRM 提示“操作无效”
查看>>
09.15,一维数组,冒泡排序
查看>>