一、資料庫的安裝方式及其安裝步驟
1、安裝方式
rpm包安裝,yum安裝,二進制包安裝,編譯安裝
2、安裝步驟 (本文是使用二進制包安裝)
1)下載下傳mariadb相應版本的資料庫,解壓
tar xf mariadb-5.5.48-linux-86_64.tar.gz -C /usr/local/
2)建立軟連結及其建立使用者和存放資料目錄和授權其目錄檔案
cd /usr/local
ln -sv mariasb-5.5.48 mysql
chown -R root.mysql mysql
groupadd -r mysql
useradd -r -g mysql mysql
mkdir /mydata/data -pv
chown -R mysql.mysql /mydata/data
3)給資料庫提供配置檔案及其啟動腳本
cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
cp /usr/local/mysql/support-files/my.server /etc/init.d/mysqld
chkconfig --add /etc/init.d/mysqld
chkconfig mysqld on
4)給/etc/my.cnf加上如下項
datadir=/mydata/data
skip-name-resolve=ON
innodb-file-per-table=ON
5)初始化資料庫及其啟動資料庫
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mydata/data
service mysqld start
6)執行mysql_secure_installation指令設定密碼及其删除匿名使用者等操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<code>[root@centos6 ~]</code><code># mysql_secure_installation</code>
<code>/usr/bin/mysql_secure_installation</code><code>: line 379: find_mysql_client: </code><code>command</code> <code>not found</code>
<code>NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB</code>
<code> </code><code>SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!</code>
<code>In order to log into MariaDB to secure it, we'll need the current</code>
<code>password </code><code>for</code> <code>the root user. If you've just installed MariaDB, and</code>
<code>you haven't </code><code>set</code> <code>the root password yet, the password will be blank,</code>
<code>so you should just press enter here.</code>
<code>Enter current password </code><code>for</code> <code>root (enter </code><code>for</code> <code>none):</code>
<code>OK, successfully used password, moving on...</code>
<code>Setting the root password ensures that nobody can log into the MariaDB</code>
<code>root user without the proper authorisation.</code>
<code>You already have a root password </code><code>set</code><code>, so you can safely answer </code><code>'n'</code><code>.</code>
<code>Change the root password? [Y</code><code>/n</code><code>] n</code>
<code> </code><code>... skipping.</code>
<code>By default, a MariaDB installation has an anonymous user, allowing anyone</code>
<code>to log into MariaDB without having to have a user account created </code><code>for</code>
<code>them. This is intended only </code><code>for</code> <code>testing, and to </code><code>make</code> <code>the installation</code>
<code>go a bit smoother. You should remove them before moving into a</code>
<code>production environment.</code>
<code>Remove anonymous </code><code>users</code><code>? [Y</code><code>/n</code><code>] y</code>
<code> </code><code>... Success!</code>
<code>Normally, root should only be allowed to connect from </code><code>'localhost'</code><code>. This</code>
<code>ensures that someone cannot guess at the root password from the network.</code>
<code>Disallow root login remotely? [Y</code><code>/n</code><code>] n</code>
<code>By default, MariaDB comes with a database named </code><code>'test'</code> <code>that anyone can</code>
<code>access. This is also intended only </code><code>for</code> <code>testing, and should be removed</code>
<code>before moving into a production environment.</code>
<code>Remove </code><code>test</code> <code>database and access to it? [Y</code><code>/n</code><code>] n</code>
<code>Reloading the privilege tables will ensure that all changes made so far</code>
<code>will take effect immediately.</code>
<code>Reload privilege tables now? [Y</code><code>/n</code><code>] y</code>
<code>Cleaning up...</code>
<code>All </code><code>done</code><code>! If you've completed all of the above steps, your MariaDB</code>
<code>installation should now be secure.</code>
<code>Thanks </code><code>for</code> <code>using MariaDB!</code>
二、MariaDB基礎
1、配置檔案查找次序
/etc/my.cnf/-->/etc/mysql/my.cnf-->~/.my.cnf
2、mariadb的指令行互動式客服端工具
mysql[options] [database]
常用選項:
-u:username,使用者名,預設為root
-h:host遠端主機或位址,預設為localhost
-p:password,使用者的密碼
-D:database_name,設定指定連接配接庫
-e:直接在指令行運作mysql資料庫中的指令
執行個體:mysql -uroot -hlocalhost -p mysql -utestuser -h10.1.%.% -p mysql -uroot -p -e 'show databases';
圖示:
<a href="http://s3.51cto.com/wyfs02/M02/88/83/wKiom1f6M8KCVSUdAABOyFt9PbE176.png" target="_blank"></a>
3、mariadb資料庫指令行客服端指令
常用幫助參數如下注釋
<code>[root@centos7 ~]</code><code># mysql -uroot -p</code>
<code>Enter password:</code>
<code>Welcome to the MariaDB monitor. Commands end with ; or \g.</code>
<code>Your MariaDB connection </code><code>id</code> <code>is 19</code>
<code>Server version: 5.5.44-MariaDB MariaDB Server</code>
<code>Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.</code>
<code>Type </code><code>'help;'</code> <code>or </code><code>'\h'</code> <code>for</code> <code>help. Type </code><code>'\c'</code> <code>to </code><code>clear</code> <code>the current input statement.</code>
<code>MariaDB [(none)]></code>
<code>MariaDB [(none)]> \? </code><code>#擷取幫助也可使用help指令</code>
<code>General information about MariaDB can be found at</code>
<code>http:</code><code>//mariadb</code><code>.org</code>
<code>List of all MySQL commands:</code>
<code>Note that all text commands must be first on line and end with </code><code>';'</code>
<code>? (\?) Synonym </code><code>for</code> <code>`help'. </code><code>#擷取幫助資訊</code>
<code>clear</code> <code>(\c) Clear the current input statement. </code><code>#清除目前行的輸入</code>
<code>connect (\r) Reconnect to the server. Optional arguments are db and host. </code><code>#重連資料庫</code>
<code>delimiter (\d) Set statement delimiter.</code>
<code>edit (\e) Edit </code><code>command</code> <code>with $EDITOR.</code>
<code>ego (\G) Send </code><code>command</code> <code>to mysql server, display result vertically. </code><code>#豎立顯示資訊</code>
<code>exit</code> <code>(\q) Exit mysql. Same as quit. </code><code>#退出目前的資料庫</code>
<code>go (\g) Send </code><code>command</code> <code>to mysql server. </code><code>#發送指令到資料庫</code>
<code>help (\h) Display this help.</code>
<code>nopager (\n) Disable pager, print to stdout.</code>
<code>notee (\t) Don't write into outfile.</code>
<code>pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.</code>
<code>print (\p) Print current </code><code>command</code><code>.</code>
<code>prompt (\R) Change your mysql prompt.</code>
<code>quit (\q) Quit mysql.</code>
<code>rehash (\</code><code>#) Rebuild completion hash.</code>
<code>source</code> <code>(\.) Execute an SQL script </code><code>file</code><code>. Takes a </code><code>file</code> <code>name as an argument.</code>
<code>status (\s) Get status information from the server.</code>
<code>system (\!) Execute a system shell </code><code>command</code><code>.</code>
<code>tee</code> <code>(\T) Set outfile [to_outfile]. Append everything into given outfile.</code>
<code>use (\u) Use another database. Takes database name as argument.</code>
<code>charset (\C) Switch to another charset. Might be needed </code><code>for</code> <code>processing binlog with multi-byte charsets.</code>
<code>warnings (\W) Show warnings after every statement.</code>
<code>nowarning (\w) Don't show warnings after every statement.</code>
<code>For server side help, </code><code>type</code> <code>'help contents'</code>
4、資料庫常用資料類型
1)字元型
char,varchar,binary,varbinary,txt,blob...
2)數值型
int,float,double,tinyint...
3)枚舉型
set,enum
4)時間日期型
date,time,datetime,timestamp,year
5)資料類型修飾符
unsigned,not null,default
三、服務端指令
1、資料定義語言(DDL),主要用于管理資料庫元件,例如索引,視圖,使用者,存儲過程,主要指令有:create,alter,drop。
對庫的操作:
建立:create
create {database|schema} [if not exists] db_name
修改:alter
alter {database|schema} [db_name]
删除:drop
drop {database|schema} [if exists] {db_name}
檢視支援的字元集:show character set
檢視支援的所有排序的規則:show collation;
<a href="http://s4.51cto.com/wyfs02/M00/88/83/wKiom1f6PTOChDL-AABWNXN9UA0968.png" target="_blank"></a>
對表的操作:
建立表:
create [temporary] table [if not exists] tbl_name
修改表:
alter [online|offline] [ignore] table tbl_name
字段:
添加:add [column] col_name column_definition
删除:drop [column] col_name
修改:change [column] old_col_name new_col_name column_definition
modify [column] col_name column_definition
鍵:
添加:ADD [CONSTRAINT [symbol]] PRIMARY KEY
add {primary key unique key foreign}(col1,col2...)
删除:
主鍵:drop primary key
外鍵:drop foreign key fk_name
索引:
添加:add {index|key} {index_name}
删除:drop {index|key} index_name
主要執行個體如下:
<code>create database testdb; </code>
<code>use testdb;</code>
<code>create table </code><code>if</code> <code>not exists students(</code><code>id</code> <code>int unsigned NOT NULL primary key, name char(20) NOT NULL, age tinyint unsigned, gender enum(</code><code>'f'</code><code>,</code><code>'m'</code><code>));</code>
<code>alter table students add class varchar(20) not null after age; </code>
<code>alter table students change gender sex enum(</code><code>'f'</code><code>,</code><code>'m'</code><code>);</code>
<code>alter table students modify class char(30) after sex; </code>
<code>alter table students drop primary key ;</code>
<code>alter table students add primary key(</code><code>id</code><code>,name);</code>
<code>alter table students add index class (class);</code>
<code>alter table students drop index class;</code>
圖示:
建立庫、表及其檢視表結構
添加和修改字段名字及其類型
<a href="http://s5.51cto.com/wyfs02/M00/88/84/wKiom1f6RgTznra9AABnLBLzdRw990.png" target="_blank"></a>
修改主鍵及其添加主鍵
<a href="http://s1.51cto.com/wyfs02/M02/88/81/wKioL1f6RvaAm4AtAABzvkE08u4908.png" target="_blank"></a>
檢視建表資訊及其表的狀态資訊
<a href="http://s1.51cto.com/wyfs02/M01/88/81/wKioL1f6SROw6bUeAABbsoVbSbI883.png" target="_blank"></a>
2、資料操縱語言(DML),主要用于管理表中的資料,實作資料的增删改查。
插入資料:insert into
insert into tbl_name [cols....] values (val1,val2,...)
執行個體:insert into students (id,name) value (1,'alren');
insert into students values (2,'alren',29,'m','one');
查詢資料: select
select [col1,col2,...] from tbl_name where clause;
執行個體:select * from students where id=1;
select name,age from students where age>11 and age<20;
select name,age from students where age between 11 and 20;
select name,age from students where name rlike 'en$';
select age,sex from students where age is not;
select age,sex from students where age is not null;
select id,name from students order by id desc;
select id,name,age from students order by id asc;
更新資料:update
update [low_priority] [ignore] table_reference
update students set age=age+10 where name like '%lren';
删除資料:delete from
delete from tbl_name where clause;
delete from students; #删除表中的所有資料,很危險,謹慎操縱。
delete from students order by age desc limit 20;
修改表為自增長
<a href="http://s1.51cto.com/wyfs02/M02/88/81/wKioL1f6UCSiUlchAAA1cYQ6aPY414.png" target="_blank"></a>
插入資料兩種方式
<a href="http://s2.51cto.com/wyfs02/M00/88/84/wKiom1f6UF3hwWfoAAAsTS0rFsE055.png" target="_blank"></a>
查詢表中資料
<a href="http://s3.51cto.com/wyfs02/M02/88/84/wKiom1f6UomDHJAIAAA5hye4Lo4226.png" target="_blank"></a>
更新資料
<a href="http://s1.51cto.com/wyfs02/M00/88/81/wKioL1f6U32A8y5hAAA5hye4Lo4766.png" target="_blank"></a>
删除資料
<a href="http://s4.51cto.com/wyfs02/M02/88/85/wKiom1f6VDexKF27AAA5hye4Lo4388.png" target="_blank"></a>
四、建立使用者及其授權管理
1、建立使用者賬号
create user 'username'@'host' [indentified by 'password']
執行個體:create user 'testuser'@'10.1.10.%.%' identified by 'pass';
2、删除使用者賬号
DROP USER user [, user] ...
drop user 'username'@'host'
删除和授權使用者
<a href="http://s2.51cto.com/wyfs02/M02/88/85/wKiom1f6WBLg-vTjAAByc9Rw_Ag292.png" target="_blank"></a>
測試是否授權成功
<a href="http://s5.51cto.com/wyfs02/M02/88/85/wKiom1f6WL7TNOk4AABtpTqRn4U205.png" target="_blank"></a>
遠端登入測試成功
<a href="http://s2.51cto.com/wyfs02/M00/88/85/wKiom1f6Wl6jIWZgAABeyXLiIWg686.png" target="_blank"></a>
3、授權管理
grant privileges_type on [object_type] db_name.tabl_name to 'username'@'host' identified by 'password'
privileges_type:all,create,alter,drop,delete,update,insert,select
庫表的對應有如下關系:db_name.tbl_name
*.*:所有庫的中的所有表
db_name.*:指定庫的所有表
db_name.tbl_name:指定庫的指定表
db_name.routine_name:指定庫上的存儲函數或過程
建立test使用者為其授權測試
<a href="http://s3.51cto.com/wyfs02/M00/88/86/wKiom1f67p3yI04iAABSVypRay0964.png" target="_blank"></a>
回收使用者權限
<a href="http://s2.51cto.com/wyfs02/M00/88/82/wKioL1f6743CsgFYAABIm5H5jok489.png" target="_blank"></a>
檢視目前使用者及其指定使用者授權資訊
<a href="http://s2.51cto.com/wyfs02/M00/88/86/wKiom1f68H6i0MO5AABK8A7UWVo560.png" target="_blank"></a>
授權一使用者隻給查詢和插入權限,則除此權限外其他權限均無
<a href="http://s3.51cto.com/wyfs02/M00/88/86/wKiom1f69FLyRgxVAABqZT-olmo029.png" target="_blank"></a>
本文轉自chengong1013 51CTO部落格,原文連結:http://blog.51cto.com/purify/1860056,如需轉載請自行聯系原作者