首先在ubuntu/debian上安装下列软件包:mysql-client- 5.0 mysql-server-5.0
在Redhat上安装下列软件包:mysql-3.23.54a-11.i386.rpm mysql-server-3.23.54a-11.i386.rpm
和mysql-devel-3.23.54a-11.i386.rmp (可以装上更高的版本,本文在ubuntu下试验)
1、在终端运 行:mysql_install_db (这一步好像可以不要)
/*启动mysqld
服务器并且建立初始MySQL 授权表,包含决定用户如何被允许连接服务器的权限*/
2、启动数据库
方法一:终端运行:sudo mymysqld_safe & //启动mysql数据库
方法二:终端运行:sudo /etc/init.d/mysql start
3、检查MYSQL是否被启动
方法一:终端运行:pstree | grep mysqld
出现如下结果则表明已经启动:
| -mysqld_safe-+-logger
| `-mysqld---9*[{mysqld}]
方法二:终端运行:sudo /etc/init.d/mysql status
出现如下结果则表明已经启动:(不同的系统版本略有差异)
* /usr/bin/mysqladmin Ver 8.41 Distrib 5.0.24a, for pc-linux-gnu on i486
Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Server version 5.0.24a-Debian_9-log
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/run/mysqld/mysqld.sock
Uptime: 1 min 23 sec
Threads: 1 Questions: 147 Slow queries: 0 Opens: 98 Flush tables: 1 Open tables: 14 Queries per second avg: 1.771
另:如想开机后自动启动,可下载一个sysvconfig命令:sudo apt-get install sysvconfig (Redhat里有这个命令可用:ntsysv) 然后在终端运行:sudo sysvconfig 在menu中进入Enable/Disable,将mysql选中保存即可。
4、关闭MYSQL数据库
方法一、sudo /etc/init.d/mysql stop
方法二、sudo mysqladmin shutdown
5、进入数据库
终端运行:mysql -u root -p //进入密码为空,mysql第一次默认root用户密码为空,敲回车即可
6、创建数据库
mysql> create database test; //创建test数据库
输出结果:Query OK, 1 row affected (0.00 sec)
mysql> show databases; //显示所有的数据库
输出结果:
+--------------------+
| Database |
+--------------------+
| information_schema |
| bear |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
7、删除数据库
mysql> database test; //删除test数据库
输出结果:
Query OK, 0 rows affected (0.00 sec)
mysql> show databases; //显示所有的数据库
输出结果:
+--------------------+
| Database |
+--------------------+
| information_schema |
| bear |
| mysql |
+--------------------+
3 rows in set (0.01 sec)
8、创建表
mysql> create database test; //创建test数据库
mysql> use test; //进入到test数据库中
mysql> show tables ; //查看所有的表
输出显示结果:Empty set (0.00 sec)
mysql> create table people( // 创建一个表名为people的表,四列分别是:
-> people_id char(6) primary key, //people_id 为主键
-> name char(10), //name
-> sex char(2), //sex
-> birthday date); //birthday
输出结果:
Query OK, 0 rows affected (0.02 sec)
mysql> show tables ; //显 示所有的表
输出结果:
+----------------+
| Tables_in_test |
+----------------+
| people |
+----------------+
1 row in set (0.00 sec)
9、删除表
/* 这一步可到最后做,因为下面还要用到表people */
mysql> table people; //删除表格
mysql> show tables ; // 显示所有的表
10、查看表结构
mysql> use test; //进入要查看表结构的数据库
mysql> describe people; //查看表结构
显示结果:
+--------+-------+----+----+------+-----+
| Field | Type | Null | Key | Default | Extra |
+--------+-------+----+--- +------+------+
| people_id | char(6) | NO | PRI | | |
| name | char(10) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+--------+-------+---+-----+-----+------+
4 rows in set (0.00 sec)
11、向表中添加和查看数据
mysql> use test; //进入test数据库
mysql> into people values //向people表中添加两组数据
-> ('200701','熊锋','m','1983/04/23');
mysql> into people values
->('200702','郑玲','w','1986-08-18');
mysql> select * people; //查看people表中的数据
显示结果:
+-----------+--------+------+------------+
| people_id | name | sex | birthday |
+-----------+--------+------+------------+
| 200701 | 熊锋 | m | 1983-04-23 |
| 200702 | 郑玲 | w | 1986-08-18 |
+-----------+--------+------+------------+
2 rows in set (0.00 sec)
mysql> select * people people_id='200701';
//查看people表中主键值为200701的数据
显示结果:
+-----------+--------+------+---------+
| people_id | name | sex | birthday |
+-----------+--------+------+---------+
| 200701 | 熊锋 | m | 1983-04-23 |
+-----------+--------+------+---------+
1 row in set (0.00 sec)
12、修改表结构
mysql> alter table people add score int(3) not null;
显示结果:
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> describe people; //显示 表结构
显示结果:
+--------+-------+----+----+------+----+
| Field | Type | Null | Key | Default | Extra |
+--------+-------+----+---+-------+----+
| people_id | char(6) | NO | PRI | | |
| name | char(10) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| core | int(11) | YES | | NULL | |
| score | int(3) | NO | | | |
+--------+-------+----+--+--------+----+
6 rows in set (0.00 sec)
注:MYSQL中的数据类型
Data Type Default Value
char/varchar/binary ""
date 0000-00-00
time 00:00:00
datetime 0000-00-00 00:00:00
timestamp 2005-06-21 12:01:00(now()!)
year 0000
enum('a','b','c') 'a'
set('a','b','c') ()(empty set)