SQL(Structure Query Language)结构化查询语言
DML(Data Mailpulate Language)数据操作语言
1. INSERT
2. DELETE
3. SELECT
4. UPDATE
DDL(Data Define Language)数据定义语言
1. CREATE
2. DROP
3. ALTER
DCL(Data Control Language)数据控制语言
1. GRANT
2. REVOKE
库,表,索引,视图,用户,存储过程,存储函数,触发器,事件调度器
1. mysqladmin -uUSERNAME -h HOSTNAME password 'NEW_PASS' -p 旧密码
2. mysql>SET PASSWORD FOR 'USERNAME'@'HOST'=PASSWORD('new_pass')
3. mysql>UPDATE mysql.user SET PASSWORD=PASSWORD('new_pass') where condition;需要通过flush privileges重读授权表
- 【-h】|【--host】:主机
- 【-u】|【--user】:用户名
- 【-p】|【--password】:密码
- 【--port】| 【-P】 :指定端口
- 【--defaults-file】:指定配置文件
- 【--defaults-extra-file】:指定主配置文件外额外的配置文件
- 【--login-path】:指定登录时用户名和密码存放文件
- 【-S】|【--socket】:指定使用的socket文件
- 【--protocol=name】:使用指定协议连接 (tcp, socket, pipe,memory)
- 【-D】| 【--database=name】:指定连接后进入的数据库
- --no-auto-rehash:禁用自动补全功能
注:
1. 如果参数中未指定协议、socket、host或端口,将使用socket进行连接
2. 如果给定了端口,没有给定host,给定了协议,还是使用socket进行连接
3. 如果协议指定了TCP,尽管给定了socket选项,连接还是将通过TCP/IP进行连接
4. 如果协议指定了socket,也提供了端口,但没有给定host,将通过socket进行连接
5. 如果协议指定了socket,并且给定了主机为localhost,将通过socket进行连接
6. 如果协议给定了socket,给出了localhost以外的主机选项,将会报错(需要提供端口选项)
7. 如果host,port和socket都提供了,但协议没有指定,将使用host和port进行连接
#查看mysql版本
[root@lotus ~]# mysqladmin --version
mysqladmin Ver 8.42 Distrib 5.7.37, for Linux on x86_64
[root@lotus ~]# mysqladmin version
mysqladmin Ver 8.42 Distrib 5.7.37, for Linux on x86_64
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Server version 5.7.37
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /tmp/mysql.sock
Uptime: 20 min 15 sec
Threads: 2 Questions: 37 Slow queries: 0 Opens: 117 Flush tables: 1 Open tables: 35 Queries per second avg: 0.030
#创建数据库
[root@lotus ~]# mysqladmin create testdb
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
6 rows in set (0.01 sec)
#删除数据库
[root@lotus ~]# mysqladmin drop testdb
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'testdb' database [y/N] y
Database "testdb" dropped
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
#测试服务器是否在线
[root@lotus ~]# mysqladmin ping -h 192.168.88.105
mysqld is alive
#查看进行列表
[root@lotus ~]# mysqladmin processlist
+----+------+-----------+----+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+----------+------------------+
| 6 | root | localhost | | Sleep | 73 | | |
| 9 | root | localhost | | Query | 0 | starting | show processlist |
+----+------+-----------+----+---------+------+----------+------------------+
#显示mysql服务器的状态
[root@lotus ~]# mysqladmin status
Uptime: 645 Threads: 2 Questions: 16 Slow queries: 0 Opens: 113 Flush tables: 1 Open tables: 31 Queries per second avg: 0.024
#每隔两秒显示一次
[root@lotus ~]# mysqladmin status --sleep 2
#一共显示多少次
[root@lotus ~]# mysqladmin status --sleep 2 --count 2
#显示服务器状态变量
[root@lotus ~]# mysqladmin extended-status
#显示服务器变量
[root@lotus ~]# mysqladmin variables
#服务器重读授权表也可使用mysqladmin reload
[root@lotus ~]# mysqladmin flush-privileges
#关闭所有打开的表flush-table
#重置线程池flush-threads
#重置大部分状态变量flush-status
#日志滚动(二进制和中继日志)flush-logs
#清除主机内部信息(DNS缓存,内部错误导致用户拒绝登录)flush-hosts
#refresh相当于flush-hosts和flush-logs
#关闭mysql服务器mysqladmin shutdown
#启动从服务器的复制线程start-slave--有两个线程SQL Thread,IO Thread
#停止从服务器的复制线程stop-slave
mysql> drop user ''@'localhost';
所有表共享一个表空间文件
建立每个表独立的表空间文件【innodb_file_per_table=1】
.frm 表结构
.ibd:表空间(表数据和表索引)
交互式模式
mysql>source /root/my.sql
批处理模式(脚本模式)
mysql < init.sql 使用输入重定向
mysql>
命令:
服务器语句,有语句结束符,默认结束符为【;】
\d(delimiter)设置服务器语句结束符
客户端命令:
\c:清除当前输入
\g:无论语句结束符是什么,直接将此语句送至服务器执行
\G:无论语句结束符是什么,直接将此语句送至服务器执行,结果以纵向显示(竖排方式显示)
【! COMMAND】 | 【system COMMAND】 执行shell命令
\W:语句执行结束后显示警告信息;
#:对新建的对象,支持补全功能
help COMMAND
#以html格式显示信息
[root@mail ~]# mysql -uroot -p --html
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.10 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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 extmail filemanager mydb mysql performance_schema test vsftpd
8 rows in set (0.00 sec)
#设定默认的库
mysql> use mydb;
Database changed
#查看当前默认的库
mysql> select database();
+------------+
| database() |
+------------+
| mydb |
+------------+
1 row in set (0.00 sec)
#mysqladmin创建数据库
[root@mail mydb]# mysqladmin -uroot -p create hellodb
Enter password:
[root@mail mydb]#
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| extmail |
| filemanager |
| hellodb |
| mydb |
| mysql |
| performance_schema |
| test |
| vsftpd |
+--------------------+
9 rows in set (0.00 sec)
#删除数据库
[root@mail mydb]# mysqladmin -uroot -p drop hellodb
Enter password:
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'hellodb' database [y/N] y
Database "hellodb" dropped
#ping一下数据库是否可用
[root@mail mydb]# mysqladmin -uroot -p ping
Enter password:
mysqld is alive
#进程列表
[root@mail mydb]# mysqladmin -uroot -p processlist
Enter password:
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 5 | root | localhost | | Sleep | 115 | | |
| 8 | root | localhost | | Query | 0 | init | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+
#status:显示mysql状态
[root@mail mydb]# mysqladmin -uroot -p status
Enter password:
Uptime: 4994 Threads: 2 Questions: 81 Slow queries: 0 Opens: 90 Flush tables: 1 Open tables: 81 Queries per second avg: 0.016
#--sleep N 等待N秒查询一次
[root@mail mydb]# mysqladmin -uroot -p status --sleep 2
Enter password:
Uptime: 5136 Threads: 2 Questions: 83 Slow queries: 0 Opens: 90 Flush tables: 1 Open tables: 81 Queries per second avg: 0.016
Uptime: 5138 Threads: 2 Questions: 84 Slow queries: 0 Opens: 90 Flush tables: 1 Open tables: 81 Queries per second avg: 0.016
Uptime: 5140 Threads: 2 Questions: 85 Slow queries: 0 Opens: 90 Flush tables: 1 Open tables: 81 Queries per second avg: 0.016
#--countN 显示N次
[root@mail mydb]# mysqladmin -uroot -p status --sleep 2 --count 2
Enter password:
Uptime: 5182 Threads: 2 Questions: 87 Slow queries: 0 Opens: 90 Flush tables: 1 Open tables: 81 Queries per second avg: 0.016
Uptime: 5184 Threads: 2 Questions: 88 Slow queries: 0 Opens: 90 Flush tables: 1 Open tables: 81 Queries per second avg: 0.016
#extended-status显示状态变量
[root@mail mydb]# mysqladmin -uroot -p extended-status;
#variables:显示服务器变量
[root@mail mydb]# mysqladmin -uroot -p variables;
#flush-privileges[reload]重读授权表
#flush-tables关闭所有打开表
#flush-threads重置所有线程
#flush-status重置大多数的服务器状态变量
#flush-logs做二进制日志和中继日志滚动
#flush-hosts清除主机的内部信息(缓存信息,由于过多连接的错误信息)
#kill
#refresh:相当于同时执行flush-hosts和flush-logs
#shutdown:停止mysql服务器进程
#version:服务器版本号及相关状态信息
#start-slave:启动复制,启动从服务器复制线程
SQL thread
IO thread
#stop-slave:关闭复制,
#显示引擎信息
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
#show table status 显示表的状态信息
mysql> show table status like 'test1'\G;
*************************** 1. row ***************************
Name: test1
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2021-06-20 19:55:29
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
在mysql的data目录中有错误日志文件:hostname.err
一般MySQL报错可以从以下几种情况进行分析
1.此前服务未关闭
2.数据初使化失败
3.数据目录位置错误
4.数据目录权限问题