2. 初始化数据库
[iyunv@AndyJiang AndyJiang]# cd /usr/bin
[iyunv@AndyJiang bin]# ./mysql_install_db
成功的话会出现以下信息:
Installing MySQL system tables...
OK
可能有一些警告信息
3. 启动mysql数据库
[iyunv@AndyJiang bin]# /etc/init.d/mysqld start
成功的话会出现以下信息:
启动 MySQL: [确定]
4. 为root用户设置密码
mysql安装后默认是没有密码的
[iyunv@AndyJiang bin]# mysqladmin -u root password '******'
//******为新设置的密码,为了好记,我把密码直接设为mysql。
5. 登录数据库
[iyunv@AndyJiang bin]# mysql -u root -p
Enter password:
成功的话会出现以下信息:
Welcome to the MySQL monitor. Commands end with ; or "g.
Your MySQL connection id is 5
Server version: 5.0.67 Source distribution
Type 'help;' or '"h' for help. Type '"c' to clear the buffer.
6. 数据库常用操作
注意:MySQL中每个命令后要用分号“;”结尾
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
其实上边的数据库mysql是很重要的,它里面有MySQL的系统信息,密码的修改和增加新用户等,实际上就是在操作这个数据库。 7、显示数据库中的表
mysql> use mysql; //首先要打开数据库。
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables; //显示表。
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| func |
|
| user_info |
+---------------------------+
18 rows in set (0.00 sec) 8、显示数据表的结构:
describe 表名; 9、建数据库:
create database 库名;
例如:创建一个名字位wangxiuhua的库 mysql> create databases wangxiuhua;
Query OK, 1 row affected (0.00 sec) 10、建立数据表
use 库名;
create table 表名 (字段设定列表);
mysql> create table xingming (id int(3) auto_increment not null primary key,xm char(2),csny date);
Query OK, 0 rows affected (0.00 sec) 11、建立数据表
describe命令查看刚才建立的表结构
mysql> describe xingming;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(3) | NO | PRI | NULL | auto_increment |
| xm | char(2) | YES | | NULL | |
| csny | date | YES | | NULL | |
+-------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec) 12、增加记录
例如:增加几条相关纪录。 mysql> insert into name values('王秀华','男','1980-05-05'); mysql> insert into name values('岩岩','女','1980-05-05');
可用select命令来验证结果。 mysql> select * from name; 13、显示表中的记录:
select * from 表名;
例如:显示mysql库中user表中的纪录。所有能对MySQL用户操作的用户都在此表中。
Select * from user;
mysql> select * from xingming;
+----+------+------------+
| id | xm | csny |
+----+------+------------+
| 1 | | 1978-05-05 |
| 2 | | 1980-05-05 |
+----+------+------------+
2 rows in set (0.00 sec)