?MariaDB數(shù)據(jù)庫
yum groupinstall MariaDB? ? 安裝MariaDB?
systemctl enable mariadb.service? ? ?開機自啟
systemctl restart mariadb.service? ? ?啟動一下
netstat -antulp | grep mysql? ? 看一下端口
firewall-cmd --permanent --add-service=mysql? ? ?防火墻設(shè)置一下
firewall-cmd --reload? ? ?重新讀配置
vim /etc/my.cnf????MariaDB的配置文件?官方默認
?MariaDB整了四套配置文件
ll /usr/share/mysql/????四套配置文件在這里
ll /var/lib/mysql/? ? 存放數(shù)據(jù)文件徐钠,一個文件夾就是一個數(shù)據(jù)庫
剛裝完默認沒有密碼,直接mysql就進入MariaDB界面
show databases;? ? 查看有幾個數(shù)據(jù)庫
use mysql? ? 使用mysql數(shù)據(jù)庫
show tables;? ? 查看有幾張表
desc user;? ? 查看user表的結(jié)構(gòu)
select database();? ? 查看當(dāng)前所在數(shù)據(jù)庫
select user();? ? ?查看當(dāng)前身份
show variables like 'innodb%';? ? 查看環(huán)境變量
ctrl + d 退出MariaDB
mysqladmin -u root password '123456'? ? 給root設(shè)置登陸MariaDB的密碼
mysql -u root -p? ? 設(shè)置密碼后就這樣子登陸
MariaDB [(none)]> status? ? 查看數(shù)據(jù)庫信息
出去locale 查看當(dāng)前語言環(huán)境
MariaDB [(none)]> system ls -l /? ? 調(diào)用系統(tǒng)命令
MariaDB [(none)]> source /scott.sql? ? 調(diào)用腳本建庫https://my.oschina.net/iamhere/blog/357809?p={{currentPage-1}}
MariaDB [scott]> show create database scott;? ? 查看數(shù)據(jù)庫語言環(huán)境
MariaDB [scott]>show create table emp;? ? 查看表的語言環(huán)境
alter database scott charset utf8;? ? 修改數(shù)據(jù)庫語言環(huán)境
alter table emp charset utf8;? ? 修改表的語言環(huán)境
這樣子改太麻煩 vim /etc/my.cnf直接去配置文件定義
systemctl restart mariadb.service? ? 重啟服務(wù)就語言環(huán)境就改過來了
但是改了配置文件以后是以后建立的才有效博个,之前的不行
drop database scott;? ? 刪除scott數(shù)據(jù)庫,然后再建立一次就好了哈哈
/////////////////////////
select * from xxx;? ?顯示全表
select ename,sal? from xxx桨武;? ? 就顯示?xxx表中的ename,sal
select ename,sal+10000 from xxx景东;? ? 給每條sal加10000
select ename,sal+10000 as newsal from xxx闻察;? ? ?給,sal起個別名
select ename,sal+10000? newsal from xxx 邻梆;? ? as不寫也可以
select ename,(sal+200)*3 from xxx 守伸;? ? 支持括號
select ename,sal+ifnull(comm绎秒,0)??as zhonshouru from xxx浦妄;? ? ?如果ifnull()里面是空值就改成0(null空值加減乘除會變null)
select distinct deptno from tmp;? ? ? ? 合并重復(fù)行
select * from xxx where?deptno=30 ;? ?加條件
select * from xxx where?sal>3000 剂娄;? ?加判斷
select * from xxx where?ename like ‘%TT%’蠢涝;? ?模糊查找(字符和日期要加單引號)
select * from xxx where?ename like ‘_TT%’;? ? 下劃線明確前面只有一個字符阅懦,一個_等于一個字符
select * from emp where deptno=30 and sal>2000;? ? 與? ?支持與或非
select * from emp where deptno=30 or sal>2000;???或??支持與或非
select * from emp where not sal>2000;????非????支持與或非
select * from emp order by sal;? ? 排序和二,默認升序,從小到大
select * from emp order by sal desc;? ? 降序
select * from emp order by deptno,sal;? ? 先按deptno排序耳胎,再按sal排序
select concat(ename,sal,comm) from emp;? ? 連接ename,sal,comm結(jié)果集
select concat(ename,sal,ifnull(comm,0)) from emp;? ? 函數(shù)嵌套
select concat(ename,'\'s sal is ',sal) from emp;? ? 連接字符串惯吕,引號需要轉(zhuǎn)義符 \
select count(*) from emp;? ? 統(tǒng)計幾行
select sum(sal),min(sal),max(sal),avg(sal) from emp;? ?總和, 最小怕午,最大废登,平均值,多行函數(shù)
select deptno,sum(sal),min(sal),max(sal),avg(sal) from emp group by deptno;? ? 分類group by?
select ename,dname from emp,dept where emp.deptno=dept.deptno;? ? 多表查詢
select y.ename,j.ename from emp y,emp j where y.mgr=j.empno;? ? 同一張表多表查詢郁惜,起了別名y和j
select ename,sal,grade from emp,salgrade where sal between losal and hisal;?多表查詢堡距,sal在losal和?hisal之間對應(yīng)列出
select ename,dname,sal,grade from emp,dept,salgrade where emp.deptno=dept.deptno and emp.sal between losal and hisal;三表查詢
create database qindatabase;? ? 創(chuàng)建數(shù)據(jù)庫
create table qintable(id int(2),name varchar(10),mail varchar(20));? ? 建表
insert into qintable(id,name,mail) values(1,'qin1','qin@qin.com');? ? 插入數(shù)據(jù)
insert into qintable values(2,'qin2','qin2@qin.com');? ? 每一列都插前面可以不寫
insert into qintable(id,name) values(3,'qin3');? ? 插入個別列
insert into qintable(id,mail) values(4,'qin4@qin.com');????? 插入個別列
insert into qintable values(5,'qin5','qin5@qin.com'),(6,'qin6','qin6@qin.com'),(7,'qin7','qin7@qin.com');? ? 一次插入多行數(shù)據(jù)
delete from qintable where id=4;? ? 刪除第4行
update qintable set name='bing' where id=2;? ? 把id等2的name改成bing
create table qin1 like qintable;? ? 采用qintable的表結(jié)構(gòu)建立qin1表
insert into qin1 select * from qintable;? ? 把?qintable表的數(shù)據(jù)插入到qin1表
create table qin2 as select * from qintable;? ? 直接創(chuàng)建一個結(jié)構(gòu)數(shù)據(jù)跟qintable都一樣的表qin2
create table qin3 as select * from qintable where 0=1;? ??采用qintable的表結(jié)構(gòu)建立qin1表,where 0=1不成立所以沒數(shù)據(jù)
alter table qintable add newlist varchar(20);? ? 給表qintable添加一列叫newlist
alter table qintable drop newlist;? ? 刪除newlist這列
alter table qintable add firstlist varchar(20) first;? ? 在第一列(first)添加一列叫firstlist?
alter table qintable add afterid varchar(20) after id;? ? 在id列后面(after id)添加一列afterid
delete from qin1;? ? 刪除qin1表的所有內(nèi)容
drop table qin1;? ? 直接刪除qin1表
truncate qin3;????刪除qin3表的所有內(nèi)容? 兆蕉,真刪羽戒,磁盤上也沒了
drop database qindatabase;? ? 刪除數(shù)據(jù)庫
//////////////外部數(shù)據(jù)導(dǎo)入
vim /qin.txt????建數(shù)據(jù)
MariaDB [(none)]> create database qindatabase;? ? 從新建庫
MariaDB [(none)]> use qindatabase;? ? 進入
create table qintable(id int(4),name varchar(10),email varchar(20));? ? 建表
load data infile '/qin.txt' into table qintable fields terminated by ',' lines terminated by '\n';? ? 將qin.txt的數(shù)據(jù)導(dǎo)入表qintable,后面的fields terminated是數(shù)據(jù)的分隔符是逗號虎韵,lines terminated 結(jié)束符是回車\n
updata qintable set name='bing' where id=2;? ? 還能修改內(nèi)容易稠,但是外面qin.txt的數(shù)據(jù)不變
select * from qintable into outfile '/var/lib/mysql/qindatabase/qintable.txt' fields terminated by ',' lines terminated by '\n';? ? 將qintable表的數(shù)據(jù)導(dǎo)出到'/var/lib/mysql/qindatabase/下的qintable.txt? ??
導(dǎo)出位置注意權(quán)限問題,還要注意SElinux上下文
?mkdir /backup
semanage fcontext -a -t mysqld_db_t '/backup{.*}?'? ? 修改上下文
restorecon -RFv /backup/????還得還原一下才生效
////////////////備份還原
MariaDB [(none)]>use scott;? ? 進入scott數(shù)據(jù)庫
退出MariaDB 操作
mysqldump -u root -p scott > /scott.dump? ? 使用root身份將數(shù)據(jù)庫scott備份到/scott.dump? ?包蓝,需要輸入root密碼
mysqldump -u root -p scott emp > /scott.emp.dump????將數(shù)據(jù)庫scott的emp表備份到/scott.emp.dump
mysqldump -u root -p scott dept salgrade > /scott.dept+salgrade.dump? ? ?同時備份dept缩多,salgrade表,多表備份
進入MariaDB 操作
MariaDB [(none)]> drop database scott;? ? 刪了scott數(shù)據(jù)庫
MariaDB [(none)]> create database qin;? ? 建立空數(shù)據(jù)庫qin
退出MariaDB 操作
mysql -u root -p qin < /scott.dump? ? ?使用root身份還原數(shù)據(jù)庫qin养晋,數(shù)據(jù)來源/scott.dump
進入MariaDB 查看發(fā)現(xiàn)數(shù)據(jù)回來了
////////////////單表還原
MariaDB [qin]> drop database qin;????刪了qin數(shù)據(jù)庫
MariaDB [(none)]> create database qin;?????建立空數(shù)據(jù)庫qin
退出MariaDB 操作
mysql -u root -p qin < /scott.emp.dump? ? ?單表導(dǎo)入
mysql -u root -p qin < /scott.dept+salgrade.dump
進入查看數(shù)據(jù)回來了
////////////////////MariaDB用戶權(quán)限
user mysql? ? 進入mysql數(shù)據(jù)庫
show tables;? ? 其中user表存放MariaDB的用戶
MariaDB [mysql]> create user qin@'%' identified by '123456';? ? ?創(chuàng)建用戶qin衬吆,登陸位置所有ip都可以連接過來,密碼123456绳泉,
去登陸發(fā)現(xiàn)不行逊抡,因為它需要所有網(wǎng)絡(luò)登陸才能進入
去2號機安裝MariaDB
yum install mariadb
mysql -h 192.168.100.1 -u qin -p? ? 通過遠程登陸192.168.100.1使用qin賬戶就可以登陸了
MariaDB [(none)]> create user qin@'localhost' identified by '123456';? ? 再回主機創(chuàng)建一個localhost用戶,這個用戶就可以直接登陸
MariaDB [mysql]> drop user qin@'%';? ? 把這qin百分百用戶給刪了
MariaDB [mysql]>?create user qin@'192.168.100.2' identified by '123456';? ? 創(chuàng)建了一個可以通過192.168.100.2登陸的用戶
MariaDB [mysql]> create user qin@'192.168.100.0/255.255.255.0' identified by '123456';? ? 創(chuàng)建網(wǎng)段用戶
MariaDB [(none)]> show privileges;? ? 查看權(quán)限
MariaDB [(none)]>grant select on scott.* to qin@'localhost';? ? 將對scott數(shù)據(jù)庫下的所有表的查詢權(quán)限(select)授權(quán)給?qin@'localhost'用戶
MariaDB [(none)]> flush privileges;? ? 如果權(quán)限沒有生效,刷新權(quán)限表
再次使用qin登陸MariaDB 就可以看到scott數(shù)據(jù)庫了零酪,查詢表冒嫡,但是不能刪改
MariaDB [(none)]> show grants;? ? 查看自己的權(quán)限
MariaDB [(none)]> show grants forqqin@'localhost';? ? 查看qin@'localhost'的權(quán)限
MariaDB [(none)]> revoke select on scott.* from qin@'localhost';? ? 收回qin@'localhost對?scott.*的select權(quán)限
MariaDB [(none)]> grant all on *.* to qinbing@'localhost' identified by '123456'? ? 創(chuàng)建用戶qinbing@'localhost'并且授權(quán)全部(all)權(quán)限對所有數(shù)據(jù)庫(?*.* );
MariaDB [(none)]> grant all on *.* to qinbing@'%' identified by '123456';? ? 增加遠程登陸
退出MariaDB
mysqladmin -u root -p password 'redhat';? ? 修改登陸密碼為redhat
MariaDB [mysql]> update user set password='123456' where user='root' and host='localhost';? ? 在數(shù)據(jù)庫里改密碼四苇,將root(localhost)的密碼改為123456? ? 但是發(fā)現(xiàn)密碼是明文的
update user set password=password('123456') where user='root' and host='localhost';? ? 增加password('123456')變密文
MariaDB [mysql]> set password=password('redhat');? ? 直接更改當(dāng)前用戶密碼為redhat
MariaDB [mysql]> set password for qin@'localhost'=password('redhat');? ? 對別的用戶改密碼
///////////////破解MariaDB密碼
systemctl stop mariadb.service? ? ?先關(guān)了MariaDB
vim /etc/my.cnf? ? 修改配置文件
systemctl restart mariadb.service? ? ?啟動數(shù)據(jù)庫
mysql -u root -p? ? 直接登陸孝凌,不用輸入密碼回車就進入數(shù)據(jù)庫了
MariaDB [(none)]> use mysql? ? 進入mysql?
MariaDB [mysql]> select host,user,password from user;
MariaDB [mysql]> update user set password=password('123456') where user='root';? ? 把密碼改了就好了
然后退出去把配置文件里的skip-grant-tables給刪了
systemctl restart mariadb.service? ? ?重啟數(shù)據(jù)庫
///////////////破解MariaDB密碼 2
systemctl stop mariadb.service ????先關(guān)了MariaDB
mysqld_safe --skip-grant-tables? ? 輸入這個跳過權(quán)限,然后打開另一個窗口
直接mysql就進入數(shù)據(jù)庫了月腋,然后就是同上操作改掉密碼就好了
systemctl restart mariadb.service? ? ?最后重啟數(shù)據(jù)庫
////////////////////
mysql_secure_installation????提高安全性安全安裝(用于生產(chǎn)環(huán)境設(shè)置)
systemctl restart mariadb.service? ? ?重啟數(shù)據(jù)庫
vim /etc/my.cnf? ? 修改配置文件
重啟機器蟀架,systemctl reboot