mariadb数据库基本使用
mariadb數(shù)據(jù)庫簡(jiǎn)介
自甲骨文公司收購(gòu)MySQL后,其在商業(yè)數(shù)據(jù)庫與開源數(shù)據(jù)庫領(lǐng)域市場(chǎng)的占有份額都躍居第一,這樣的格局引起了業(yè)內(nèi)很多的人士的擔(dān)憂,因?yàn)樯虡I(yè)數(shù)據(jù)庫的老大有可能將MySQL閉源。為了避免Oracle將MySQL閉源,而無開源的類MySQL數(shù)據(jù)庫可用,MySQL社區(qū)采用分支的方式來避開這個(gè)風(fēng)險(xiǎn)。MariaDB數(shù)據(jù)庫就這樣誕生了,MariaDB是一個(gè)向后兼容,可能在以后替代MySQL的數(shù)據(jù)庫產(chǎn)品,其官方地址為:https://mariadb.org/ 。mariadb和mysql幾乎是一樣的。首先,mariadb就是由mysql的創(chuàng)始人負(fù)責(zé)維護(hù)的。而mariadb就是mysql創(chuàng)始人女兒的名字。使用方法和mysql相同
mariadb與mysql的區(qū)別:
1.MariaDB不僅僅是Mysql的一個(gè)替代品,MariaDB包括的一些新特性使它優(yōu)于MySQL
2.MariaDB跟MySQL在絕大多數(shù)方面是兼容的,對(duì)于開發(fā)者來說,幾乎感覺不到任何不同。目前MariaDB是發(fā)展最快的MySQL分支版本,新版本發(fā)布速度已經(jīng)超過了Oracle官方的MySQL版本。
3.MariaDB 是一個(gè)采用Aria存儲(chǔ)引擎的MySQL分支版本, 這個(gè)項(xiàng)目的更多的代碼都改編于 MySQL 6.0
MariaDB雖然被視為MySQL數(shù)據(jù)庫的替代品,但它在擴(kuò)展功能、存儲(chǔ)引擎以及一些新的功能改進(jìn)方面都強(qiáng)過MySQL。而且從MySQL遷移到MariaDB也是非常簡(jiǎn)單的:
1、數(shù)據(jù)和表定義文件(.frm)是二進(jìn)制兼容的 2、所有客戶端API、協(xié)議和結(jié)構(gòu)都是完全一致的 3、所有文件名、二進(jìn)制、路徑、端口等都是一致的 4、所有的MySQL連接器,比如PHP、Perl、Python、Java、.NET、MyODBC、Ruby以及MySQL C connector等在MariaDB中都保持不變 5、mysql-client包在MariaDB服務(wù)器中也能夠正常運(yùn)行 6、共享的客戶端庫與MySQL也是二進(jìn)制兼容的安裝mariadb 數(shù)據(jù)庫
apt install mariadb-server mariadb-client #安裝mariadb 服務(wù)端程序 systemctl restart mariadb.service #啟動(dòng)程序 systemctl enable mariadb.service #設(shè)為開機(jī)自啟動(dòng) ss -nltp |grep 3306 #mysql 服務(wù)默認(rèn)的端口號(hào)是tcp 3306MariaDB 高級(jí)應(yīng)用介紹
(這里只是介紹沒有做對(duì)應(yīng)的實(shí)驗(yàn))
Mariadb 高級(jí)應(yīng)用范例如
?主從復(fù)制
?讀寫分離
?數(shù)據(jù)分片
?集群應(yīng)用
?…
數(shù)據(jù)庫分片指:
通過某種特定的條件,將我們存放在一個(gè)數(shù)據(jù)庫中的數(shù)據(jù)分散存放在不同的多個(gè)數(shù)據(jù)庫(主機(jī))中,這樣來達(dá)到分散單臺(tái)設(shè)備的負(fù)載,根據(jù)切片規(guī)則,可分為以下兩種切片模式
MariaDB 基礎(chǔ)應(yīng)用
Mariadb 數(shù)據(jù)庫連接方式及初始化
1、連接數(shù)據(jù)庫
Mysql 命令行工具是MySQL 官方提供的連接工具,用戶可以通過mysql 連接到mysqld 上進(jìn)行一系列的SQL 操作。
mysql 工具有兩種模式:交互模式和命令行模式。交互模式指令需要連接到mysql 服務(wù)器下達(dá),命令行模式通過特定(-e)參數(shù)讀取shell 命令行傳遞的指令到服務(wù)器。
命令格式:
mysql [OPTIONS] [database]
連接數(shù)據(jù)庫示例:
(1)使用root 賬號(hào)連接服務(wù)器,無密碼登錄
(2)使用root 賬號(hào)連接服務(wù)器,使用密碼登錄,-h 指定登陸主機(jī)的IP 或名字,注意未指定-h 選項(xiàng)時(shí),默認(rèn)登陸本機(jī)的數(shù)據(jù)庫
mysql -u root -p'passwd' -h <ip>一些常用的查詢命令示例:
select database(); #查看當(dāng)前進(jìn)入的數(shù)據(jù)庫 select user(); #查看當(dāng)前登錄用戶 show databases; #查看數(shù)據(jù)庫 use mysql #進(jìn)入mysql 數(shù)據(jù)庫 show tables; #查看此數(shù)據(jù)庫的表,test 沒表,可以看其他數(shù)據(jù)庫的 desc user; #查看表結(jié)果 show variables like 'innodb%'; #查看環(huán)境變量 有時(shí)命令輸錯(cuò)停在>界面出不來,一般是少些了'或者;補(bǔ)全即可退出,實(shí)在不行可以CTRL+D 在重新登錄2、重新初始化mysql
Mariadb 在安裝完畢后,就自動(dòng)初始化好了基礎(chǔ)的數(shù)據(jù)庫,目錄位于:/var/lib/mysql
Mysql 的配置文件:/etc/mariadb.cnf,為兼容mysql, 原來的配置文件my.cnf 是它的一個(gè)軟鏈接
我們也可以通過命令mysql_secure_installation,重新初始化mysql:
mysql_secure_installation Enter current password for root (enter for none): #輸入當(dāng)前root 的密碼,密碼為空直接回車 即可 Change the root password? [Y/n] #是否修改root 的密碼 Remove anonymous users? [Y/n] #是否刪除匿名用戶 Disallow root login remotely? [Y/n] #是否不允許root 遠(yuǎn)程登錄 Remove test database and access to it? [Y/n] #是否刪除測(cè)試數(shù)據(jù)庫test Reload privilege tables now? [Y/n] #是否重新加載授權(quán)信息3、重啟
systemctl restart mariadbMariadb 數(shù)據(jù)庫權(quán)限管理
1、權(quán)限設(shè)置中,登陸位置的配置說明
mariadb 的用戶登錄限制包括用戶名和登錄位置兩部分。早期版本%就可以代表所有的連接,后期版本localhost 表示本地登錄,%表示tcp/ip 的遠(yuǎn)程登錄的所有ip。%也可以寫具體主機(jī)或網(wǎng)段,比如192.168.200.10、192.168.100.%或者192.168.200.0/255.255.255.0,其他格式不識(shí)別。
如果登錄主機(jī)符合多個(gè)連接條件,默認(rèn)連接第一個(gè)身份,設(shè)置權(quán)限的時(shí)候需要注意
2、建立新用戶、設(shè)定密碼與登陸位置示例:
在mysql 的命令行狀態(tài)下:
select user(); #查看當(dāng)前登錄用戶 use mysql select host,user,password from user; #查看數(shù)據(jù)庫所有登錄范圍、用戶和密碼,4 個(gè)root 不是同一個(gè)用戶,可以單獨(dú)設(shè)置密碼 create user uos@'localhost' identified by '123456'; #建立可以本地登錄的用戶uos 并設(shè)置密碼123456 create user uos@'%' identified by '123456'; #建立可以遠(yuǎn)程登錄的用戶uos并設(shè)置密碼123456 create user test@'localhost' identified by '123456'; #建立可以本地登錄的用戶test并設(shè)置密碼123456 create user test@'192.168.200.10' identified by '123456'; #建立可以從192.168.200.10 主機(jī)登錄的用戶test 并設(shè)置密碼123456 grant all on *.* to uos@'localhost'; #授權(quán)本地登錄的用戶uos 對(duì)所有數(shù)據(jù)庫的所有表有所有權(quán)限 grant select on scott.* to test@'localhost'; #授權(quán)本地登錄的用戶test 可以讀取scott 數(shù)據(jù)庫的所有表 grant all on *.* to uostest@'%' identified by '123456'; #可以建立用戶、授權(quán)、設(shè)置密碼一起做 flush privileges; #刷新授權(quán)表 show privileges; #查看所有權(quán)限 show grants for uos@'localhost'; #查看用戶權(quán)限 show grants for test@'localhost'; revoke select on scott.* from test@'192.168.200.10'; #收回權(quán)限3、修改新密碼(需輸入root 原始密碼)
(1)使用mysqladmin 修改
mysqladmin -u root -p password '123456'(2)或進(jìn)入數(shù)據(jù)庫,在mysql 命令行中修改
mysql -uroot -p set password=password('uos'); #對(duì)當(dāng)前用戶設(shè)置密碼,立刻生效 set password for uos@'localhost'=password('123456'); #對(duì)任意用戶設(shè)置密碼 use mysql #使用mysql 這個(gè)內(nèi)置數(shù)據(jù)庫 update user set password=password('uos') where user='root' and host='localhost'; #修改密碼后需要flush privileges;刷新權(quán)限表,或重啟服務(wù)4、mariadb 忘記root 密碼怎么辦?
vim /etc/mysql/mariadb.conf.d/50-server.cnf[mysqld] skip-grant-tables #在[mysqld]下添加此字段,進(jìn)入但用戶模式重啟服務(wù)
systemctl restart mariadb.service輸入mysql 命令無需密碼直接進(jìn)入數(shù)據(jù)庫
use mysql update user set password=password('uos') where user='root';從5.5.7 版本中mysql 數(shù)據(jù)庫中就開始引入plugin 這項(xiàng)配置,用來進(jìn)行用戶密碼驗(yàn)證
update user set plugin='' where user='root';停止mariadb 服務(wù),刪除配置文件的skip-grant-tables 選項(xiàng),重啟mariadb 服務(wù),使用新密碼進(jìn)入
數(shù)據(jù)庫字符集修改
查看Linux 的字符集用locale 命令
注:客戶端的字符集要和數(shù)據(jù)庫的字符集一致,不一致有可能亂碼
進(jìn)入mysql 命令行,status 查看mariadb 屬性
status Server characterset:utf8mb4 Db characterset: utf8mb4 Client characterset: utf8mb4 Conn. characterset: utf8mb4 設(shè)置mariadb server 的默認(rèn)字符集:設(shè)置mariadb server 的默認(rèn)字符集:
vim /etc/mysql/mariadb.conf.d/50-server.cnf
character-set-server = utf8 #collation-server = utf8重啟mariadb
systemctl restart mariadb.service再驗(yàn)證字符集
mysql -uroot -p
查看數(shù)據(jù)庫的字符集
create database scott; drop database scott; #刪除數(shù)據(jù)庫 source /scott.sql #使用腳本重建數(shù)據(jù)庫 show create database scott; #在更改字符集之后建立,所以是字符集是utf8 show create table emp;更改scott 數(shù)據(jù)庫和表的字符集
alter database scott charset utf8; alter table emp charset utf8;數(shù)據(jù)庫DDL,DML 和DCL 語言操作
SQL 語言可以理解成向服務(wù)端提問的語言,SQL 語言按照功能可以分為幾種子語言:
- 數(shù)據(jù)定義語言(DDL):創(chuàng)建和管理數(shù)據(jù)庫,包括創(chuàng)建修改表,定義索引,管理約束條 件。DDL 操作對(duì)象為數(shù)據(jù)庫內(nèi)部的對(duì)象。
- 數(shù)據(jù)操縱語言(DML):查詢和更新數(shù)據(jù)庫中的數(shù)據(jù),用于添加,刪除,更新,查詢。 DML的操作對(duì)象是表的內(nèi)部數(shù)據(jù)。而不會(huì)涉及到表的定義,結(jié)構(gòu)的修改。
- 數(shù)據(jù)控制語言(DCL):對(duì)用戶的權(quán)限控制
「1」建立scott 數(shù)據(jù)庫快速建立腳本
SCOTT 是數(shù)據(jù)庫內(nèi)部的一個(gè)示例用戶,缺省口令為tiger,下面有表emp, dept 等,這些表和表間的關(guān)系演示了關(guān)系型數(shù)據(jù)庫的一些基本原理
vim /scott.sql
這里省略了表格。
「2」查數(shù)據(jù)
select * from emp; select ename,sal from emp; select ename,(sal+200)*3 as bonus from emp; #支持算數(shù)表達(dá)式加減乘數(shù)和括號(hào)等 select ename,sal,comm,sal+ifnull(comm,0) as income from emp; #空值參與算術(shù)運(yùn)算為空值,建議用ifnull 函數(shù)轉(zhuǎn)換 select distinct deptno from emp; #用distinct 去除重復(fù)結(jié)果 select ename,sal,deptno from emp where deptno=30; select * from emp where ename like '___TT'; #_代表任意單個(gè)字符 select * from emp where ename like '%LL%'; #%代表任意字符 select ename,sal,deptno from emp where deptno=30 and sal>2000; #與 select ename,sal,deptno from emp where deptno=30 or sal>2000; #或 select ename,sal,deptno from emp where not sal>2000; #非 select * from emp order by sal; #默認(rèn)升序,降序加desc select empno,ename,sal,deptno from emp order by deptno,sal desc; #多列排序 select concat(ename,'\'s sal is ',sal) from emp; #支持多種函數(shù),不一一舉了 select count(*) from emp; select deptno,sum(sal),min(sal),max(sal),avg(sal) from emp group by deptno; select deptno,avg(sal) from emp group by deptno having avg(sal)<2000; select ename,dname from emp,dept; #笛卡爾乘積 select ename,dname from emp,dept where emp.deptno=dept.deptno; select ename,sal,grade from emp join salgrade on sal between losal and hisal; select y.ename yuangong,j.ename jingli from emp y,emp j where y.mgr=j.empno; select ename,dname,sal,grade from emp,dept,salgrade where emp.deptno=dept.deptno and emp.sal between salgrade.losal and salgrade.hisal; select ename,sal from emp where sal=(select max(sal) from emp); #子查詢找到工資最高薪 select ename from emp where empno not in (select mgr from emp where mgr is not null); #in 結(jié)果集不能有空值,否則結(jié)果為空「3」創(chuàng)建表
創(chuàng)建數(shù)據(jù)庫
創(chuàng)建表并建立字段
格式:create table 數(shù)據(jù)表名(字段名字段類型);
「4」修改表
create table uos1 like uostable; #沒有數(shù)據(jù),只是復(fù)制了表的結(jié)構(gòu) insert into uos1 select * from uostable; #復(fù)制數(shù)據(jù),表不存在無法復(fù)制 create table uos2 as select * from uostable; #新建表,并復(fù)制整個(gè)表結(jié)構(gòu)+數(shù)據(jù)查看表結(jié)構(gòu)
格式:desc 數(shù)據(jù)表名;
向數(shù)據(jù)表插入數(shù)據(jù)
格式:insert into 數(shù)據(jù)表名(id,name,mail) values(1,‘uos1’,‘uos1@uos.com’);
更新表
update uostable set name='test' where id=2;修改表結(jié)構(gòu)
alter table uostable add newlist varchar(20); #增加newlist 列 alter table uostable drop newlist; #刪除newlist 列 alter table uostable add firstlist varchar(20) first; #增加firstlist 列到第一列 alter table uostable add afterid varchar(30) after id; #增加afterid 列到id 列后面「5」刪除表
delete from uostable where id=4; #要加上where 約束,沒有where 刪除整個(gè)表 delete from uos1; #DML 操作,清除表數(shù)據(jù),保留表結(jié)果 truncate uos2; #DDL 操作,清除表數(shù)據(jù),保留表結(jié)果,更徹底,降低高水位線 drop table uos1; #清除表結(jié)構(gòu)和數(shù)據(jù)「6」外部表導(dǎo)入導(dǎo)出
建立測(cè)試用外部表(即以逗號(hào)分隔各列的CSV 格式)
vim /uos.txt
將外部表導(dǎo)入數(shù)據(jù)庫
create database uosdatabase; use uosdatabase create table uostable(id int(4),name varchar(10),email varchar(20)); load data infile '/uos.txt' into table uostable fields terminated by ',' lines terminated by '\n';將數(shù)據(jù)庫導(dǎo)出為外部表
select * from uostable into outfile '/var/lib/mysql/uosdatabase/uostable.txt' fields terminated by ',' lines terminated by '\n';通過外部表導(dǎo)入scott 數(shù)據(jù)庫
準(zhǔn)備數(shù)據(jù):分別建立scott 數(shù)據(jù)庫的3 個(gè)CSV 表文件
vim /scott.emp.txt
vim /scott.dept.txt
10,'ACCOUNTING','NEW YORK' 20,'RESEARCH','DALLAS' 30,'SALES','CHICAGO' 40,'OPERATIONS','BOSTON'vim /scott.salgrade.txt
1,700,1200 2,1201,1400 3,1401,2000建立scott 庫和三個(gè)表,并導(dǎo)入數(shù)據(jù)
create database scott; use scott create table emp(empno int(4),ename varchar(10),job varchar(9),mgr int(4) default null,sal int(7),comm int(7) default null,deptno int(2)); load data infile '/scott.emp.txt' into table emp fields terminated by ',' lines terminated by '\n'; create table dept(deptno int(2),dname varchar(14),loc varchar(13)); load data infile '/scott.dept.txt' into table dept fields terminated by ',' lines terminated by '\n'; create table salgrade(grade int,losal int,hisal int); load data infile '/scott.salgrade.txt' into table salgrade fields terminated by ',' lines terminated by'\n';將scott 數(shù)據(jù)庫導(dǎo)出為外部表
use scott select * from emp into outfile '/var/lib/mysql/scott/scott.emp.txt' fields terminated by ',' lines terminated by '\n';數(shù)據(jù)庫管理程序mysqladmin 詳解
mysqladmin 是一個(gè)執(zhí)行mysqld 管理操作的客戶端程序。它可以用來檢查服務(wù)器的配置和當(dāng)前狀態(tài)、創(chuàng)建和刪除數(shù)據(jù)庫等。
因此使用mysqladmin 前就確保所連接的mysqld 服務(wù)端進(jìn)程正常運(yùn)行和連接
mysqladmin 工具的使用格式:
mysqladmin [option] command [command option] command ......option 選項(xiàng):
?-c number 自動(dòng)運(yùn)行次數(shù)統(tǒng)計(jì),必須和-i 一起使用 ?-i number 間隔多長(zhǎng)時(shí)間重復(fù)執(zhí)行 示例:每隔兩秒查看一次服務(wù)器的狀態(tài),總共重復(fù)5 次。 mysqladmin -uroot -p -i 2 -c 5 status ?-h, --host=name Connect to host. 連接的主機(jī)名或iP ?-p, --password[=name] 登錄密碼,如果不寫于參數(shù)后,則會(huì)提示輸入 ?-P, --port=# Port number to use for connection. 指定數(shù)據(jù)庫端口 ?-s, --silent Silently exit if one can't connect to server. ?-S, --socket=name Socket file to use for connection. 指定socket file ?-i, --sleep=# Execute commands again and again with a sleep between. 間隔一段時(shí)間 執(zhí)行一次 ?-u, --user=name User for login if not current user.登錄數(shù)據(jù)庫用戶名 ?-v, --verbose Write more information. 寫更多的信息 ?-V, --version Output version information and exit. 顯示版本mysqladmin 的相關(guān)命令示例:
mysqladmin password uos123 #<==設(shè)置密碼,前文用過的。 mysqladmin -uroot -puos123 password uos #<==修改密碼,前文用過的。 mysqladmin -uroot -puos123 status #<==查看狀態(tài),相當(dāng)于show status。 mysqladmin -uroot -puos123 -i 1 status #<==每秒查看一次狀態(tài)。 mysqladmin -uroot -puos123 extended-status #<==等同show global status;。 mysqladmin -uroot -puos123 flush-logs #<==切割日志。 mysqladmin -uroot -puos123 processlist #<==查看執(zhí)行的SQL 語句信息。 mysqladmin -uroot -puos123 processlist -i 1 #<==每秒查看一次執(zhí)行的SQL 語句。 mysqladmin -uroot -p'uos' shutdown #<==關(guān)閉mysql 服務(wù),前文用過的。 mysqladmin -uroot -p'uos' variables #<==相當(dāng)于show variables。Mariadb 數(shù)據(jù)庫備份與還原
在linux 環(huán)境備份數(shù)據(jù)庫
mysqldump -u root -p scott > /scott.dump #備份數(shù)據(jù)庫 mysqldump -u root -p scott emp > /scott.emp.dump #備份數(shù)據(jù)庫中的表 mysqldump -u root -p scott dept salgrade > /scott.dept+salgrade.dump #備份數(shù)據(jù)庫中的多個(gè)表在linux 環(huán)境還原數(shù)據(jù)庫
>drop database scott; #如果報(bào)錯(cuò),刪除/var/lib/mysql/scott/scott.emp.txt 文件 >create database uos; #不建立數(shù)據(jù)庫,無法導(dǎo)入整庫,隨意命名 mysql -u root -p uos < /scott.dump #還原數(shù)據(jù)庫 >drop database uos; >create database uos; #重建數(shù)據(jù)庫用于測(cè)試還原表 mysql -u root -p uos < /scott.emp.dump #還原數(shù)據(jù)庫中的表 mysql -u root -p uos < /scott.dept+salgrade.dump #還原數(shù)據(jù)庫中的多個(gè)表Mariadb 數(shù)據(jù)庫索引
什么是索引
索引是一種特殊的文件(InnoDB 數(shù)據(jù)表上的索引是表空間的一個(gè)組成部分),它們包含著對(duì)數(shù)
據(jù)表里所有記錄的引用指針。
更通俗的說,數(shù)據(jù)庫索引好比是一本書前面的目錄,在查找內(nèi)容之前可以先在目錄中查找索引位置,以此快速定位查詢數(shù)據(jù)。
對(duì)于索引,會(huì)保存在額外的文件中。
索引的類型
1、普通索引 2、唯一性索引 3、主鍵索引(主索引) 4、復(fù)合索引 5、全文索引創(chuàng)建普通索引
創(chuàng)建表時(shí)添加索引
語法:
以使用key 關(guān)鍵字,也可以使用index 關(guān)鍵字。索引名稱,可以加也可以不加,不加
使用字段名作為索引名。
示例:
MariaDB [book]> create table demo(id int(4),name varchar(20),pwd varchar(20),index(pwd)); Query OK, 0 rows affected (0.08 sec) MariaDB [book]> create table demo1(id int(4),name varchar(20),pwd varchar(20),key(pwd)); Query OK, 0 rows affected (0.01 sec) MariaDB [book]> create table demo2(id int(4),name varchar(20),pwd varchar(20),key index_pwd(pwd)); # 為索引加上名稱 Query OK, 0 rows affected (0.02 sec)總結(jié)
以上是生活随笔為你收集整理的mariadb数据库基本使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 存储设备的设备惟一标示符(DUID)
- 下一篇: 小芯片与大芯片技术