- 連接數(shù)據(jù)庫(kù)
- mysql -u root -p "密碼"
-h 連接的主機(jī)ip
-u 連接的用戶(hù)名
-p 密碼
2 數(shù)據(jù)庫(kù)操作
- 創(chuàng)建數(shù)據(jù)庫(kù)
create database “數(shù)據(jù)庫(kù)名稱(chēng)” charset=utf8; - 刪除數(shù)據(jù)庫(kù)
drop database “數(shù)據(jù)庫(kù)名稱(chēng)”; - 切換數(shù)據(jù)庫(kù)
use “數(shù)據(jù)庫(kù)名稱(chēng)”; - 查看當(dāng)前選擇的數(shù)據(jù)庫(kù)名稱(chēng)
select database();
- 表操作
- 查看當(dāng)前數(shù)據(jù)庫(kù)中所有的表
show tables; - 創(chuàng)建表
create table "表名"(列及類(lèi)型)
如: create table students( id int auto_increment primary key, sname varchar(10) not null ); - 修改表
alter table “表名” drop|add|change 列名 類(lèi)型; - 刪除表兜材;
drop table “表名”; - 查看表結(jié)構(gòu)
desc “表名”; - 更改表名稱(chēng)
rename table “原表名” to “新表名”; - 查看表的創(chuàng)建時(shí)間贼急;
show create table “表名”;
- 數(shù)據(jù)操作
- 查詢(xún)
select * from "表名"; - 修改
update “表名” set 列1=值1,... where 條件 - 刪除
delete from 表名 where 條件 - 增加
全列插入:insert into 表名 values(...)
缺省插入:insert into 表名(列1,...) values(值1,...)
同時(shí)插入多條數(shù)據(jù):insert into 表名 values(...),(...)...;
或insert into 表名(列1,...) values(值1,...),(值1,...)...;
主鍵列是自動(dòng)增長(zhǎng),但是在全列插入時(shí)需要占位,通常使用0儿惫,插入成功后以實(shí)際數(shù)據(jù)為準(zhǔn)
5 備份和恢復(fù)
備份
1). 進(jìn)入超級(jí)管理員
sudo -s
2)進(jìn)入mysql庫(kù)目錄
cd /var/lib/mysql
3)運(yùn)行mysqldump命令
mysqldump –uroot –p 數(shù)據(jù)庫(kù)名 > ~/Desktop/備份文件.sql;
4)按提示輸入mysql的密碼
數(shù)據(jù)恢復(fù)
連接mysql,創(chuàng)建數(shù)據(jù)庫(kù)
退出連接列荔,執(zhí)行如下命令
mysql -uroot –p 數(shù)據(jù)庫(kù)名 < ~/Desktop/備份文件.sql
根據(jù)提示輸入mysql密碼