一、從命令行登錄MySQL數據庫服務器
1、登錄使用默認3306端口的MySQL
/usr/local/mysql/bin/mysql -u root -p
2、通過TCP連接管理不同端口的多個MySQL(注意:MySQL4.1以上版本才有此項功能)
/usr/local/mysql/bin/mysql -u root -p --protocol=tcp --host=localhost --port=3307
3、通過socket套接字管理不同端口的多個MySQL
/usr/local/mysql/bin/mysql -u root -p --socket=/tmp/mysql3307.sock
4、通過端口和IP管理不同端口的多個MySQL
/usr/local/mysql/bin/mysql -u root -p -P 3306 -h 127.0.0.1
--------------------------------------------------------------------------------
二、數據庫操作SQL語句
1、顯示服務器上當前存在什麼數據庫
SHOW DATABASES;
2、創建名稱為rewin的數據庫
CREATE DATABASE rewin;
3、刪除名稱為rewin的數據庫
DROP DATABASE rewin;
4、選擇rewin數據庫
USE rewin;
--------------------------------------------------------------------------------
三、表操作SQL語句(登錄之後必須用以上的USE命令選擇一個數據庫,再進行表操作)
1、顯示當前數據庫中存在什麼表
SHOW TABLES;
2、創建數據庫表zhangyan:在mysql>後粘貼以下SQL語句,存儲引擎為MYISAM,字段id為主鍵、唯一索引。
CREATE TABLE `zhangyan` (
`id` INT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 20 ) NOT NULL ,
`password` CHAR( 32 ) NOT NULL ,
`time` DATETIME NOT NULL ,
`number` FLOAT( 10 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;
3、查看zhangyan表結構
DESCRIBE zhangyan;
4、從表中檢索信息
4.1、從zhangyan表中檢索所有記錄
SELECT * FROM zhangyan;
4.2、從zhangyan表中檢索特定的行:字段username等於abc,字段number等於1,按字段id降序排列
SELECT * FROM zhangyan WHERE username = abc AND number=1 ORDER BY id DESC;
4.3、從zhangyan表中檢索指定的字段:username和password
SELECT username, password FROM zhangyan;
4.4、從zhangyan表中檢索出唯一的不重復記錄:
SELECT DISTINCT username FROM zhangyan;
5、插入信息到zhangyan表
INSERT INTO zhangyan (id, username, password, time, number, content) VALUES (, abc, 123456, 2007-08-06 14:32:12, 23.41, hello world);
6、更新zhangyan表中的指定信息
UPDATE zhangyan SET content = hello china WHERE username = abc;
7、刪除zhangyan表中的指定信息
DELETE FROM zhangyan WHERE id = 1;
8、清空zhangyan表
DELETE FROM zhangyan;
9、刪除zhangyan表
DROP TABLE zhangyan;
10、更改表結構,將zhangyan表username字段的字段類型改為CHAR(25)
ALTER TABLE zhangyan CHANGE username username CHAR(25);
11、將當前目錄下的mysql.sql導入數據庫
SOURCE ./mysql.sql;
--------------------------------------------------------------------------------
四、數據庫權限操作SQL語句
1、創建一個具有root權限,可從任何IP登錄的用戶sina,密碼為zhangyan
GRANT ALL PRIVILEGES ON *.* TO sina@% IDENTIFIED BY zhangyan;
2、創建一個具有"數據操作"、"結構操作"權限,只能從192.168.1.***登錄的用戶sina,密碼為zhangyan
GRANT SELECT , INSERT , UPDATE , DELETE , FILE , CREATE , DROP , INDEX , ALTER , CREATE TEMPORARY TABLES , CREATE VIEW , SHOW VIEW , CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON *.* TO [email protected].% IDENTIFIED BY zhangyan;
3、創建一個只擁有"數據操作"權限,只能從192.168.1.24登錄,只能操作rewin數據庫的zhangyan表的用戶sina,密碼為zhangyan
GRANT SELECT , INSERT , UPDATE , DELETE ON rewin.zhangyan TO [email protected] IDENTIFIED BY zhangyan;
4、創建一個擁有"數據操作"、"結構操作"權限,可從任何IP登錄,只能操作rewin數據庫的用戶sina,密碼為zhangyan
GRANT SELECT , INSERT , UPDATE , DELETE , CREATE , DROP , INDEX , ALTER , CREATE TEMPORARY TABLES , CREATE VIEW , SHOW VIEW , CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON rewin.* TO sina@% IDENTIFIED BY zhangyan;
5、刪除用戶
DROP USER sina@%;
6.MySQL中將字符串aaa批量替換為bbb的SQL語句
UPDATE 表名 SET 字段名 = REPLACE (字段名, aaa, bbb);
7.修復損壞的表
①、用root帳號從命令行登錄MySQL:
mysql -u root -p
②、輸入root帳號的密碼。
③、選定數據庫名(本例中的數據庫名為student):
use student;
④、修復損壞的表(本例中要修復的表為smis_user_student):
repair table smis_user_student;udent;