1.:安裝與初始化mysql
1)安裝 sql
yum install -y mariadb\*數據庫
2)初始化centos
systemctl restart mariadb數據結構
systemctl enable mariadbide
mysql_secure_installationui
查看數據庫版本:select 'version' ;spa
除設置密碼外,一直摁"y"。rest
2.對數據庫的操做orm
1)查庫:show databases;
2)創庫:create database 庫名;
自定義數據庫字符集建立數據庫:GBK:create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
UTF8: create database test2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
3)刪庫:drop database 庫名;
4)進庫:use 庫名;
3.建立表
(1)瞭解常見數據類型
日期和時間數據類型
MySQL數據類型 |
含義 |
date |
3字節,日期,格式:2014-09-18 |
time |
3字節,時間,格式:08:42:30 |
datetime |
8字節,日期時間,格式:2014-09-18 08:42:30 |
timestamp |
4字節,自動存儲記錄修改的時間 |
year |
1字節,年份 |
整數型
MySQL數據類型 |
含義(有符號) |
tinyint |
1字節,範圍(-128~127) |
smallint |
2字節,範圍(-32768~32767) |
mediumint |
3字節,範圍(-8388608~8388607) |
int |
4字節,範圍(-2147483648~2147483647) |
bigint |
8字節,範圍(+-9.22*10的18次方) |
bigint |
8字節,範圍(+-9.22*10的18次方) |
浮點型
MySQL數據類型 |
含義 |
float(m, d) |
4字節,單精度浮點型,m總個數,d小數位 |
double(m, d) |
8字節,雙精度浮點型,m總個數,d小數位 |
decimal(m, d) |
decimal是存儲爲字符串的浮點數 |
字符串數據類型
MySQL數據類型 |
含義 |
char(n) |
固定長度,最多255個字符 |
varchar(n) |
可變長度,最多65535個字符 |
tinytext |
可變長度,最多255個字符 |
text |
可變長度,最多65535個字符 |
mediumtext |
可變長度,最多2的24次方-1個字符 |
longtext |
可變長度,最多2的32次方-1個字符 |
(2) create table 表名
(
列名1 數據類型,
列名2 數據類型,
列名3 數據類型
);
實例:create table list
(
id int,
name varchar(50),
passwd varchar(100)
);
4 .查看錶數據結構:desc 表名;
5.查詢數據庫內表詳情;show tables ; !!!(查以前,要進入相應的庫use 庫名;)
6.刪除一個表:drop table 表名;
7.修改表
(1)重命名錶:
alter table 舊錶名 rename 新表名;
(2)向表中添加一列:
alter table 表名 add 要添加的列名 數據類型;
(3)刪除表中的一列:
alert table 表名 drop column 被刪的列名;
(4)修改一個列的數據類型:
alter table 表名 modify column 列名 數據類型;
(5)重命名一個列:
alter table 表名 change column 舊列名 新列名 數據類型;
8.向表中插入語句
(1)向表中所有列都插入一條記錄:
insert into 表名稱 values (值1,值2,值3);
例:insert into name values (99,'zhangsan','zhangsan-passwd');
(2)指定列插入一條記錄:
insert into 表名稱(列1,列3) values (值1,值3);
例:insert into name(username,password) values (lisi,lisi-passwd);
9.查詢數據
(1)從表格中查詢數據記錄:
查詢表中所有列的數據記錄:select * from 表名稱;
例: select * from name;
(2)查詢表中指定列的數據記錄:select 列名1,列名2,列名3 from 表名稱;
例: select username,password from name;
10.按條件查詢數據
select 列名稱 from 表名 where 指定列 運算符 值; (知足條件則列出指定表的一行!"*"表明全部!)
例:select * from name where id=3 ;
select username,password from name where id=3 ;
select username,password from name where id>3;
select username,password from name where id<3;
select username,password from name where id<>3; 不等於
select username,password from name where id>=3;
select username,password from name where id<=3;
11.刪除一條記錄:
(1)刪除表中所有記錄:delete * from 表名稱:
例:delete * from name;
(2)刪除表中指定的記錄:delete from 表名稱 where 列名 運算符 值:
例:delete from name where id=3; !注意字符串須要單引號!
12.更新一條記錄
(1)從表中更新一條記錄:update 表名稱 set 列名稱=新值 where 列=值;
(2)mysql管理員密碼丟失找回:
1.關閉數據庫:systemctl stop mariadb
2.mysqld_safe --skip-grant-tables --user=mysql &
3.進入數據庫 mysql 直接進去 ,在數據庫內更改用戶密碼:update mysql.user set password=password("新密碼") where user='root' and host='localhost' ; 注!更新完畢,要使用 flush privileges ;刷新數據庫,並推出 \q 。
4.mysqladmin -u root -p shutdown 注意!!在此處輸入更新了的密碼,而後 重啓數據庫: systemctl restart mariadb
公式: update name set 變量名='值' where 判斷條件 and 判斷條件;
13.返回結果去除重複項:
select distinct 列名稱 from 表名稱;
例:select distinct username from name;
14.where條件中使用邏輯組合:
select * from 表名稱 where 列名1='值1' and 列名2='值2'; 必須知足and先後兩個條件 ==》爲真
select * from 表名稱 where 列名1='值1' or 列名2='值2'; 必須知足or先後的某一個條件 ==》爲真
例:select * from name where username=zhangsan and id=1 ;
select * from name where username=zhangsan or id=0 ;
15.對查詢結果進行排序:
(1)從小到大排序:select * from 表名稱 order by 列名稱;
例:select * from name order by id;
(2)從大到小排列:select * form 表名稱 order by 列名稱; desc; 注意desc在這裏是」倒序」的意思!
例:select * from name order by id desc;
16.mariadb用戶管理
(1)建立一個mariadb數據庫用戶:
create user 用戶名 identified by '密碼' ;
例:create user Luigi identified by 'redhat-passwd';
!使用 flush privileges ;刷新!
(2)刪除一個用戶:drop user '用戶名'@'主機名';
例:drop user 'Luigi'@'localhost';
!使用 flush privileges ;刷新!
(3)重命名用戶:rename user 原用戶名 to 新用戶名;
例: rename user Luigi to Natasha;
!使用 flush privileges ;刷新!
(4)修改用戶密碼:
①修改當前用戶密碼:set PASSWORD=PASSWORD('新密碼');
例 : set PASSWORD=PASSWORD('redhat-passwd');
!使用 flush privileges ;刷新!
②修改指定用戶密碼:set PASSWORD for 用戶名=PASSWORD('新密碼');
例:set PASSWORD for zhangsan=PASSWORD('zhangsan-passwd');
!使用 flush privileges ;刷新!
17.權限管理
(1)權限分類:1.檢查用戶是否能鏈接主機。
2.是否有操做數據庫的權限。
(2)受權層級:1.全局層級
2.數據庫層級
3.表層級
4.列層級
5.子程序層級
(3)使用grant指令授予用戶權限,使用revoke撤銷用戶權限
權限分類;
select 查詢權限
all 全部權限
usage (受限制!)登入權限
查看MySQL用戶權限:
show grants for 用戶名;
好比:
show grants for root@'localhost'; ### !!root用戶例外!!
①授予一個用戶權限grant 權限 on 層級 to '用戶名'@'主機名' identified by '密碼';
例:授予Luigi對所有數據庫的所有管理權限:grant all on *.* to 'Luigi'@'%' identified by 'redhat';
!使用 flush privileges ;刷新!
授予Luigi對centos數據庫的本地查詢權限:grant select on centos.* to 'Luigi'@'localhost' identified by 'redhat';
!使用 flush privileges ;刷新!
②撤銷一個用戶的權限:revoke all privileges from 用戶名;
例:revoke all on *.* from Luigi;
!使用 flush privileges ;刷新!
(4)主機鏈接認證:grant all privileges on *.* to '用戶'@'主機名' identified by '用戶密碼';
例:grant all privileges on *.* to 'Luigi'@'*.example.com' identified by 'redhat';
!使用 flush privileges ;刷新!
18.簡單備份與恢復:
(1)備份一個指定的數據庫:mysqldump -u root -p 數據庫名稱 > /備份的路徑/備份文件
例:mysqldump -u root -p rhce > /root/rhce.sql
(2)恢復一個數據庫:mysql -u root -p 數據庫的名稱 < /備份的路徑/數據庫名稱
例 : mysql -u root -p rhce < /root/rhce.sql