当前位置:Gxlcms > 数据库问题 > Mysql入门基础命令

Mysql入门基础命令

时间:2021-07-01 10:21:17 帮助过:13人阅读

查看特定表的全部字段
  1. mysql> desc db;
  2. +-----------------------+---------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-----------------------+---------------+------+-----+---------+-------+
  5. | Host | char(60) | NO | PRI | | |
  6. | Db | char(64) | NO | PRI | | |
  7. | User | char(16) | NO | PRI | | |
  8. | Select_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  9. | Insert_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  10. | Update_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  11. | Delete_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  12. | Create_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  13. | Drop_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  14. | Grant_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  15. | References_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  16. | Index_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  17. | Alter_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  18. | Create_tmp_table_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  19. | Lock_tables_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  20. | Create_view_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  21. | Show_view_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  22. | Create_routine_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  23. | Alter_routine_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  24. | Execute_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  25. | Event_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  26. | Trigger_priv | enum(‘N‘,‘Y‘) | NO | | N | |
  27. +-----------------------+---------------+------+-----+---------+-------+
  28. 22 rows in set (0.01 sec)
  29. #使用如下命令将建表语句全部列出来
  30. mysql> show create table db\G;
  31. *************************** 1. row ***************************
  32. Table: db
  33. Create Table: CREATE TABLE `db` (
  34. `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
  35. `Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
  36. `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
  37. `Select_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  38. `Insert_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  39. `Update_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  40. `Delete_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  41. `Create_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  42. `Drop_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  43. `Grant_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  44. `References_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  45. `Index_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  46. `Alter_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  47. `Create_tmp_table_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  48. `Lock_tables_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  49. `Create_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  50. `Show_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  51. `Create_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  52. `Alter_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  53. `Execute_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  54. `Event_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  55. `Trigger_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,
  56. PRIMARY KEY (`Host`,`Db`,`User`),
  57. KEY `User` (`User`)
  58. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT=‘Database privileges‘
  59. 1 row in set (0.00 sec)

1.4    查看当前登录的用户

  1. mysql> select user();
  2. +----------------+
  3. | user() |
  4. +----------------+
  5. | root@localhost |
  6. +----------------+
  7. 1 row in set (0.00 sec)

1.5    查看当前正在使用的数据库

  1. mysql> select database();
  2. +------------+
  3. | database() |
  4. +------------+
  5. | mysql |
  6. +------------+
  7. 1 row in set (0.00 sec)

1.6    创建一个新库

  1. mysql> create database zabbix;
  2. Query OK, 1 row affected (0.38 sec)

1.7  创建一个新表

  1. mysql> create table user(id int(4),name char(40),tel int(11));
  2. Query OK, 0 rows affected (0.19 sec)
  3. mysql> show create table user\G
  4. *************************** 1. row ***************************
  5. Table: user
  6. Create Table: CREATE TABLE `user` (
  7. `id` int(4) DEFAULT NULL,
  8. `name` char(40) DEFAULT NULL,
  9. `tel` int(11) DEFAULT NULL
  10. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  11. 1 row in set (0.00 sec)

1.8    查看当前mysql数据版本

  1. mysql> select version();
  2. +------------+
  3. | version() |
  4. +------------+
  5. | 5.6.40-log |
  6. +------------+
  7. 1 row in set (0.00 sec)

1.9    查看Mysql当前状态

  1. mysql> show status;
  2. +-----------------------------------------------+-------------+
  3. | Variable_name | Value |
  4. +-----------------------------------------------+-------------+
  5. | Aborted_clients | 0 |
  6. | Aborted_connects | 14 |
  7. | Binlog_cache_disk_use | 0 |
  8. | Binlog_cache_use | 0 |
  9. | Binlog_stmt_cache_disk_use | 0 |
  10. | Binlog_stmt_cache_use | 4 |
  11. | Bytes_received | 1343 |
  12. | Bytes_sent | 34853 |
  13. | Com_admin_commands | 0 |
  14. | Com_assign_to_keycache | 0 |
  15. | Com_alter_db | 0 |
  16. | Com_alter_db_upgrade | 0 |
  17. | Com_alter_event | 0 |
  18. | Com_alter_function | 0 |
  19. | Com_alter_procedure | 0 |
  20. | Com_alter_server | 0 |
  21. | Com_alter_table | 0 |
  22. | Com_alter_tablespace | 0 |
  23. | Com_alter_user | 0 |
  24. | Com_analyze | 0 |
  25. | Com_begin | 0 |
  26. | Com_binlog | 0 |
  27. | Com_call_procedure | 0 |
  28. | Com_change_db | 2 |

1.10    查看Mysql参数

  1. mysql> show variables\G;
  2. *************************** 1. row ***************************
  3. Variable_name: auto_increment_increment
  4. Value: 1
  5. *************************** 2. row ***************************
  6. Variable_name: auto_increment_offset
  7. Value: 1
  8. *************************** 3. row ***************************
  9. Variable_name: autocommit
  10. Value: ON
  11. *************************** 4. row ***************************
  12. Variable_name: automatic_sp_privileges
  13. Value: ON
  14. *************************** 5. row ***************************
  15. Variable_name: avoid_temporal_upgrade
  16. Value: OFF

1.11    修改Mysql的参数max_connect_errors

  1. mysql> show variables like ‘max_connect%‘;
  2. +--------------------+-------+
  3. | Variable_name | Value |
  4. +--------------------+-------+
  5. | max_connect_errors | 100 |
  6. | max_connections | 151 |
  7. +--------------------+-------+
  8. 2 rows in set (0.01 sec)
  9. mysql> set global max_connect_errors = 1000;
  10. Query OK, 0 rows affected (0.00 sec)
  11. mysql> show variables like ‘max_connect%‘;#类似shell命令行*通配符
  12. +--------------------+-------+
  13. | Variable_name | Value |
  14. +--------------------+-------+
  15. | max_connect_errors | 1000 |
  16. | max_connections | 151 |
  17. +--------------------+-------+
  18. 2 rows in set (0.00 sec)
  19. #set global临时更改参数,如果永久修改则需要在/etc/my.cnf配置文件中修改

1.12    查看当前Mysql服务器的队列

  1. mysql> show processlist;
  2. +----+------+------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
  3. | Id | User | Host | db | Command | Time | State | Info |
  4. +----+------+------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
  5. | 2 | rep | 10.0.0.251:55984 | NULL | Binlog Dump | 3109 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL |
  6. | 23 | root | localhost | zabbix | Query | 0 | init | show processlist |
  7. +----+------+------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
  8. 2 rows in set (0.00 sec)
  9. #查看当前Mysql在做什么操作,也可以是否有锁表

1.13    创建一个普通用户并授权

  1. mysql> grant all on *.* to zabbix identified by ‘123456‘;
  2. Query OK, 0 rows affected (0.00 sec)
  3. #本地创建一个zabbix用户,授权所有权限
  4. mysql> grant all on zabbix.* to ‘zabbix01‘@‘10.0.0.251‘ identified by ‘123456‘;
  5. Query OK, 0 rows affected (0.01 sec)
  6. #指定特定的网络机器登录mysql数据库并授权zabbix数据库下的所有表
  7. mysql> grant all on zabbix.* to ‘zabbix02‘@‘%‘ identified by ‘123456‘;
  8. Query OK, 0 rows affected (0.00 sec)
  9. #指定所有主机登录mysql数据库并授权zabbix数据库下的所有表
  10. mysql> select user,host from mysql.user where user like ‘zabbix%‘;
  11. +----------+------------+
  12. | user | host |
  13. +----------+------------+
  14. | zabbix | % |
  15. | zabbix02 | % |
  16. | zabbix01 | 10.0.0.251 |
  17. +----------+------------+
  18. 3 rows in set (0.00 sec)

二、Mysql基本操作

2.1    查询语句

  1. 第一种查询方式:
  2. mysql> select count(*) from mysql.user;
  3. +----------+
  4. | count(*) |
  5. +----------+
  6. | 7 |
  7. +----------+
  8. 1 row in set (0.00 sec)
  9. 第二种查询方式:
  10. mysql> select * from mysql.user;
  11. mysql> select user,host from mysql.user;
  12. +-----------+------------+
  13. | user | host |
  14. +-----------+------------+
  15. | zabbix | % |
  16. | zabbix02 | % |
  17. | rep | 10.0.0.% |
  18. | zabbix01 | 10.0.0.251 |
  19. | root | 127.0.0.1 |
  20. | root | localhost |
  21. | wordpress | localhost |
  22. +-----------+------------+
  23. 7 rows in set (0.00 sec)
  24. mysql> select user,host from mysql.user where user like ‘zabbix%‘;
  25. +----------+------------+
  26. | user | host |
  27. +----------+------------+
  28. | zabbix | % |
  29. | zabbix02 | % |
  30. | zabbix01 | 10.0.0.251 |
  31. +----------+------------+
  32. 3 rows in set (0.00 sec)

2.2    插入语句

  1. mysql> insert into zabbix.user values(3,‘test‘,1111111111),(4,‘boy‘,222222222);
  2. Query OK, 2 rows affected (0.00 sec)
  3. Records: 2 Duplicates: 0 Warnings: 0
  4. mysql> select * from zabbix.user where name=‘test‘ or name=‘boy‘;
  5. +------+------+------------+
  6. | id | name | tel |
  7. +------+------+------------+
  8. | 3 | test | 1111111111 |
  9. | 4 | boy | 222222222 |
  10. +------+------+------------+
  11. 2 rows in set (0.00 sec)

2.3    更新表语句

  1. mysql> update zabbix.user set name=‘girl‘ where id=3;
  2. Query OK, 1 row affected (1.69 sec)
  3. Rows matched: 1 Changed: 1 Warnings: 0
  4. mysql> select * from zabbix.user where id=3;
  5. +------+------+------------+
  6. | id | name | tel |
  7. +------+------+------------+
  8. | 3 | girl | 1111111111 |
  9. +------+------+------------+
  10. 1 row in set (0.00 sec)

2.4    清空表语句

  1. mysql> truncate table zabbix.user;
  2. Query OK, 0 rows affected (0.09 sec)
  3. mysql> select * from zabbix.user;
  4. Empty set (0.00 sec)

2.5    删除表语句

  1. mysql> drop table zabbix.user;
  2. Query OK, 0 rows affected (0.34 sec)
  3. mysql> show tables;
  4. Empty set (0.00 sec)

2.6    删除数据库

  1. mysql> show databases like ‘zabbix%‘;
  2. +--------------------+
  3. | Database (zabbix%) |
  4. +--------------------+
  5. | zabbix |
  6. +--------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> drop database zabbix;
  9. Query OK, 0 rows affected (0.12 sec)
  10. mysql> show databases like ‘zabbix%‘;
  11. Empty set (0.00 sec)

三、Mysql数据备份与恢复

3.1    Mysql备份

  1. [root@web01 ~]# mysqldump -u root -p123456 wordpress > /tmp/wordpress_
  2. $(date +%F).sql
  3. Warning: Using a password on the command line interface can be insecure.
  4. [root@web01 ~]# ls -l /tmp/
  5. total 784
  6. -rw-r--r-- 1 root root 801875 Nov 11 17:33 wordpress_2018-11-11.sql

3.2    Mysql恢复

  1. #删除原wordpress数据库
  2. [root@web01 ~]# mysql -u root -p123456 -e ‘drop database wordpress;‘
  3. Warning: Using a password on the command line interface can be insecure.
  4. #查看原wordpress数据库是否删除成功
  5. [root@web01 ~]# mysql -u root -p123456 -e "show databases";
  6. Warning: Using a password on the command line interface can be insecure.
  7. +--------------------+
  8. | Database |
  9. +--------------------+
  10. | information_schema |
  11. | dev |
  12. | guohua |
  13. | jumpserver |
  14. | mysql |
  15. | oldboy |
  16. | oldgirl |
  17. | performance_schema |
  18. | spdb |
  19. +--------------------+
  20. #创建空的wordpress数据库
  21. [root@web01 ~]# mysql -u root -p123456 -e "create database wordpress";
  22. Warning: Using a password on the command line interface can be insecure.
  23. #查看新的wordpress数据是否新建完成
  24. [root@web01 ~]# mysql -u root -p123456 -e "show databases like ‘wordpress%‘";
  25. Warning: Using a password on the command line interface can be insecure.
  26. +-----------------------+
  27. | Database (wordpress%) |
  28. +-----------------------+
  29. | wordpress |
  30. +-----------------------+
  31. #从以备份的wordpress恢复wordpres数据库
  32. [root@web01 ~]# mysql -u root -p123456 wordpress < /tmp/wordpress_2018-11-11.sql
  33. Warning: Using a password on the command line interface can be insecure.
  34. #检查wordpress库是否恢复成功
  35. [root@web01 ~]# mysql -u root -p123456 -e "use wordpress;show tables;"
  36. Warning: Using a password on the command line interface can be insecure.
  37. +-------------------------+
  38. | Tables_in_wordpress |
  39. +-------------------------+
  40. | wolf_commentmeta |
  41. | wolf_comments |
  42. | wolf_links |
  43. | wolf_options |
  44. | wolf_postmeta |
  45. | wolf_posts |
  46. | wolf_term_relationships |
  47. | wolf_term_taxonomy |
  48. | wolf_termmeta |
  49. | wolf_terms |
  50. | wolf_usermeta |
  51. | wolf_users |
  52. +-------------------------+

Mysql入门基础命令

标签:数据备份与恢复   incr   mil   wait   基础命令   slave   databases   ESS   second   

人气教程排行