Mysql 常用操作
Mysql 常用操作
查询库 show databases;
切换库 use mysql;
查看库里的表 show tables;
查看表里的字段 desc tb_name; //tb_name表示字段名
查看建表语句 show create table tb_name\G; //\G表示由竖排显示(示的更加有条理)
查看当前用户 select user();
查看当前使用的数据库 select databsase();
创建库 create database db1;
创建表 use db1; create table t1(`id` int(4), `name` char(40));
查看当前数据库版本 select version();
查看数据库状态 show status;
查看各参数 show variables; show variables like 'max_connect%';
修改参数 set global max_connect_errors=1000;
查看队列 show processlist; show full processlist;
说明:以上的命令要 mysql 中执行。并且每行命令的结尾需要加;
### 具体操作如下
[root@dl-001 ~]# mysql -uroot -p'mysqldl991124' -h192.168.19.128 //使用内网登录mysql
mysql> select user(); //查看当前用户
+-------------+
| user() |
+-------------+
| root@dl-001 |
+-------------+
说明: 这里的用户是root@dl-001。 如果登录时不指定ip,用户则是本机localhost。
[root@dl-001 ~]# less .mysql_history //查看Linux中记录的mysql的命令历史
_HiStOrY_V2_
mysqladmin\040-uroot\040passwd\040'mysqldl991124'
quit
mysql\040-uroot\040-p
mysqldl991124
show\040databases;
mysqladmin\040-uroot\040passwd\040'mysqldl991124';
mysql\040-uroot\040-p123456\040-h127.0.0.1\040-P3306
;
mysql\040-uroot\040-p123456\040-S/tmp/mysql.sock;
mysql\040-uroot\040-p'123456'\040-e\040"show\040databases";
use\040mysql;
select\040*\040from\040user
;
mysql> create database db1; //创建库,名为db1;
Query OK, 1 row affected (0.01 sec)
mysql> show databases; //查看库;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use db1; //指定db1库
mysql> create table dl(`id` int(4) , `name` char(40)); //在指定的库里创建表,并在表中添加id和name字段
Query OK, 0 rows affected (0.09 sec)
mysql> desc dl; //查看表字段,方法1
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(4) | YES | | NULL | |
| name | char(40) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> desc dl\G; //查看表字段, 方法2 (竖排显示)
*************************** 1. row ***************************
Field: id
Type: int(4)
Null: YES
Key:
Default: NULL
Extra:
*************************** 2. row ***************************
Field: name
Type: char(40)
Null: YES
Key:
Default: NULL
Extra:
2 rows in set (0.01 sec)
ERROR:
No query specified
mysql> show tables; //查看当前库中的表
+---------------+
| Tables_in_db1 |
+---------------+
| dl |
+---------------+
1 row in set (0.00 sec)
mysql> drop create table dl; //删除dl表;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create table dl' at line 1
mysql> drop table dl;
Query OK, 0 rows affected (0.01 sec)
mysql> create table dl(`id` int(4) ,`name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=UTF8; //重新创建并指定引擎和字符集编码;
Query OK, 0 rows affected (0.10 sec)
mysql> show create table dl\G //查看表结构
*************************** 1. row ***************************
Table: dl
Create Table: CREATE TABLE `dl` (
`id` int(4) DEFAULT NULL,
`name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)