前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学习笔记0521----mysql管理

学习笔记0521----mysql管理

作者头像
嘻哈记
发布2020-11-24 10:27:25
9280
发布2020-11-24 10:27:25
举报
文章被收录于专栏:运维学习交流运维学习交流

mysql管理

  • 预习笔记
    • 1. 设置更改root密码
      • 1.1 设置MySQL的root密码
        • 1.1.1 添加MySQL的bin目录到系统环境变量
        • 1.1.2 检查MySQL是否启动
        • 1.1.3 设置root密码
      • 1.2 修改MySQL的root密码
        • 1.2.1 知道root密码更改密码
        • 1.2.1 忘记root密码更改密码
    • 2. 连接MySQL方法
      • 2.1 mysql -uroot -plinux002
      • 2.2 mysql -uroot -plinux002 -h127.0.0.1 -P3306
      • 2.3 mysql -uroot -plinux002 -S/tmp/mysql.sock
      • 2.4 mysql -uroot -plinux002 -e "show databases"
    • 3. MySQL常用命令
      • 3.1 查询库 (show databases;)
      • 3.2 切换库(use mysql;)
      • 3.3 查看库里的表(show tables)
      • 3.4 查看表里的字段(desc user;)
      • 3.5 查看建表语句(show create table user\G;)
      • 3.6 查看当前用户(select user();)
      • 3.7 查看当前使用的数据库(select database();)
      • 3.8 创建库(create database db1;)
      • 3.9 创建表(create table t1(\`id\` int(4), \`name\` char(40)); )
      • 3.10 查看当前数据库版本(select version();)
      • 3.11 查看数据库状态 (show status;)
      • 3.11 查看表里的参数(show variables;)
      • 3.12 修改参数(set global max_connect_errors=1000;)
      • 3.13 查看队列(show processlist;)
    • 4. MySQL用户管理
      • 4.1 创建好用户提示无法登陆
      • 4.2 创建的用户需要添加localhost的权限才可以登陆
    • 5. 常用sql语句
      • 5.1 统计一个表中有多少行
      • 5.2 显示一个表中所有的内容
      • 5.3 查询一个段或者多个段的数据
      • 5.4 插入一行数据
      • 5.5 更改表中数据
      • 5.6 清空表中的数据,保留表结构
      • 5.7 删除表
      • 5.8 删除库
    • 6. MySQL数据库备份恢复
      • 6.1 备份库
      • 6.2 恢复库
      • 6.3 备份表
      • 6.4 恢复表
      • 6.5 备份所有库
      • 6.6 只备份表结构
  • 课后总结

预习笔记

13.1 设置更改root密码 13.2 连接mysql 13.3 mysql常用命令 13.4 mysql用户管理 13.5 常用sql语句 13.6 mysql数据库备份恢复 使用xtrabackup备份innodb引擎的数据库 innobackupex 备份 Xtrabackup 增量备份http://zhangguangzhi.top/2017/08/23/innobackex%E5%B7%A5%E5%85%B7%E5%A4%87%E4%BB%BDmysql%E6%95%B0%E6%8D%AE/#%E4%B8%89%E3%80%81%E5%BC%80%E5%A7%8B%E6%81%A2%E5%A4%8Dmysql 相关视频 链接:http://pan.baidu.com/s/1miFpS9M 密码:86dx 链接:http://pan.baidu.com/s/1o7GXBBW 密码:ue2f 扩展 mysql5.7 root密码更改 http://www.apelearn.com/bbs/thread-7289-1-1.html myisam 和innodb引擎对比 http://www.pureweber.com/article/myisam-vs-innodb/ mysql 配置详解: http://blog.linuxeye.com/379.html mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html 同学分享的亲身mysql调优经历: http://www.apelearn.com/bbs/thread-11281-1-1.html SQL语句教程 http://www.runoob.com/sql/sql-tutorial.html 什么是事务?事务的特性有哪些? http://blog.csdn.net/yenange/article/details/7556094 根据binlog恢复指定时间段的数据 https://blog.csdn.net/lilongsy/article/details/74726002 mysql字符集调整 http://xjsunjie.blog.51cto.com/999372/1355013

1. 设置更改root密码

1.1 设置MySQL的root密码

1.1.1 添加MySQL的bin目录到系统环境变量

MySQL的安装路径为:/usr/local/mysql/,可执行文件在bin目录下,此目录并未添加到系统的环境变量中,所以要使用mysql命令,需要把 /usr/local/mysql/bin/ 目录添加到系统的环境变量中。

代码语言:javascript
复制
## 临时添加到系统环境变量 ##
[root@linux-02 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@linux-02 ~]# export PATH=$PATH:/usr/local/mysql/bin/         
[root@linux-02 ~]# !echo
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin/:/usr/local/mysql/bin/

## 永久添加到系统环境变量 ##
[root@linux-02 ~]# vim /etc/profile
## 添加以下内容 ##
export PATH=$PATH:/usr/local/mysql/bin/ 
[root@linux-02 ~]# source /etc/profile
1.1.2 检查MySQL是否启动

在使用mysql命令的时候需要先启动mysql,检查下系统是否有启动MySQL,如果没有启动,使用 /etc/init.d/mysqld start 启动MySQL。

代码语言:javascript
复制
[root@linux-02 ~]# ps aux | grep mysql
root       7050  0.0  0.0 115644  1816 ?        S    5月20   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/linux-02.pid
mysql      7255  0.0 24.4 1303012 455456 ?      Sl   5月20   0:11 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=linux-02.err --pid-file=/data/mysql/linux-02.pid --socket=/tmp/mysql.sock
root       7949  0.0  0.0 112728   988 pts/0    S+   00:20   0:00 grep --color=auto mysql
[root@linux-02 ~]# 
1.1.3 设置root密码

mysql启动,第一次进MySQL数据库root是没有密码,为了安全起见,还是需要给root设置一个密码。使用 mysqladmin -uroot password ‘mysql’ 给root用户添加密码。

代码语言:javascript
复制
[root@linux-02 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> quit
Bye


## 给MySQL中的root用户添加密码 ##
[root@linux-02 ~]# mysqladmin -uroot password 'mysql' 
Warning: Using a password on the command line interface can be insecure.
[root@linux-02 ~]# mysql -uroot -pmysql
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 4
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> quit
Bye

1.2 修改MySQL的root密码

1.2.1 知道root密码更改密码

在使用MySQL的时候,我们知道root的密码的时候,如果要更改密码可以使用mysqladmin,具体操作如下。

代码语言:javascript
复制
[root@linux-02 ~]# mysqladmin -uroot -p'mysql' password '123456'   
Warning: Using a password on the command line interface can be insecure.
[root@linux-02 ~]# mysql -uroot -p123456
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 6
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> quit
Bye
[root@linux-02 ~]# 
1.2.1 忘记root密码更改密码
  • 如果忘记MySQL的root密码,更改mysql密码需要进行以下几步操作:
  • 修改/etc/my.cnf ,在mysqld模块添加:skip-grant
  • 重启MySQL服务:/etc/init.d/mysqld restart
  • 使用 mysql -uroot -p 无密码进入MySQL
  • 切换到mysql的数据库:use mysql ;
  • 执行命令:update user set password=password(‘linux002’) where user=‘root’;
  • 退出mysql:quit
  • 修改/etc/my.cnf ,删除刚才添加的skip-grant
  • 重启MySQL服务
代码语言:javascript
复制
[root@linux-02 ~]# vim /etc/my.cnf
 ## 添加如下内容 ##
 skip-grant

[root@linux-02 ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

[root@linux-02 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 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> update user set password=password('linux002') where user='root'; 
Query OK, 0 rows affected (0.00 sec)
Rows matched: 4  Changed: 0  Warnings: 0
mysql> quit
Bye


[root@linux-02 ~]# vim /etc/my.cnf
 ## 删除添加的内容,可以加#号注释 ##
 # skip-grant
[root@linux-02 ~]# !servi
service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

[root@linux-02 ~]# mysql -uroot -plinux002
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 1
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> quit
Bye
[root@linux-02 ~]# 

2. 连接MySQL方法

2.1 mysql -uroot -plinux002

默认连接本机的mysql,端口3306。

代码语言:javascript
复制
[root@linux-02 ~]# mysql -uroot -plinux002
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 2
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> quit
Bye

2.2 mysql -uroot -plinux002 -h127.0.0.1 -P3306

使用此方式常用于远程连接MySQL。

代码语言:javascript
复制
[root@linux-02 ~]# mysql -uroot -plinux002 -h127.0.0.1 -P3306
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 3
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> quit
Bye
[root@linux-02 ~]# 

2.3 mysql -uroot -plinux002 -S/tmp/mysql.sock

指定sock文件连接MySQL,仅用于本机。

代码语言:javascript
复制
[root@linux-02 ~]# mysql -uroot -plinux002  -S/tmp/mysql.sock
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 4
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> quit
Bye
[root@linux-02 ~]# 

2.4 mysql -uroot -plinux002 -e “show databases”

常用于脚本中。

代码语言:javascript
复制
[root@linux-02 ~]# mysql -uroot -plinux002  -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
[root@linux-02 ~]# 

3. MySQL常用命令

3.1 查询库 (show databases;)

代码语言:javascript
复制
[root@linux-02 ~]# mysql -uroot -plinux002  -S/tmp/mysql.sock
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 6
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql>  

3.2 切换库(use mysql;)

代码语言:javascript
复制
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> 

3.3 查看库里的表(show tables)

代码语言:javascript
复制
mysql> 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)

mysql> 

3.4 查看表里的字段(desc user;)

代码语言:javascript
复制
mysql> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key | Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                       |       |
| User                   | char(16)                          | NO   | PRI |                       |       |
| Password               | char(41)                          | NO   |     |                       |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Delete_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Create_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Drop_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Reload_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Shutdown_priv          | enum('N','Y')                     | NO   |     | N                     |       |
| Process_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| File_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Grant_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| References_priv        | enum('N','Y')                     | NO   |     | N                     |       |
| Index_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Alter_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Show_db_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Super_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Create_tmp_table_priv  | enum('N','Y')                     | NO   |     | N                     |       |
| Lock_tables_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Execute_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Repl_slave_priv        | enum('N','Y')                     | NO   |     | N                     |       |
| Repl_client_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Create_view_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Show_view_priv         | enum('N','Y')                     | NO   |     | N                     |       |
| Create_routine_priv    | enum('N','Y')                     | NO   |     | N                     |       |
| Alter_routine_priv     | enum('N','Y')                     | NO   |     | N                     |       |
| Create_user_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Event_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Trigger_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Create_tablespace_priv | enum('N','Y')                     | NO   |     | N                     |       |
| ssl_type               | enum('','ANY','X509','SPECIFIED') | NO   |     |                       |       |
| ssl_cipher             | blob                              | NO   |     | NULL                  |       |
| x509_issuer            | blob                              | NO   |     | NULL                  |       |
| x509_subject           | blob                              | NO   |     | NULL                  |       |
| max_questions          | int(11) unsigned                  | NO   |     | 0                     |       |
| max_updates            | int(11) unsigned                  | NO   |     | 0                     |       |
| max_connections        | int(11) unsigned                  | NO   |     | 0                     |       |
| max_user_connections   | int(11) unsigned                  | NO   |     | 0                     |       |
| plugin                 | char(64)                          | YES  |     | mysql_native_password |       |
| authentication_string  | text                              | YES  |     | NULL                  |       |
| password_expired       | enum('N','Y')                     | NO   |     | N                     |       |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
43 rows in set (0.00 sec)

mysql> desc user;

3.5 查看建表语句(show create table user\G;)

代码语言:javascript
复制
mysql> show create table user\G;
*************************** 1. row ***************************
       Table: user
Create Table: CREATE TABLE `user` (
  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
  `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
  `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
  `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '',
  `ssl_cipher` blob NOT NULL,
  `x509_issuer` blob NOT NULL,
  `x509_subject` blob NOT NULL,
  `max_questions` int(11) unsigned NOT NULL DEFAULT '0',
  `max_updates` int(11) unsigned NOT NULL DEFAULT '0',
  `max_connections` int(11) unsigned NOT NULL DEFAULT '0',
  `max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
  `plugin` char(64) COLLATE utf8_bin DEFAULT 'mysql_native_password',
  `authentication_string` text COLLATE utf8_bin,
  `password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  PRIMARY KEY (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges'
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

3.6 查看当前用户(select user();)

代码语言:javascript
复制
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> 

3.7 查看当前使用的数据库(select database();)

代码语言:javascript
复制
mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

mysql> 

3.8 创建库(create database db1;)

代码语言:javascript
复制
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

3.9 创建表(create table t1(`id` int(4), `name` char(40)); )

create table t1(`id` int(4), `name` char(40)); --t1为名字 id、name为字段 int为格式 char字符串最长40。

代码语言:javascript
复制
mysql> use db1;
Database changed
mysql> create table t1(`id` int(4), `name` char(40)); 
Query OK, 0 rows affected (0.01 sec)

mysql> show create table t1\G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

3.10 查看当前数据库版本(select version();)

代码语言:javascript
复制
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.43    |
+-----------+
1 row in set (0.00 sec)

mysql> 

3.11 查看数据库状态 (show status;)

代码语言:javascript
复制
mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name                                 | Value       |
+-----------------------------------------------+-------------+
| Aborted_clients                               | 0           |
| Aborted_connects                              | 0           |
| Binlog_cache_disk_use                         | 0           |
| Binlog_cache_use                              | 0           |
| Binlog_stmt_cache_disk_use                    | 0           |
| Binlog_stmt_cache_use                         | 0           |
| Bytes_received                                | 1170        |
| Bytes_sent                                    | 31193       |
| Com_admin_commands                            | 0           |
| Com_assign_to_keycache                        | 0           |
| Com_alter_db                                  | 0           |
| Com_alter_db_upgrade                          | 0           |
| Com_alter_event                               | 0           |
| Com_alter_function                            | 0           |
| Com_alter_procedure                           | 0           |
| Com_alter_server                              | 0           |
| Com_alter_table                               | 0           |
| Com_alter_tablespace                          | 0           |
| Com_alter_user                                | 0           |
| Com_analyze                                   | 0           |
| Com_begin                                     | 0           |
| Com_binlog                                    | 0           |
| Com_call_procedure                            | 0           |
| Com_change_db                                 | 2           |
| Com_change_master                             | 0           |
| Com_check                                     | 0           |
| Com_checksum                                  | 0           |
| Com_commit                                    | 0           |
| Com_create_db                                 | 1           |
| Com_create_event                              | 0           |
| Com_create_function                           | 0           |
| Com_create_index                              | 0           |
| Com_create_procedure                          | 0           |
| Com_create_server                             | 0           |
| Com_create_table                              | 1           |
| Com_create_trigger                            | 0           |
| Com_create_udf                                | 0           |
| Com_create_user                               | 0           |
| Com_create_view                               | 0           |
| Com_dealloc_sql                               | 0           |
| Com_delete                                    | 0           |
| Com_delete_multi                              | 0           |
| Com_do                                        | 0           |
| Com_drop_db                                   | 0           |
| Com_drop_event                                | 0           |
| Com_drop_function                             | 0           |
| Com_drop_index                                | 0           |
| Com_drop_procedure                            | 0           |
| Com_drop_server                               | 0           |
| Com_drop_table                                | 0           |
| Com_drop_trigger                              | 0           |
| Com_drop_user                                 | 0           |
| Com_drop_view                                 | 0           |
| Com_empty_query                               | 0           |
| Com_execute_sql                               | 0           |
| Com_flush                                     | 0           |
| Com_get_diagnostics                           | 0           |
| Com_grant                                     | 0           |
| Com_ha_close                                  | 0           |
| Com_ha_open                                   | 0           |
| Com_ha_read                                   | 0           |
| Com_help                                      | 0           |
| Com_insert                                    | 0           |
| Com_insert_select                             | 0           |
| Com_install_plugin                            | 0           |
| Com_kill                                      | 0           |
| Com_load                                      | 0           |
| Com_lock_tables                               | 0           |
| Com_optimize                                  | 0           |
| Com_preload_keys                              | 0           |
| Com_prepare_sql                               | 0           |
| Com_purge                                     | 0           |
| Com_purge_before_date                         | 0           |
| Com_release_savepoint                         | 0           |
| Com_rename_table                              | 0           |
| Com_rename_user                               | 0           |
| Com_repair                                    | 0           |
| Com_replace                                   | 0           |
| Com_replace_select                            | 0           |
| Com_reset                                     | 0           |
| Com_resignal                                  | 0           |
| Com_revoke                                    | 0           |
| Com_revoke_all                                | 0           |
| Com_rollback                                  | 0           |
| Com_rollback_to_savepoint                     | 0           |
| Com_savepoint                                 | 0           |
| Com_select                                    | 6           |
| Com_set_option                                | 0           |
| Com_signal                                    | 0           |
| Com_show_binlog_events                        | 0           |
| Com_show_binlogs                              | 0           |
| Com_show_charsets                             | 0           |
| Com_show_collations                           | 0           |
| Com_show_create_db                            | 0           |
| Com_show_create_event                         | 0           |
| Com_show_create_func                          | 0           |
| Com_show_create_proc                          | 0           |
| Com_show_create_table                         | 3           |
| Com_show_create_trigger                       | 0           |
| Com_show_databases                            | 4           |
| Com_show_engine_logs                          | 0           |
| Com_show_engine_mutex                         | 0           |
| Com_show_engine_status                        | 0           |
| Com_show_events                               | 0           |
| Com_show_errors                               | 0           |
| Com_show_fields                               | 29          |
| Com_show_function_code                        | 0           |
| Com_show_function_status                      | 0           |
| Com_show_grants                               | 0           |
| Com_show_keys                                 | 0           |
| Com_show_master_status                        | 0           |
| Com_show_open_tables                          | 0           |
| Com_show_plugins                              | 0           |
| Com_show_privileges                           | 0           |
| Com_show_procedure_code                       | 0           |
| Com_show_procedure_status                     | 0           |
| Com_show_processlist                          | 0           |
| Com_show_profile                              | 0           |
| Com_show_profiles                             | 0           |
| Com_show_relaylog_events                      | 0           |
| Com_show_slave_hosts                          | 0           |
| Com_show_slave_status                         | 0           |
| Com_show_status                               | 1           |
| Com_show_storage_engines                      | 0           |
| Com_show_table_status                         | 0           |
| Com_show_tables                               | 3           |
| Com_show_triggers                             | 0           |
| Com_show_variables                            | 0           |
| Com_show_warnings                             | 0           |
| Com_slave_start                               | 0           |
| Com_slave_stop                                | 0           |
| Com_stmt_close                                | 0           |
| Com_stmt_execute                              | 0           |
| Com_stmt_fetch                                | 0           |
| Com_stmt_prepare                              | 0           |
| Com_stmt_reprepare                            | 0           |
| Com_stmt_reset                                | 0           |
| Com_stmt_send_long_data                       | 0           |
| Com_truncate                                  | 0           |
| Com_uninstall_plugin                          | 0           |
| Com_unlock_tables                             | 0           |
| Com_update                                    | 0           |
| Com_update_multi                              | 0           |
| Com_xa_commit                                 | 0           |
| Com_xa_end                                    | 0           |
| Com_xa_prepare                                | 0           |
| Com_xa_recover                                | 0           |
| Com_xa_rollback                               | 0           |
| Com_xa_start                                  | 0           |
| Compression                                   | OFF         |
| Connection_errors_accept                      | 0           |
| Connection_errors_internal                    | 0           |
| Connection_errors_max_connections             | 0           |
| Connection_errors_peer_address                | 0           |
| Connection_errors_select                      | 0           |
| Connection_errors_tcpwrap                     | 0           |
| Connections                                   | 7           |
| Created_tmp_disk_tables                       | 1           |
| Created_tmp_files                             | 6           |
| Created_tmp_tables                            | 8           |
| Delayed_errors                                | 0           |
| Delayed_insert_threads                        | 0           |
| Delayed_writes                                | 0           |
| Flush_commands                                | 1           |
| Handler_commit                                | 0           |
| Handler_delete                                | 0           |
| Handler_discover                              | 0           |
| Handler_external_lock                         | 0           |
| Handler_mrr_init                              | 0           |
| Handler_prepare                               | 0           |
| Handler_read_first                            | 0           |
| Handler_read_key                              | 0           |
| Handler_read_last                             | 0           |
| Handler_read_next                             | 0           |
| Handler_read_prev                             | 0           |
| Handler_read_rnd                              | 0           |
| Handler_read_rnd_next                         | 125         |
| Handler_rollback                              | 0           |
| Handler_savepoint                             | 0           |
| Handler_savepoint_rollback                    | 0           |
| Handler_update                                | 0           |
| Handler_write                                 | 117         |
| Innodb_buffer_pool_dump_status                | not started |
| Innodb_buffer_pool_load_status                | not started |
| Innodb_buffer_pool_pages_data                 | 170         |
| Innodb_buffer_pool_bytes_data                 | 2785280     |
| Innodb_buffer_pool_pages_dirty                | 0           |
| Innodb_buffer_pool_bytes_dirty                | 0           |
| Innodb_buffer_pool_pages_flushed              | 17          |
| Innodb_buffer_pool_pages_free                 | 8021        |
| Innodb_buffer_pool_pages_misc                 | 0           |
| Innodb_buffer_pool_pages_total                | 8191        |
| Innodb_buffer_pool_read_ahead_rnd             | 0           |
| Innodb_buffer_pool_read_ahead                 | 0           |
| Innodb_buffer_pool_read_ahead_evicted         | 0           |
| Innodb_buffer_pool_read_requests              | 696         |
| Innodb_buffer_pool_reads                      | 167         |
| Innodb_buffer_pool_wait_free                  | 0           |
| Innodb_buffer_pool_write_requests             | 47          |
| Innodb_data_fsyncs                            | 17          |
| Innodb_data_pending_fsyncs                    | 0           |
| Innodb_data_pending_reads                     | 0           |
| Innodb_data_pending_writes                    | 0           |
| Innodb_data_read                              | 2805760     |
| Innodb_data_reads                             | 182         |
| Innodb_data_writes                            | 29          |
| Innodb_data_written                           | 564736      |
| Innodb_dblwr_pages_written                    | 17          |
| Innodb_dblwr_writes                           | 2           |
| Innodb_have_atomic_builtins                   | ON          |
| Innodb_log_waits                              | 0           |
| Innodb_log_write_requests                     | 9           |
| Innodb_log_writes                             | 4           |
| Innodb_os_log_fsyncs                          | 7           |
| Innodb_os_log_pending_fsyncs                  | 0           |
| Innodb_os_log_pending_writes                  | 0           |
| Innodb_os_log_written                         | 6144        |
| Innodb_page_size                              | 16384       |
| Innodb_pages_created                          | 4           |
| Innodb_pages_read                             | 166         |
| Innodb_pages_written                          | 17          |
| Innodb_row_lock_current_waits                 | 0           |
| Innodb_row_lock_time                          | 0           |
| Innodb_row_lock_time_avg                      | 0           |
| Innodb_row_lock_time_max                      | 0           |
| Innodb_row_lock_waits                         | 0           |
| Innodb_rows_deleted                           | 0           |
| Innodb_rows_inserted                          | 0           |
| Innodb_rows_read                              | 0           |
| Innodb_rows_updated                           | 0           |
| Innodb_num_open_files                         | 9           |
| Innodb_truncated_status_writes                | 0           |
| Innodb_available_undo_logs                    | 128         |
| Key_blocks_not_flushed                        | 0           |
| Key_blocks_unused                             | 6698        |
| Key_blocks_used                               | 0           |
| Key_read_requests                             | 0           |
| Key_reads                                     | 0           |
| Key_write_requests                            | 0           |
| Key_writes                                    | 0           |
| Last_query_cost                               | 0.000000    |
| Last_query_partial_plans                      | 0           |
| Max_used_connections                          | 1           |
| Not_flushed_delayed_rows                      | 0           |
| Open_files                                    | 46          |
| Open_streams                                  | 0           |
| Open_table_definitions                        | 81          |
| Open_tables                                   | 81          |
| Opened_files                                  | 163         |
| Opened_table_definitions                      | 15          |
| Opened_tables                                 | 21          |
| Performance_schema_accounts_lost              | 0           |
| Performance_schema_cond_classes_lost          | 0           |
| Performance_schema_cond_instances_lost        | 0           |
| Performance_schema_digest_lost                | 0           |
| Performance_schema_file_classes_lost          | 0           |
| Performance_schema_file_handles_lost          | 0           |
| Performance_schema_file_instances_lost        | 0           |
| Performance_schema_hosts_lost                 | 0           |
| Performance_schema_locker_lost                | 0           |
| Performance_schema_mutex_classes_lost         | 0           |
| Performance_schema_mutex_instances_lost       | 0           |
| Performance_schema_rwlock_classes_lost        | 0           |
| Performance_schema_rwlock_instances_lost      | 0           |
| Performance_schema_session_connect_attrs_lost | 0           |
| Performance_schema_socket_classes_lost        | 0           |
| Performance_schema_socket_instances_lost      | 0           |
| Performance_schema_stage_classes_lost         | 0           |
| Performance_schema_statement_classes_lost     | 0           |
| Performance_schema_table_handles_lost         | 0           |
| Performance_schema_table_instances_lost       | 0           |
| Performance_schema_thread_classes_lost        | 0           |
| Performance_schema_thread_instances_lost      | 0           |
| Performance_schema_users_lost                 | 0           |
| Prepared_stmt_count                           | 0           |
| Qcache_free_blocks                            | 1           |
| Qcache_free_memory                            | 1031352     |
| Qcache_hits                                   | 0           |
| Qcache_inserts                                | 0           |
| Qcache_lowmem_prunes                          | 0           |
| Qcache_not_cached                             | 11          |
| Qcache_queries_in_cache                       | 0           |
| Qcache_total_blocks                           | 1           |
| Queries                                       | 62          |
| Questions                                     | 50          |
| Select_full_join                              | 0           |
| Select_full_range_join                        | 0           |
| Select_range                                  | 0           |
| Select_range_check                            | 0           |
| Select_scan                                   | 8           |
| Slave_heartbeat_period                        |             |
| Slave_last_heartbeat                          |             |
| Slave_open_temp_tables                        | 0           |
| Slave_received_heartbeats                     |             |
| Slave_retried_transactions                    |             |
| Slave_running                                 | OFF         |
| Slow_launch_threads                           | 0           |
| Slow_queries                                  | 0           |
| Sort_merge_passes                             | 0           |
| Sort_range                                    | 0           |
| Sort_rows                                     | 0           |
| Sort_scan                                     | 0           |
| Ssl_accept_renegotiates                       | 0           |
| Ssl_accepts                                   | 0           |
| Ssl_callback_cache_hits                       | 0           |
| Ssl_cipher                                    |             |
| Ssl_cipher_list                               |             |
| Ssl_client_connects                           | 0           |
| Ssl_connect_renegotiates                      | 0           |
| Ssl_ctx_verify_depth                          | 0           |
| Ssl_ctx_verify_mode                           | 0           |
| Ssl_default_timeout                           | 0           |
| Ssl_finished_accepts                          | 0           |
| Ssl_finished_connects                         | 0           |
| Ssl_server_not_after                          |             |
| Ssl_server_not_before                         |             |
| Ssl_session_cache_hits                        | 0           |
| Ssl_session_cache_misses                      | 0           |
| Ssl_session_cache_mode                        | NONE        |
| Ssl_session_cache_overflows                   | 0           |
| Ssl_session_cache_size                        | 0           |
| Ssl_session_cache_timeouts                    | 0           |
| Ssl_sessions_reused                           | 0           |
| Ssl_used_session_cache_entries                | 0           |
| Ssl_verify_depth                              | 0           |
| Ssl_verify_mode                               | 0           |
| Ssl_version                                   |             |
| Table_locks_immediate                         | 70          |
| Table_locks_waited                            | 0           |
| Table_open_cache_hits                         | 11          |
| Table_open_cache_misses                       | 21          |
| Table_open_cache_overflows                    | 0           |
| Tc_log_max_pages_used                         | 0           |
| Tc_log_page_size                              | 0           |
| Tc_log_page_waits                             | 0           |
| Threads_cached                                | 0           |
| Threads_connected                             | 1           |
| Threads_created                               | 1           |
| Threads_running                               | 1           |
| Uptime                                        | 10901       |
| Uptime_since_flush_status                     | 10901       |
+-----------------------------------------------+-------------+
341 rows in set (0.01 sec)

mysql> 

3.11 查看表里的参数(show variables;)

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
mysql> show variables like "version";
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| version       | 5.6.43 |
+---------------+--------+
1 row in set (0.00 sec)

mysql> show variables like "version%";
+-------------------------+------------------------------+
| Variable_name           | Value                        |
+-------------------------+------------------------------+
| version                 | 5.6.43                       |
| version_comment         | MySQL Community Server (GPL) |
| version_compile_machine | x86_64                       |
| version_compile_os      | linux-glibc2.12              |
+-------------------------+------------------------------+
4 rows in set (0.00 sec)

mysql> 

3.12 修改参数(set global max_connect_errors=1000;)

global只是临时修改,要永久生效需要去配置文件添加。

代码语言:javascript
复制
mysql> show variables like "max_con%";
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

mysql> set global max_connect_errors=1000 ;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like "max_con%";
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 1000  |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

mysql> 

3.13 查看队列(show processlist;)

查看当前mysql服务器的队列: 查看服务器队列可以查看当前mysql在干什么,也可以发行是否有锁表! show processlist; show full processlist; 完整查看(可以看到info这里显示比较完整)

代码语言:javascript
复制
mysql> show processlist;  
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  6 | root | localhost | db1  | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

mysql> show full processlist; 
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host      | db   | Command | Time | State | Info                  |
+----+------+-----------+------+---------+------+-------+-----------------------+
|  6 | root | localhost | db1  | Query   |    0 | init  | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

mysql> 

4. MySQL用户管理

  • mysql> grant all privileges on . to jack@‘localhost’ identified by “jack” with grant option; Query OK, 0 rows affected (0.01 sec)
  • GRANT命令说明:
  • ALL PRIVILEGES 是表示所有权限,你也可以使用select、update等权限。
  • ON 用来指定权限针对哪些库和表。
  • . 中前面的号用来指定数据库名,后面的号用来指定表名。
  • TO 表示将权限赋予某个用户。
  • jack@‘localhost’ 表示jack用户,@后面接限制的主机,可以是IP、IP段、域名以及%,%表示任何地方。注意:这里%有的版本不包括本地,以前碰到过给某个用户设置了%允许任何地方登录,但是在本地登录不了,这个和版本有关系,遇到这个问题再加一个localhost的用户就可以了。
  • IDENTIFIED BY 指定用户的登录密码。
  • WITH GRANT OPTION 这个选项表示该用户可以将自己拥有的权限授权给别人。注意:经常有人在创建操作用户的时候不指定WITH GRANT OPTION选项导致后来该用户不能使用GRANT命令创建用户或者给其它用户授权。 备注:可以使用GRANT重复给用户添加权限,权限叠加,比如你先给用户添加一个select权限,然后又给用户添加一个insert权限,那么该用户就同时拥有了select和insert权限。

4.1 创建好用户提示无法登陆

代码语言:javascript
复制
[root@linux-02 ~]# mysql -uroot -plinux002  
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 12
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> grant all on *.* to user1@'%' identified by ’123456‘ ;
Query OK, 0 rows affected (0.00 sec)


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

mysql> exit
Bye

[root@linux-02 ~]# mysql -uuser1 -p123456 
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES)

4.2 创建的用户需要添加localhost的权限才可以登陆

  • 无法登录原因:
  • 其中两个账户有相同的用户名monty和密码some_pass。两个账户均为超级用户账户,具有完全的权限可以做任何事情。一个账户 (‘monty’@‘localhost’)只用于从本机连接时。另一个账户(‘monty’@’%’)可用于从其它主机连接。请注意monty的两个账户必须能从任何主机以monty连接。没有localhost账户,当monty从本机连接时,mysql_install_db创建的localhost的匿名用户账户将占先。结果是,monty将被视为匿名用户。原因是匿名用户账户的Host列值比’monty’@’%'账户更具体,这样在user表排序顺序中排在前面。
代码语言:javascript
复制
[root@linux-02 ~]# mysql -uroot -plinux002
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 15
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> grant all on *.* to user1@'localhost' identified  by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye


[root@linux-02 ~]# mysql -uuser1 -p123456
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 16
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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 ;
+---------------------------------------------------------------------------------------------------------------+
| Grants for user1@%                                                                                            |
+---------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+---------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

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

mysql> exit
Bye

5. 常用sql语句

5.1 统计一个表中有多少行

  • select count(*) from db1.t1;
  • 其中 db1表示库名,t1表示表名,中间用点隔开。此命令尽量少用,如果数据有几百万条,很容易浪费mysql的资源。
代码语言:javascript
复制
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use db1;
Database changed
mysql> show tables ;
+---------------+
| Tables_in_db1 |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)

mysql> select count(*) from db1.t1;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql> select * from db1.t1;
Empty set (0.00 sec)

mysql> 

5.2 显示一个表中所有的内容

  • select * from mysql.db;
  • 此命令也要少用,在数据量很大的时候,使用此命令占用mysql很大的资源。
代码语言:javascript
复制
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> 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)

mysql> select * from mysql.db;
+------+---------+------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| Host | Db      | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Create_tmp_table_priv | Lock_tables_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Execute_priv | Event_priv | Trigger_priv |
+------+---------+------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| %    | test    |      | Y           | Y           | Y           | Y           | Y           | Y         | N          | Y               | Y          | Y          | Y                     | Y                | Y                | Y              | Y                   | N                  | N            | Y          | Y            |
| %    | test\_% |      | Y           | Y           | Y           | Y           | Y           | Y         | N          | Y               | Y          | Y          | Y                     | Y                | Y                | Y              | Y                   | N                  | N            | Y          | Y            |
+------+---------+------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
2 rows in set (0.00 sec)

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: Y
Create_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: Y
Create_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 rows in set (0.00 sec)

ERROR: 
No query specified

mysql> 

5.3 查询一个段或者多个段的数据

  • select db,host from mysql.db;
  • 其中db段和host段之间需要使用逗号隔开。
代码语言:javascript
复制
mysql> select db from mysql.db;
+---------+
| db      |
+---------+
| test    |
| test\_% |
+---------+
2 rows in set (0.00 sec)

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

mysql> 

5.4 插入一行数据

  • insert into db1.t1 values (234,‘qwe’);
  • 插入数据的时候,是字符串的值,建议加上单引号。
代码语言:javascript
复制
mysql> use db1
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_db1 |
+---------------+
| t1            |
+---------------+
1 row in set (0.01 sec)

mysql> select * from db1.t1;
Empty set (0.00 sec)

mysql> insert into db1.t1 values (234,'qwe');
Query OK, 1 row affected (0.05 sec)

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|  234 | qwe  |
+------+------+
1 row in set (0.00 sec)

mysql> 

5.5 更改表中数据

  • update db1.t1 set name=‘linux’ where id=234;
代码语言:javascript
复制
mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|  234 | qwe  |
+------+------+
1 row in set (0.00 sec)

mysql> update db1.t1 set name='linux' where id=234;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from db1.t1;
+------+-------+
| id   | name  |
+------+-------+
|  234 | linux |
+------+-------+
1 row in set (0.00 sec)

mysql> 

5.6 清空表中的数据,保留表结构

  • truncate table db1.t1;
  • 此命令和drop不同的是,操作紧紧是把表中的数据给删除,表结构保留。
代码语言:javascript
复制
mysql> select * from db1.t1;
+------+-------+
| id   | name  |
+------+-------+
|  234 | linux |
+------+-------+
1 row in set (0.00 sec)

mysql> 
mysql> truncate table db1.t1;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from db1.t1;
Empty set (0.00 sec)

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> 

5.7 删除表

代码语言:javascript
复制
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)

mysql> drop table db1.t1;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
Empty set (0.00 sec)

mysql> 

5.8 删除库

代码语言:javascript
复制
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database db1;
Query OK, 0 rows affected (0.04 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

6. MySQL数据库备份恢复

6.1 备份库

  • mysqldump -uroot -plinux002 mysql > /data/mysql.bak/mysql.sql
代码语言:javascript
复制
[root@linux-02 ~]# mysqldump  -uroot -plinux002  mysql > /data/mysql.bak/mysql.sql
Warning: Using a password on the command line interface can be insecure.
[root@linux-02 ~]# du -sh /data/mysql.bak/mysql.sql
632K	/data/mysql.bak/mysql.sql
[root@linux-02 ~]# 

6.2 恢复库

  • mysql -uroot -plinux002 mysql < /data/mysql.bak/mysql.sql
  • 先把需要恢复的库创建好。
代码语言:javascript
复制
[root@linux-02 ~]# mysql  -uroot -plinux002  
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 7
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> create database mysql ;
Query OK, 1 row affected (0.00 sec)

mysql> exit
Bye
[root@linux-02 ~]# mysql  -uroot -plinux002  mysql < /data/mysql.bak/mysql.sql
Warning: Using a password on the command line interface can be insecure.
[root@linux-02 ~]# 

6.3 备份表

  • mysqldump -uroot -plinux002 mysql user > /data/mysql.bak/mysql_user.sql
  • mysql 是库名,user 是表名,中间使用空格隔开
代码语言:javascript
复制
[root@linux-02 ~]# mysqldump -uroot -plinux002 mysql user > /data/mysql.bak/mysql_user.sql
Warning: Using a password on the command line interface can be insecure.
[root@linux-02 ~]# du -sh  /data/mysql.bak/mysql_user.sql 
8.0K	/data/mysql.bak/mysql_user.sql
[root@linux-02 ~]# 

6.4 恢复表

  • mysql -uroot -plinux002 mysql < /data/mysql.bak/mysql_user.sql
  • 恢复表时不需要指定表名,只需要指定库名即可
代码语言:javascript
复制
[root@linux-02 ~]# mysql -uroot -plinux002 mysql < /data/mysql.bak/mysql_user.sql
Warning: Using a password on the command line interface can be insecure.

6.5 备份所有库

  • mysqldump -uroot -plinux002 -A >/data/mysql.bak/all_data.sql
  • -A 指定备份所有库
  • 在恢复库的时候同样需要先创建好库名称才可以恢复。
代码语言:javascript
复制
[root@linux-02 ~]# mysqldump -uroot -plinux002 -A > /data/mysql.bak/all_data.sql
Warning: Using a password on the command line interface can be insecure.
[root@linux-02 ~]# du -sh /data/mysql.bak/all_data.sql 
632K	/data/mysql.bak/all_data.sql
[root@linux-02 ~]# 

6.6 只备份表结构

  • mysqldump -uroot -plinux002 -d > /data/mysql.bak/all_data_list.sql
  • -d 指定备份表结构
代码语言:javascript
复制
[root@linux-02 ~]# mysqldump -uroot -plinux002 -d  > /data/mysql.bak/all_data_list.sql
Warning: Using a password on the command line interface can be insecure.
[root@linux-02 ~]# du -sh !$
du -sh /data/mysql.bak/all_data_list.sql
4.0K	/data/mysql.bak/all_data_list.sql
[root@linux-02 ~]# 

课后总结

mysql版本变化 https://www.cnblogs.com/ivictor/p/9807284.html mariadb的版本变化 https://downloads.mariadb.org/ 主从: https://blog.csdn.net/xiaoyi23000/article/details/80521423 https://blog.csdn.net/u013399093/article/details/70568837 https://www.cnblogs.com/abobo/p/4242417.html

mysql参数调整 http://isky000.com/database/mysql-perfornamce-tuning-cache-parameter

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • mysql管理
  • 预习笔记
    • 1. 设置更改root密码
      • 1.1 设置MySQL的root密码
      • 1.2 修改MySQL的root密码
    • 2. 连接MySQL方法
      • 2.1 mysql -uroot -plinux002
      • 2.2 mysql -uroot -plinux002 -h127.0.0.1 -P3306
      • 2.3 mysql -uroot -plinux002 -S/tmp/mysql.sock
      • 2.4 mysql -uroot -plinux002 -e “show databases”
    • 3. MySQL常用命令
      • 3.1 查询库 (show databases;)
      • 3.2 切换库(use mysql;)
      • 3.3 查看库里的表(show tables)
      • 3.4 查看表里的字段(desc user;)
      • 3.5 查看建表语句(show create table user\G;)
      • 3.6 查看当前用户(select user();)
      • 3.7 查看当前使用的数据库(select database();)
      • 3.8 创建库(create database db1;)
      • 3.9 创建表(create table t1(`id` int(4), `name` char(40)); )
      • 3.10 查看当前数据库版本(select version();)
      • 3.11 查看数据库状态 (show status;)
      • 3.11 查看表里的参数(show variables;)
      • 3.12 修改参数(set global max_connect_errors=1000;)
      • 3.13 查看队列(show processlist;)
    • 4. MySQL用户管理
      • 4.1 创建好用户提示无法登陆
      • 4.2 创建的用户需要添加localhost的权限才可以登陆
    • 5. 常用sql语句
      • 5.1 统计一个表中有多少行
      • 5.2 显示一个表中所有的内容
      • 5.3 查询一个段或者多个段的数据
      • 5.4 插入一行数据
      • 5.5 更改表中数据
      • 5.6 清空表中的数据,保留表结构
      • 5.7 删除表
      • 5.8 删除库
    • 6. MySQL数据库备份恢复
      • 6.1 备份库
      • 6.2 恢复库
      • 6.3 备份表
      • 6.4 恢复表
      • 6.5 备份所有库
      • 6.6 只备份表结构
  • 课后总结
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档