mysql目錄
1. 數(shù)據(jù)庫目錄
/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)
顯示錯誤,說明密碼已經(jīng)修改
用修改后的密碼登錄
# 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添加到系統(tǒng)的啟動服務組里面去
# /sbin/chkconfig -- add mysqld
3) 把mysql從啟動服務組里面刪除
# /sbin/chkconfig --del mysqld
mysql的常用操作
1. 顯示數(shù)據(jù)庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql剛安裝完有三個數(shù)據(jù)庫:information_schema、mysql和test。mysql庫非常重要,它里面有MySQL的系統(tǒng)信息,我們改密碼和新增用戶,實際上就是用這個庫中的相關表進行操作。
2. 顯示數(shù)據(jù)庫中的表
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. 顯示數(shù)據(jù)表的結構
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 表名(字段設定列表);
例如:在剛創(chuàng)建的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 數(shù)據(jù)庫.* to 用戶名@登錄主機 identified by “密碼”
例1、 增加一個用戶fuyiing密碼為redhat,讓他可以在任何主機上登錄,并對所有數(shù)據(jù)庫有查詢、插入、修改、刪除的權限。首先用以root用戶連入MySQL,然后鍵入以下命令
mysql> grant select,insert,update,delete on *.* to fuying@station12 identified by "redhat";
Query OK, 1 rows affected (0.00 sec)
增加的用戶是十分危險的,如果知道了fuying的密碼,那么他就可以在網(wǎng)上的任何一臺電腦上登錄你的MySQL數(shù)據(jù)庫并對你的數(shù)據(jù)為所欲為了,解決辦法見例2
例2、 增加一個用戶xiaodi密碼為redhat,讓此用戶只可以在localhost上登錄,并可以對數(shù)據(jù)庫lifang進行查詢、插入、修改、刪除的操作(localhost指本地主機,即MySQL數(shù)據(jù)庫所在的那臺主機),這樣用戶即使用知道xiaodi的密碼,他也無法從網(wǎng)上直接訪問數(shù)據(jù)庫,只能通過 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. 備份
例如:將上例創(chuàng)建的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 < back_lifang
Enter password:
[root@station12 mysql]#