1、連接Mysql
- 格式:
mysql -h主機地址 -u用戶名 -p用戶密碼
- 本地
mysql -utaoyali -ptaoyali
- 遠程
mysql -h192.168.110.110 -utaoyali -ptaoyali
注意用戶名前可以有空格也可以沒有空格,但是密碼前必須沒有空格,否則讓你重新輸入密碼。
2、修改密碼
- 格式:
mysqladmin -u用戶名 -p舊密碼 password 新密碼
- 給root加個密碼ab12
mysqladmin -u root -password ab12
注:因為開始時root沒有密碼,所以-p舊密碼一項就可以省略了。 如果進入了mysql后想修改密碼,就直接用mysql語句就好了:
set PASSWORD=PASSWORD("123");注意mysql語句以分號結束
- 再將root的密碼改為djg345
mysqladmin -u root -p ab12 password djg345
3.增加新用戶
注意:和上面不同,下面的因為是MYSQL環(huán)境中的命令,所以后面都帶一個分號作為命令結束符
格式:
grant select on 數(shù)據(jù)庫.* to 用戶名@登錄主機 identified by “密碼”
- 增加一個用戶test1密碼為abc,讓他可以在任何主機上登錄,并對所有數(shù)據(jù)庫有查詢、插入、修改、刪除的權限。首先用root用戶連入MYSQL,然后鍵入以下命令:
grant select,insert,update,delete on *.* to [email=test1@”%]test1@”%[/email]” Identified by “abc”;
但增加的用戶是十分危險的,你想如某個人知道test1的密碼,那么他就可以在internet上的任何一臺電腦上登錄你的mysql數(shù)據(jù)庫并對你的數(shù)據(jù)可以為所欲為了,解決辦法見??。
- 增加一個用戶test2密碼為abc,讓他只可以在localhost上登錄,并可以對數(shù)據(jù)庫mydb進行查詢、插入、修改、刪除的操作(localhost指本地主機,即MYSQL數(shù)據(jù)庫所在的那臺主機),這樣用戶即使用知道test2的密碼,他也無法從internet上直接訪問數(shù)據(jù)庫,只能通過MYSQL主機上的web頁來訪問了。
grant select,insert,update,delete on mydb.* to [email=test2@localhost]test2@localhost[/email] identified by “”;
4.數(shù)據(jù)庫操作
- 創(chuàng)建數(shù)據(jù)庫
create database database_name;
- 修改指定數(shù)據(jù)庫中所有varchar類型的表字段的字符集為UTF8,并將排序規(guī)則修改為utf8_general_ci
SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY `', column_name, '` ', DATA_TYPE, '(', CHARACTER_MAXIMUM_LENGTH, ') CHARACTER SET UTF8 COLLATE utf8_general_ci', (CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END), ';')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'databaseName'
AND DATA_TYPE = 'varchar'
AND
(
CHARACTER_SET_NAME != 'utf8'
OR
COLLATION_NAME != 'utf8_general_ci'
);
- 修改指定數(shù)據(jù)庫中所有數(shù)據(jù)表的字符集為UTF8,并將排序規(guī)則修改為utf8_general_ci
SELECT CONCAT('ALTER TABLE ', table_name, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;')
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'databaseName'
5.數(shù)據(jù)表數(shù)據(jù)操作
- 顯示所有的數(shù)據(jù)庫
show databases;
- 進入dataBase_name數(shù)據(jù)庫
use dataBase_name;
- 顯示dataBase_name數(shù)據(jù)庫的所有表
show tables;
- 顯示table_name表的字段信息
desc table_name;
- 創(chuàng)建表
create table MIFit_Image (name char(100), path char(100), count int(10), firstName char(100), firstMD5 char(100), secondName char(100), secondMD5 char(100), thirdName char(100), thirdMD5 char(100));
- 修改表名
rename table MIFit_Image to MIFit_Image_New
- 刪除表
drop table tableName;
- 插入數(shù)據(jù)
insert into MIFit_Image (name, path, count, firstName, firstMD5, secondName, secondMD5, thirdName, thirdMD5) VALUES ('test', 'test', 1, 'name1', 'md1', 'name2', 'md2', 'name3', 'md3');
- 查詢表中的數(shù)據(jù)
select * from MIFit_Image;
select * from MIFit_Image where name = 'test';
select * from MyClass order by id limit 0,2;
- 查詢數(shù)據(jù)庫中的重復數(shù)據(jù) ( | b71edca50989258e68fadcc3cb9bc689 | 2 | )
select firstMD5, count(*) as count from MIFit_Image group by firstMD5 having count > 1;
- 查詢重復數(shù)據(jù)的其它字段的數(shù)據(jù)
select folderName, path, firstMD5 from MIFit_Image where firstMD5 in (select firstMD5 from MIFit_Image group by firstMD5 having count(firstMD5) > 1);
- 更新數(shù)據(jù)
update MIFit_Image set folderName ='Mary' where id=1;
- 刪除數(shù)據(jù)
delete from MIFit_Image where folderName = 'test';
6.數(shù)據(jù)表字段操作
- 添加字段 (int 自增 不為null 主鍵)
alter table MIFit_Image add id int auto_increment not null primary key;
- 修改字段的順序 (id 放置最前)
ALTER TABLE 表名 MODIFY 字段名1 數(shù)據(jù)類型 FIRST | AFTER 字段名2;
其中:
字段名1:表示需要修改位置的字段的名稱。
數(shù)據(jù)類型:表示“字段名1”的數(shù)據(jù)類型。
FIRST:指定位置為表的第一個位置。
AFTER 字段名2:指定“字段名1”插入在“字段名2”之后。
alter table MIFit_Image modify id int first;
alter table MIFit_Image modify id int after name;
- 移除id主鍵標志
alter table MIFit_Image modify id int, drop primary key;
- 修改字段名name為folderName
alter table MIFit_Image change name folderName char(100);
- 刪除字段
alter table testTable drop folderName;
- 加索引
alter table MIFit_Image add index indexName (folderName);
- 刪除索引
alter table MIFit_Image drop index indexName;