---
title: "Windows7下MySQL5.5.20免安装版的配置"
author: "Speeddsy"
date: "2012-06-15T20:38:00Z"
tags: []
categories: ["数据库"]
slug: "windows7-mysql5-5-20"
draft: false
lastmod: "2012-11-09T10:18:03Z"
id: 1721447
url: "https://speeddsy.iteye.com/blog/1721447"
---

**MySQL Windows安装包说明：**  
**1、mysql-5.5.20-win32.msi:Windows 安装包，图形化的下一步下一步的安装。**  
**2、mysql-5.5.20.zip，这个是windows源文件，需要编译，对应的Linux源文件是mysql-5.5.20.tar.gz**  
**3、mysql-5.5.20-win32.zip，这个文件解包后即可使用，是编译好的windows32位Mysql。**

1、下载mysql-5.5.20-win32.zip，解压到D:\\，D盘就会出现mysql-5.5.20-win32目录

2、配置MYSQL的环境变量  
新增系统变量MYSQL\_HOME： D:\\mysql-5.5.20-win32  
在PATH变量的最后面添加： ;%MYSQL\_HOME%\\bin  
保存即可。

3、打开文件my-huge.ini另存为my.ini，删除my.ini中的所有配置，在my.ini文件中加入如下简单配置：(my.ini是保存在与my-huge.ini同一个目录下的）(#表示注释）

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. #ThefollowingoptionswillbepassedtoallMySQLclients
02. \[client]
03. #password=your\_password
04. port=3306
05. #设置mysql客户端的字符集
06. default-character-set=utf8
07. #TheMySQLserver
08. \[mysqld]
09. port=3306
10. #设置mysql的安装目录
11. basedir=D:\\mysql-5.5.20-win32
12. #设置mysql数据库的数据存放目录,必须是data或者\\xxx-data
13. datadir=D:\\mysql-5.5.20-win32\\data
14. #设置服务器段的字符集
15. character\_set\_server=utf8

```
# The following options will be passed to all MySQL clients
[client]
#password	= your_password
port		= 3306
#设置mysql客户端的字符集
default-character-set = utf8

# The MySQL server
[mysqld]
port		= 3306
#设置mysql的安装目录
basedir = D:\mysql-5.5.20-win32
#设置mysql数据库的数据存放目录,必须是data或者\xxx-data
datadir = D:\mysql-5.5.20-win32\data
#设置服务器段的字符集
character_set_server = utf8
```

4、注册服务  
运行--&gt;cmd，输入命令：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysqld--installmysql--defaults-file=d:\\mysql-5.5.20-win32\\my.ini

```
mysqld --install mysql --defaults-file=d:\mysql-5.5.20-win32\my.ini
```

或着，  
运行--&gt;cmd，输入命令：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysqld--installmysql

```
mysqld --install mysql
```

删除服务：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. scdeletemysql

```
sc delete mysql
```

在“服务”中就会出现mysql这一项。

5、启动服务：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. netstartmysql

```
net start mysql
```

停止服务：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. netstopmysql

```
net stop mysql
```

6、服务启动后：  
登录MySQL服务器：  
命令格式：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql-hhostname-uusername-p

```
mysql -h hostname -u username -p
```

或

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql-hhostname-uusername-p

```
mysql -hhostname -uusername -p
```

命令说明：mysql命令将调用MySQL监视程序，这是一个可以将我们连接到MySQL服务器端的客户端命令行工具。  
选项说明：  
\-h选项：用于指定所希望连接的主机，即运行MySQL服务器的机器。如果在运行MySQL服务器的机器上运行该命令，则可以忽略该选项和hostname参数；如果不是，必须用运行MySQL服务器的主机名称来代替主机名称参数。  
\-u命令：用于指定连接数据库时使用的用户名称。  
\-p命令：用于指定用户输入的密码

此时我本机安装了MYSQL，可忽略该选项和hostname参数：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql-uroot-p

```
mysql -uroot -p
```

注：  
MySQL的管理员用户名为root，密码默认为空

修改root密码  
MySQL配置好后，启动成功，默认密码是空，但是为了安全，设置密码（MySQL有一个默认用户名为root，密码自己设定：假如设为root）。  
1）登录MySQL root用户：  
打开命令行，执行：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql-uroot-p

```
mysql -uroot -p
```

2）修改root密码：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;updatemysql.usersetpassword="root"whereUser="root";
2. mysql&gt;flushprivileges;

```
   mysql> update mysql.user set password="root" where User="root";
   mysql> flush privileges;
   
```

**修改该修改密码的语句：update mysql.user set password="root" where User="root";**  
**为： update mysql.user set password=password("root") where User="root";**

详细说明：见最底下的补充说明。

以后再进入MySQL，则为：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql-uroot-proot

```
mysql -uroot -proot
```

7、常用命令：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. showdatabases;--显示数据库
02. usedatabasename;--用数据库
03. showtables;--显示表
04. createtabletablename(field-name-1fieldtype-1modifiers,field-name-2fieldtype-2modifiers,....);--创建表
05. altertabletablenameaddnew-fielnamenewfieldtype--为表加入新列
06. insertintotablename(fieldname-1,fieldname-2,fieldname-n)valuse(value-1,value-2,value-n)--增
07. deletefromtablenamewherefieldname=value--删
08. updatetablenamesetfieldname=new-valuewhereid=1--改
09. select\*fromtablename--查
10. desctablename--表定义描述
11. showcreatetabletablename--可以查看引擎
12. altertabletablenameengine=InnoDB--修改引擎
13. createtabletablename(idint(11),namevarchar(10))type=INNODB--建表是设置引擎

```
show databases;--显示数据库
use databasename; --用数据库
show tables;--显示表
create table tablename(field-name-1 fieldtype-1 modifiers,field-name-2 fieldtype-2 modifiers,....);--创建表
alter table tablename add new-fielname new fieldtype--为表加入新列
insert into tablename(fieldname-1,fieldname-2,fieldname-n)valuse(value-1,value-2,value-n)--增
delete from tablename where fieldname=value--删
update tablename set fieldname=new-value where id=1--改
select * from tablename--查
desc tablename--表定义描述
show create table tablename--可以查看引擎
alter table tablename engine=InnoDB--修改引擎
create table tablename(id int(11),name varchar(10) )type=INNODB--建表是设置引擎
```

8、例如：  
（1）登录MySQL服务器后，查看当前时间，登录的用户以及数据库的版本

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;selectnow(),user(),version();
2. +---------------------+----------------+-----------+
3. |now()|user()|version()|
4. +---------------------+----------------+-----------+
5. |2012-02-2620:29:51|root@localhost|5.5.20|
6. +---------------------+----------------+-----------+
7. 1rowinset(0.00sec)

```
mysql> select now(),user(),version();
+---------------------+----------------+-----------+
| now()               | user()         | version() |
+---------------------+----------------+-----------+
| 2012-02-26 20:29:51 | root@localhost | 5.5.20    |
+---------------------+----------------+-----------+
1 row in set (0.00 sec)
```

（2）显示数据库列表

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;showdatabases;
02. +--------------------+
03. |Database|
04. +--------------------+
05. |information\_schema|
06. |mysql|
07. |performance\_schema|
08. |test|
09. +--------------------+
10. 4rowsinset(0.03sec)

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

（3）新增数据库并查看

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;createdatabasetest\_db;
02. QueryOK,1rowaffected(0.00sec)
03. mysql&gt;showdatabases;
04. +--------------------+
05. |Database|
06. +--------------------+
07. |information\_schema|
08. |mysql|
09. |performance\_schema|
10. |test|
11. |test\_db|
12. +--------------------+
13. 5rowsinset(0.00sec)

```
mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)

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

（4）选择数据库

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;usetest\_db;
2. Databasechanged

```
mysql> use test_db;
Database changed
```

查看已选择的数据库：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;selectdatabase();
2. +------------+
3. |database()|
4. +------------+
5. |test\_db|
6. +------------+
7. 1rowinset(0.00sec)

```
mysql> select database();
+------------+
| database() |
+------------+
| test_db    |
+------------+
1 row in set (0.00 sec)
```

（5）显示当前数据库的所有数据表

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;showtables;
2. Emptyset(0.00sec)

```
mysql> show tables;
Empty set (0.00 sec)
```

（6）新建数据表并查看

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;createtableperson(
2. -&gt;idint,
3. -&gt;namevarchar(20),
4. -&gt;sexchar(1),
5. -&gt;birthdate
6. -&gt;);
7. QueryOK,0rowsaffected(0.09sec)

```
mysql> create table person(
    -> id int,
    -> name varchar(20),
    -> sex char(1),
    -> birth date
    -> );
Query OK, 0 rows affected (0.09 sec)
```

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;showtables;
2. +-------------------+
3. |Tables\_in\_test\_db|
4. +-------------------+
5. |person|
6. +-------------------+
7. 1rowinset(0.00sec)

```
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| person            |
+-------------------+
1 row in set (0.00 sec)
```

（7）获取表结构

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;descperson;
02. +-------+-------------+------+-----+---------+-------+
03. |Field|Type|Null|Key|Default|Extra|
04. +-------+-------------+------+-----+---------+-------+
05. |id|int(11)|YES||NULL||
06. |name|varchar(20)|YES||NULL||
07. |sex|char(1)|YES||NULL||
08. |birth|date|YES||NULL||
09. +-------+-------------+------+-----+---------+-------+
10. 4rowsinset(0.01sec)

```
mysql> desc person;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| sex   | char(1)     | YES  |     | NULL    |       |
| birth | date        | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
```

或者

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;describeperson;
02. +-------+-------------+------+-----+---------+-------+
03. |Field|Type|Null|Key|Default|Extra|
04. +-------+-------------+------+-----+---------+-------+
05. |id|int(11)|YES||NULL||
06. |name|varchar(20)|YES||NULL||
07. |sex|char(1)|YES||NULL||
08. |birth|date|YES||NULL||
09. +-------+-------------+------+-----+---------+-------+
10. 4rowsinset(0.01sec)

```
mysql> describe person;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| sex   | char(1)     | YES  |     | NULL    |       |
| birth | date        | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
```

（8）查询表中的数据

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;select\*fromperson;
2. Emptyset(0.00sec)

```
mysql> select * from person;
Empty set (0.00 sec)
```

（9）插入数据

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;insertintoperson(id,name,sex,birth)
2. -&gt;values(1,'zhangsan','1','1990-01-08');
3. QueryOK,1rowaffected(0.04sec)

```
mysql> insert into person(id,name,sex,birth)
    -> values(1,'zhangsan','1','1990-01-08');
Query OK, 1 row affected (0.04 sec)
```

查询表中的数据：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;select\*fromperson;
2. +------+----------+------+------------+
3. |id|name|sex|birth|
4. +------+----------+------+------------+
5. |1|zhangsan|1|1990-01-08|
6. +------+----------+------+------------+
7. 1rowinset(0.00sec)

```
mysql> select * from person;
+------+----------+------+------------+
| id   | name     | sex  | birth      |
+------+----------+------+------------+
|    1 | zhangsan | 1    | 1990-01-08 |
+------+----------+------+------------+
1 row in set (0.00 sec)
```

（10）修改字段的类型

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;altertablepersonmodifysexchar(8);
2. QueryOK,1rowaffected(0.17sec)
3. Records:1Duplicates:0Warnings:0

```
mysql> alter table person modify sex char(8);
Query OK, 1 row affected (0.17 sec)
Records: 1  Duplicates: 0  Warnings: 0
```

查看字段描述：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;descperson;
02. +-------+-------------+------+-----+---------+-------+
03. |Field|Type|Null|Key|Default|Extra|
04. +-------+-------------+------+-----+---------+-------+
05. |id|int(11)|YES||NULL||
06. |name|varchar(20)|YES||NULL||
07. |sex|char(8)|YES||NULL||
08. |birth|date|YES||NULL||
09. +-------+-------------+------+-----+---------+-------+
10. 4rowsinset(0.01sec)

```
mysql> desc person;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| sex   | char(8)     | YES  |     | NULL    |       |
| birth | date        | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
```

（11）新增一个字段

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;altertablepersonadd(addressvarchar(50));
2. QueryOK,1rowaffected(0.27sec)
3. Records:1Duplicates:0Warnings:0

```
mysql> alter table person add(address varchar(50));
Query OK, 1 row affected (0.27 sec)
Records: 1  Duplicates: 0  Warnings: 0
```

查看字段描述：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;descperson;
02. +---------+-------------+------+-----+---------+-------+
03. |Field|Type|Null|Key|Default|Extra|
04. +---------+-------------+------+-----+---------+-------+
05. |id|int(11)|YES||NULL||
06. |name|varchar(20)|YES||NULL||
07. |sex|char(8)|YES||NULL||
08. |birth|date|YES||NULL||
09. |address|varchar(50)|YES||NULL||
10. +---------+-------------+------+-----+---------+-------+
11. 5rowsinset(0.01sec)

```
mysql> desc person;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| sex     | char(8)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
```

（12）更新字段内容  
查看修改前表的内容：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;select\*fromperson;
2. +------+----------+------+------------+---------+
3. |id|name|sex|birth|address|
4. +------+----------+------+------------+---------+
5. |1|zhangsan|1|1990-01-08|NULL|
6. +------+----------+------+------------+---------+
7. 1rowinset(0.00sec)

```
mysql> select * from person;
+------+----------+------+------------+---------+
| id   | name     | sex  | birth      | address |
+------+----------+------+------------+---------+
|    1 | zhangsan | 1    | 1990-01-08 | NULL    |
+------+----------+------+------------+---------+
1 row in set (0.00 sec)
```

修改：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;updatepersonsetname='lisi'whereid=1;
02. QueryOK,1rowaffected(0.04sec)
03. Rowsmatched:1Changed:1Warnings:0
04. mysql&gt;select\*fromperson;
05. +------+------+------+------------+---------+
06. |id|name|sex|birth|address|
07. +------+------+------+------------+---------+
08. |1|lisi|1|1990-01-08|NULL|
09. +------+------+------+------------+---------+
10. 1rowinset(0.00sec)
11. mysql&gt;updatepersonsetsex='man',address='China'whereid=1;
12. QueryOK,1rowaffected(0.04sec)
13. Rowsmatched:1Changed:1Warnings:0
14. mysql&gt;select\*fromperson;
15. +------+------+------+------------+---------+
16. |id|name|sex|birth|address|
17. +------+------+------+------------+---------+
18. |1|lisi|man|1990-01-08|China|
19. +------+------+------+------------+---------+
20. 1rowinset(0.00sec)

```
mysql> update person set name='lisi' where id=1;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from person;
+------+------+------+------------+---------+
| id   | name | sex  | birth      | address |
+------+------+------+------------+---------+
|    1 | lisi | 1    | 1990-01-08 | NULL    |
+------+------+------+------------+---------+
1 row in set (0.00 sec)

mysql> update person set sex='man',address='China' where id=1;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from person;
+------+------+------+------------+---------+
| id   | name | sex  | birth      | address |
+------+------+------+------------+---------+
|    1 | lisi | man  | 1990-01-08 | China   |
+------+------+------+------------+---------+
1 row in set (0.00 sec)
```

为了方便下面测试删除数据，在向person表中插入2条数据：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;insertintoperson(id,name,sex,birth,address)
02. -&gt;values(2,'wangwu','man','1990-01-10','China');
03. QueryOK,1rowaffected(0.02sec)
04. mysql&gt;insertintoperson(id,name,sex,birth,address)
05. -&gt;values(3,'zhangsan','man','1990-01-10','China');
06. QueryOK,1rowaffected(0.04sec)
07. mysql&gt;select\*fromperson;
08. +------+----------+------+------------+---------+
09. |id|name|sex|birth|address|
10. +------+----------+------+------------+---------+
11. |1|lisi|man|1990-01-08|China|
12. |2|wangwu|man|1990-01-10|China|
13. |3|zhangsan|man|1990-01-10|China|
14. +------+----------+------+------------+---------+
15. 3rowsinset(0.00sec)

```
mysql> insert into person(id,name,sex,birth,address)
    -> values(2,'wangwu','man','1990-01-10','China');
Query OK, 1 row affected (0.02 sec)

mysql> insert into person(id,name,sex,birth,address)
    -> values(3,'zhangsan','man','1990-01-10','China');
Query OK, 1 row affected (0.04 sec)

mysql> select * from person;
+------+----------+------+------------+---------+
| id   | name     | sex  | birth      | address |
+------+----------+------+------------+---------+
|    1 | lisi     | man  | 1990-01-08 | China   |
|    2 | wangwu   | man  | 1990-01-10 | China   |
|    3 | zhangsan | man  | 1990-01-10 | China   |
+------+----------+------+------------+---------+
3 rows in set (0.00 sec)
```

（13）删除表中的数据  
删除表中指定的数据：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;deletefrompersonwhereid=2;
02. QueryOK,1rowaffected(0.02sec)
03. mysql&gt;select\*fromperson;
04. +------+----------+------+------------+---------+
05. |id|name|sex|birth|address|
06. +------+----------+------+------------+---------+
07. |1|lisi|man|1990-01-08|China|
08. |3|zhangsan|man|1990-01-10|China|
09. +------+----------+------+------------+---------+
10. 2rowsinset(0.00sec)

```
mysql> delete from person where id=2;
Query OK, 1 row affected (0.02 sec)

mysql> select * from person;
+------+----------+------+------------+---------+
| id   | name     | sex  | birth      | address |
+------+----------+------+------------+---------+
|    1 | lisi     | man  | 1990-01-08 | China   |
|    3 | zhangsan | man  | 1990-01-10 | China   |
+------+----------+------+------------+---------+
2 rows in set (0.00 sec)
```

删除表中全部的数据：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;deletefromperson;
2. QueryOK,2rowsaffected(0.04sec)
3. mysql&gt;select\*fromperson;
4. Emptyset(0.00sec)

```
mysql> delete from person;
Query OK, 2 rows affected (0.04 sec)

mysql> select * from person;
Empty set (0.00 sec)
```

（14）重命名表  
查看重命名前的表名：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;showtables;
2. +-------------------+
3. |Tables\_in\_test\_db|
4. +-------------------+
5. |person|
6. +-------------------+
7. 1rowinset(0.00sec)

```
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| person            |
+-------------------+
1 row in set (0.00 sec)
```

重命名：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;altertablepersonrenameperson\_test;
2. QueryOK,0rowsaffected(0.04sec)
3. mysql&gt;showtables;
4. +-------------------+
5. |Tables\_in\_test\_db|
6. +-------------------+
7. |person\_test|
8. +-------------------+
9. 1rowinset(0.00sec)

```
mysql> alter table person rename person_test;
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| person_test       |
+-------------------+
1 row in set (0.00 sec)
```

（15）新增主键

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;altertableperson\_testaddprimarykey(id);
02. QueryOK,0rowsaffected(0.11sec)
03. Records:0Duplicates:0Warnings:0
04. mysql&gt;descperson\_test;
05. +---------+-------------+------+-----+---------+-------+
06. |Field|Type|Null|Key|Default|Extra|
07. +---------+-------------+------+-----+---------+-------+
08. |id|int(11)|NO|PRI|0||
09. |name|varchar(20)|YES||NULL||
10. |sex|char(8)|YES||NULL||
11. |birth|date|YES||NULL||
12. |address|varchar(50)|YES||NULL||
13. +---------+-------------+------+-----+---------+-------+
14. 5rowsinset(0.00sec)

```
mysql> alter table person_test add primary key(id);
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc person_test;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   | PRI | 0       |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| sex     | char(8)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
```

删除主键：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;altertableperson\_testdropprimarykey;
02. QueryOK,0rowsaffected(0.18sec)
03. Records:0Duplicates:0Warnings:0
04. mysql&gt;descperson\_test;
05. +---------+-------------+------+-----+---------+-------+
06. |Field|Type|Null|Key|Default|Extra|
07. +---------+-------------+------+-----+---------+-------+
08. |id|int(11)|NO||0||
09. |name|varchar(20)|YES||NULL||
10. |sex|char(8)|YES||NULL||
11. |birth|date|YES||NULL||
12. |address|varchar(50)|YES||NULL||
13. +---------+-------------+------+-----+---------+-------+
14. 5rowsinset(0.01sec)

```
mysql> alter table person_test drop primary key;
Query OK, 0 rows affected (0.18 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc person_test;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   |     | 0       |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| sex     | char(8)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
```

（16）删除表

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;droptableperson\_test;
2. QueryOK,0rowsaffected(0.04sec)
3. mysql&gt;showtables;
4. Emptyset(0.00sec)

```
mysql> drop table person_test;
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
Empty set (0.00 sec)
```

（17）删除数据库

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. mysql&gt;showdatabases;
02. +--------------------+
03. |Database|
04. +--------------------+
05. |information\_schema|
06. |mysql|
07. |performance\_schema|
08. |test|
09. |test\_db|
10. +--------------------+
11. 5rowsinset(0.00sec)
12. mysql&gt;dropdatabasetest\_db;
13. QueryOK,0rowsaffected(0.11sec)
14. mysql&gt;showdatabases;
15. +--------------------+
16. |Database|
17. +--------------------+
18. |information\_schema|
19. |mysql|
20. |performance\_schema|
21. |test|
22. +--------------------+
23. 4rowsinset(0.00sec)

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

mysql> drop database test_db;
Query OK, 0 rows affected (0.11 sec)

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

（18）查看建表语句

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;showcreatetabletable\_name;

```
mysql> show create table table_name;
```

**补充说明：**  
update mysql.user set password="root" where User="root";修改的不是密码，如果按照这个方式修改了，重新登录时将会报错：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

1. mysql&gt;updatemysql.usersetpassword="root"whereUser="root";
2. QueryOK,3rowsaffected(0.00sec)
3. Rowsmatched:3Changed:3Warnings:0
4. mysql&gt;exit
5. Bye
6. C:\\Users\\liqiong&gt;mysql-uroot-p
7. Enterpassword:\*\*\**
8. ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:Y
9. ES)

```
mysql> update mysql.user set password="root" where User="root";
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> exit
Bye

C:\Users\liqiong>mysql -uroot -p
Enter password: ****
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: Y
ES)
```

请按照以下方式重新修改密码，即可登录成功：

Mysql代码 [![复制代码](http://supportopensource.iteye.com/images/icon_copy.gif)](http://supportopensource.iteye.com/blog/1415527 "复制代码")[![收藏代码](http://supportopensource.iteye.com/images/icon_star.png)![](http://supportopensource.iteye.com/images/spinner.gif)]()

01. C:\\Users\\liqiong&gt;mysql-uroot
02. WelcometotheMySQLmonitor.Commandsendwith;or\\g.
03. YourMySQLconnectionidis4
04. Serverversion:5.5.20MySQLCommunityServer(GPL)
05. Copyright(c)2000,2011,Oracleand/oritsaffiliates.Allrightsreserved.
06. OracleisaregisteredtrademarkofOracleCorporationand/orits
07. affiliates.Othernamesmaybetrademarksoftheirrespective
08. owners.
09. Type'help;'or'\\h'forhelp.Type'\\c'toclearthecurrentinputstatement.
10. mysql&gt;updatemysql.usersetpassword=password("root")whereUser="root";
11. QueryOK,3rowsaffected(0.00sec)
12. Rowsmatched:3Changed:3Warnings:0
13. mysql&gt;flushprivileges;
14. QueryOK,0rowsaffected(0.00sec)
15. mysql&gt;exit
16. Bye
17. C:\\Users\\liqiong&gt;mysql-uroot-p
18. Enterpassword:\*\*\**
19. WelcometotheMySQLmonitor.Commandsendwith;or\\g.
20. YourMySQLconnectionidis5
21. Serverversion:5.5.20MySQLCommunityServer(GPL)
22. Copyright(c)2000,2011,Oracleand/oritsaffiliates.Allrightsreserved.
23. OracleisaregisteredtrademarkofOracleCorporationand/orits
24. affiliates.Othernamesmaybetrademarksoftheirrespective
25. owners.
26. Type'help;'or'\\h'forhelp.Type'\\c'toclearthecurrentinputstatement.
27. mysql&gt;

```
C:\Users\liqiong>mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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> update mysql.user set password=password("root") where User="root";
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

C:\Users\liqiong>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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>

```