1. 程式人生 > >mysqls數據庫的常用管理命令

mysqls數據庫的常用管理命令

rom out rem defaults 開啟 物理 pda 自動 hang

庫,表管理
Mysqladmin –u root –p password ‘123123’ //設置數據庫密碼
Show databases; //查看數據庫
Create database school; //創建數據庫(school)
Use school; //進入數據庫(school)
Show tables; //查看school中的表
Create table info(id int not null primary key auto_increment,name char(10)not null,score decimal(5,2),hobby int(2)); //創建表(info)
if not exists info( id int(4) primary key, name varchar(10) not null, card varchar(50) unique key, hobby int(4)); //如info表不存在則建立該表
alter table test01 rename info01 //修改表名
alter table info change name username varchar(10) unique key; //修改信息名,並設置約束
alter table info add address varchar(50) default ‘地址不詳‘ ; //添加信息
alter table info drop password; //刪除信息
alter table hob add constraint pk_id primary key hob (sid); //添加主鍵
Select from info //查看info表中的數據
Insert into info (id,name,score) values (6,’test’,null); //插入數據
5
Select
from info where id=6; //條件篩選
Update info set score=75 where id=6; //更新信息
Delete from info where name=’test’; //刪除信息
Select from info where 1=1 order by score(asc升序)(desc降序); //排序(升序/降序)
Desc info; //查看表結構
Select info.name,info.score,hob.hobname from info inner join hob where info.hobby=hob.id;
select username from info where hobby=(select sid from hob where hobname=‘遊泳‘);
//多表查詢
select a.name,a.score,b.hobname from info a inner join hob b where a.hobby=b.id;
//別名查詢
create table infos select a.name,a.score,b.hobname from info a inner join hob b where a.hobby=b.id; //新建表infos內容為多表查詢結果
create table test01(select a.username,b.hobname from info a inner join hob b where a.hobby=b.sid); //也可以這麽寫
Drop table infos; //刪除infos表
統計count()
Select count(
) from infos;
平均值avg()
Select avg(score) from infos

事務,視圖
事務:一組操作共同執行或者都不執行,結果保持一致
Begin 開始
Commit 提交
roll back 回滾(回到bigin之前的狀態)
原子性:不可分割,視為一個整體
一致性:前後結果保持一致
隔離性:對數據進行修改的所有並發事務是彼此隔離的
持久性:執行成功不可修改
Set autocommit=0;禁止自動提交
Set autocommit=1;開啟自動提交

索引
Create index <索引的名字>ON tablename(列的列表)
Create unique index <索引的名字>ON tablename(列的列表)
Create index id_index on info(id); //創建普通索引
Create unique index id_index on info(id); //創建唯一索引
Create table infos (descript TEXT,FULLTEXT(descript)); //創建全文索引
Create index id_age_index on info
Show index from info;
Drop index id_index from info; //刪除索引
Alter table info add primary key(id); //修改表增加主鍵
Alter table info add column age int; //增加列
Alter table info drop column age int; //刪除列

賬戶管理
Select user,authentication_string,host from user; //查詢已創建的用戶
Create user ‘test01’@’localhost’ identified by ‘abc123’; //創建用戶
Password(‘abc123’) //生成密碼abc123的密文
drop user ‘test02’@’localhost’; //刪除用戶
rename user ‘test01’@’localhost’ to ‘user01’@’192.168.175.128’; //賬戶重命名;
set password for ‘user02‘@‘localhost‘ = password‘abc123‘; //修改賬戶密碼
mysqld –skip-grant-tables //跳過user表登錄(修改root密碼,先關服務1)

vim /etc/my.cnf(2)
[mysqld]
port = 3306
socket = /home/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
skip-grant-tables //添加如下內容(跳過user表登錄)
update mysql.user set authentication_string = password(‘123abc’) where user=’root’;
給root用戶設置密碼
Grant 權限 on 數據庫.表to 用戶@主機 identified by 密碼
grant all on . to ‘test02‘@‘localhost‘ identified by ‘abc123‘; //創建新用戶或更改已有用戶數據
Grant select school.info to ‘tom’@’localhost’ identified by ‘abc123’; //給tom賬戶查看school庫下,info表的權限
Show grants; //查看用戶權限
revoke 權限 on 數據庫.表 from 用戶@主機;
revoke update on . from ‘user02’@’localhost’; //撤銷權限

[client]
Default-character-set=utf8

日誌管理
vim /etc/my.cnf
#錯誤日誌
log-error=/usr/local/mysql/data/mysql_error.log
#通用日誌
general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log
#二進制日誌
log_bin=mysql-bin
#慢日誌
slow_query_log=ON
slow_query_log_file=mysql_slow_query.log
long_query_time=1
mysqlbinlog --no-defaults mysql-bin.000001 //查看二進制日誌
mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000001 //解碼查看二進制日誌

數據庫備份
tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data/ //將mysql數據庫文件夾進行打包操作。(物理層面的備份)

du -sh /usr/local/mysql/data/
du -sh /opt/mysql-2018-08-30.tar.xz //檢查文件大小
mysqldump -u root -p school info > /opt/info.sql //將school庫中的info表信息導出放在/opt/info.sql中

mysqldump -u root -p -d school info > /opt/info.sql //將school庫中的info表結構導出放在/opt/info.sql中

mysql -u root -p school < /opt/info.sql //將/opt/info.sql中的信息導回數據庫school中

mysqldump -uroot -pabc123 school > /opt/school.sql
Mysqldump –uroot –p --databases school mysql > /opt/school.sql //將school和mysql庫下的信息保存到/opt/school.sql文件夾下
Mysqldump –uroot –p –all-databases
Source /opt/school.sql; //在數據庫中倒回文件

mysqldump –uroot –pabc123 school > school.sql
提前創建數據庫school
mysql -u root –p123 school < school.sql

mysqldump -u root -p --databases school > school.sql
無需提前創建數據庫
mysql -u root –p123 < school.sql

mysqlbinlog --no-defaults mysql-bin.000001 | mysql -uroot -p //增量還原

mysqls數據庫的常用管理命令