# mysql 常用操作

mysql 连接

使用 mysq l命令：`yum install mysql`

用法：

* -u ：指定用户
* -p ：指定密码
* -h ：指定主机
* -P ：指定端口
* -S ：指定连接的sock
* -e ：指定运行sql命令

```bash
mysql -uroot -p123456

mysql -uroot -p123456 -h127.0.0.1 -P3306

mysql -uroot -p123456 -S/tmp/mysql.sock

mysql -uroot -p123456 -e “show databases”
```

查看当前用户

```sql
> select user();
```

> select user()\G; \G 为按列显示

查看mysqlb版本

```sql
> select version();
```

> mysql结构 ： 库 --> 表 --> 行 --> 字段Aborted

查看当前使用的数据库

```sql
> select databsase();
```

查看数据库

```sql
> show databases;
```

查看数据库中的表

```bash
> use mysql;  # 切换库
> show tables; # 查看mysql库中的所有表
```

查看表结构

```sql
> desc tbname;
```

查看数据库和表的创建语句

```bash
> show create database dbname;
> show create table tbname;
```

查看数据库状态

```sql
> show status;
```

查看进程工作

```sql
> show processlist; 
> show full processlist;
```

查看各参数

```sql
> show variables; 
> show variables like 'max_connect%';
```

修改参数

```sql
> set global max_connect_errors=1000;
```

用户登陆授权

```sql
> grant dbcommand on dbname.tbname to 'user'@'host' identified by 'password';
> flush privileges;      #授权后刷新权限
```

查看用户权限

```sql
> show grants;
> show grants for user@192.168.127.1;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lack.gitbook.io/operation-and-maintenance/mysqlji-he/mysql-chang-yong-ming-ling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
