mysql目录
 1.       数据库目录
 /var/lib/mysql
 2.       配置文件
 /usr/share/mysql(mysql.server命令及配置文件)
 3.       相关命令
 /usr/bin(mysqladmin mysqldump等命令)
 4.       启动脚本
 /etc/rc.d/init.d/(启动脚本文件mysql的目录)
安装
 # yum -y install mysql*
 # service mysqld start
 # netstat -tlpn | grep mysql
 # mysql
 mysql默认没有密码,安装完毕增加密码的重要性是不言而喻的
 # /usr/bin/mysqladmin -u root password redhat
 # mysql
 error 1045: access denied for user: 'root@localhost' (using password: no)
 显示错误,说明密码已经修改
 用修改后的密码登录
 # mysql -u root -p
 enter password: (输入修改后的密码redhat)
 welcome to the mysql monitor. commands end with ; or /g.
 your mysql connection id is 4 to server version: 4.0.16-standard
 type 'help;' or '/h' for help. type '/c' to clear the buffer.
 mysql>
 这是通过mysqladmin命令修改口令,也可通过修改库来更改口令。
启动与停止
 1.       启动
 mysql安装完成后启动文件mysql在/etc/init.d目录下,在需要启动时运行下面命令即可
 # /etc/init.d/mysqld start
 2.       停止
 # /usr/bin/mysqladmin -u root -p shutdown
 3.       自动启动
 1)         察看mysql是否在自动启动列表中
 # /sbin/chkconfig --list
 2)         把mysql添加到系统的启动服务组里面去
 # /sbin/chkconfig -- add mysqld
 3)         把mysql从启动服务组里面删除
 # /sbin/chkconfig --del mysqld
mysql的常用操作
 1.       显示数据库
 mysql> show databases;
 +--------------------+
 | database           |
 +--------------------+
 | information_schema | 
 | mysql              | 
 | test               | 
 +--------------------+
 3 rows in set (0.00 sec)
 mysql刚安装完有三个数据库:information_schema、mysql和test。mysql库非常重要,它里面有mysql的系统信息,我们改密码和新增用户,实际上就是用这个库中的相关表进行操作。
 2.       显示数据库中的表
 mysql> use mysql;    (打开库,对每个库进行操作就要打开此库,类似于foxpro )
 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                      | 
 | help_category             | 
 | help_keyword              | 
 | help_relation             | 
 | help_topic                | 
 | host                      | 
 | proc                      | 
 | procs_priv                | 
 | tables_priv               | 
 | time_zone                 | 
 | time_zone_leap_second     | 
 | time_zone_name            | 
 | time_zone_transition      | 
 | time_zone_transition_type | 
 | user                      | 
 +---------------------------+
 17 rows in set (0.01 sec)
 3.       显示数据表的结构
 describe 表名;
 mysql> describe user;
 4.       显示表中的记录
 select * from 表名
 mysql> select * from user;
 5.       建库
 create database 库名
 mysql> create database lifang;
 query ok, 1 row affected (0.01 sec)
 6.       建表
 use 库名
 create table 表名(字段设定列表);
 例如:在刚创建的lifang库中建立表name,表中有id(序号,自动增长),xm(姓名),xb(姓别),csny(出身年月)四个字段
 mysql> use lifang;
 database changed
 mysql> create table name (id int(3) auto_increment not null primary key,xm char(8),xb char(2),csny date);
 query ok, 0 rows affected (0.01 sec)
 可以用describe命令察看刚建立的表结构
 mysql> describe name 
     -> ;
 +-------+---------+------+-----+---------+----------------+
 | field | type    | null | key | default | extra          |
 +-------+---------+------+-----+---------+----------------+
 | id    | int(3) | no   | pri | null    | auto_increment | 
 | xm    | char(8) | yes |     | null    |                | 
 | xb    | char(2) | yes |     | null    |                | 
 | csny | date    | yes |     | null    |                | 
 +-------+---------+------+-----+---------+----------------+
 4 rows in set (0.00 sec)
 7.       增加记录
 mysql> insert into name values('','lifang','female','1984-05-10');
 query ok, 1 row affected, 2 warnings (0.00 sec)
mysql> insert into name values('','fuying','female','1986-07-14');
 query ok, 1 row affected, 2 warnings (0.00 sec)
mysql> insert into name values('','xiaodi','female','1982-12-28');
 query ok, 1 row affected, 2 warnings (0.00 sec)
 可用select命令来验证结果
 mysql> select * from name;
 +----+--------+------+------------+
 | id | xm     | xb   | csny       |
 +----+--------+------+------------+
 | 1 | lifang | fe    | 1984-05-10 | 
 | 2 | fuying | fe   | 1986-07-14 | 
 | 3 | xiaodi | fe   | 1982-12-28 | 
 +----+--------+------+------------+
 3 rows in set (0.00 sec)
 8.       修改记录
 例如将xiaodi的出生年月改为1985-12-28
 mysql> update name set csny='1985-12-28' where xm='xiaodi';
 query ok, 1 row affected (0.00 sec)
 rows matched: 1 changed: 1 warnings: 0
 9.       删除记录
 mysql> delete from name where xm='xiaodi';
 query ok, 1 row affected (0.00 sec)
 10.   删库和删表
 drop database 库名
 drop table 表名
 mysql> drop table name;
 query ok, 1 rows affected (0.00 sec)
mysql> drop database lifang;
 query ok, 1 rows affected (0.00 sec)
增加mysql用户
 格式:grant select on 数据库.* to 用户名@登录主机 identified by “密码”
 例1、       增加一个用户fuyiing密码为redhat,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令
 mysql> grant select,insert,update,delete on *.* to fuying@station12 identified by redhat;
 query ok, 1 rows affected (0.00 sec)
 增加的用户是十分危险的,如果知道了fuying的密码,那么他就可以在网上的任何一台电脑上登录你的mysql数据库并对你的数据为所欲为了,解决办法见例2
 例2、       增加一个用户xiaodi密码为redhat,让此用户只可以在localhost上登录,并可以对数据库lifang进行查询、插入、修改、删除的操作(localhost指本地主机,即mysql数据库所在的那台主机),这样用户即使用知道xiaodi的密码,他也无法从网上直接访问数据库,只能通过 mysql主机来操作lifang库
 mysql> grant select,insert,update,delete on lifang.* to xiaodi@localhost identified by redhat;
 query ok, 1 rows affected (0.00 sec)
 用新增的用户如果登录不了mysql,在登录时用如下命令:
 # mysql -u fuying -p -h 192.168.0.12           (-h后跟的是要登录主机的ip地址)
备份与恢复
 1.       备份
 例如:将上例创建的lifang库备份到文件back_lifang中
 [root@station12 mysql]# mysqldump -u root -p --opt lifang > back_lifang
 enter password: 
 [root@station12 mysql]#
 2.       恢复
 [root@station12 mysql]# mysql -u root -p lifang  
enter password: 
 [root@station12 mysql]#
   
 
   