數(shù)據(jù)庫(kù)

MySQL安裝

MySQL是一種開(kāi)放源碼的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(RDBMS),MySQL數(shù)據(jù)庫(kù)系統(tǒng)使用最常用的數(shù)據(jù)庫(kù)管理語(yǔ)言——結(jié)構(gòu)化查詢(xún)語(yǔ)言(SQL)進(jìn)行數(shù)據(jù)庫(kù)管理。在WEB應(yīng)用方面MySQL是最好的RDBMS(Relation Database Management System ,關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng))應(yīng)用軟件之一。

前提條件

  • 安裝MySQL服務(wù)端、客戶(hù)端
  • 客戶(hù)端連接服務(wù)端
  • 客戶(hù)端發(fā)送命令給服務(wù)端MySQL服務(wù)的接受命令并執(zhí)行相應(yīng)操作
  • 下載安裝包“mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz”
# 安裝依賴(lài)yum -y install perl perl-devel autoconf libaio   把下載的安裝包移動(dòng)到/usr/local/下。
  • 解壓
tar zxvf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz
  • 復(fù)制解壓后的mysql目錄到系統(tǒng)的本地軟件目錄
cp mysql-5.6.33-linux-glibc2.5-x86_64 /usr/local/mysql -r
  • 添加系統(tǒng)mysql組和mysql用戶(hù)
groupadd mysqluseradd -r -g mysql -s /bin/false mysql
注意:Because the user is required only for ownership purposes, not login purposes, the useradd command uses the -r and -s /bin/false options to create a user that does not have login permissions to your server host. Omit these options if your useradd does not support them.
  • 進(jìn)入安裝mysql軟件目錄,修改目錄擁有者為mysql用戶(hù)
cd mysql/chown -R mysql:mysql ./
  • 安裝數(shù)據(jù)庫(kù),此處可能出現(xiàn)錯(cuò)誤。
./scripts/mysql_install_db --user=mysql

FATAL ERROR: please install the following Perl modules before executing scripts/mysql_install_db:Data::Dumper  #解決方法:yum install -y perl-Data-Dumper
  • 修改當(dāng)前目錄擁有者為root用戶(hù)
    
chown -R root:root ./
  • 修改當(dāng)前data目錄擁有者為mysql用戶(hù)
chown -R mysql:mysql data

—————————————————————————————————————— 到此數(shù)據(jù)庫(kù)安裝完畢

  • mysql服務(wù)開(kāi)機(jī)自啟動(dòng)

    添加開(kāi)機(jī)啟動(dòng),把啟動(dòng)腳本放到開(kāi)機(jī)初始化目錄。cp support-files/mysql.server /etc/init.d/mysql# 賦予可執(zhí)行權(quán)限chmod +x /etc/init.d/mysql# 添加服務(wù)chkconfig --add mysql # 顯示服務(wù)列表chkconfig --list 
    
![image](http://upload-images.jianshu.io/upload_images/13008160-43c7185aa482dffb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
如果看到mysql的服務(wù),并且3,4,5都是on的話(huà)則成功,如果是off,則執(zhí)行 chkconfig --level 345 mysql on
  • 啟動(dòng)mysql服務(wù)
#創(chuàng)建缺少的文件夾mkdir /var/log/mariadbservice mysql start正常提示信息:Starting MySQL. SUCCESS!
  • 把mysql客戶(hù)端放到默認(rèn)路徑
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

附:建議使用軟鏈過(guò)去,不要直接包文件復(fù)制,便于系統(tǒng)安裝多個(gè)版本的mysql

通過(guò)使用 mysql -uroot -p 連接數(shù)據(jù)庫(kù)(默認(rèn)數(shù)據(jù)庫(kù)的root用戶(hù)沒(méi)有密碼,這個(gè)需要設(shè)置一個(gè)密碼)。

錯(cuò)誤信息:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 解決方法:打開(kāi)/etc/my.cnf,看看里面配置的socket位置是什么目錄?!皊ocket=/var/lib/mysql/mysql.sock”路徑和“/tmp/mysql.sock”不一致。建立一個(gè)軟連接:ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

到這里任務(wù)算是完成了。之后就可以創(chuàng)建數(shù)據(jù)庫(kù)用戶(hù),然后使用數(shù)據(jù)庫(kù)了。

image

MySQL操作

連接數(shù)據(jù)庫(kù)

mysql  -u user -p                   例:mysql -u root -p
  • 常見(jiàn)錯(cuò)誤
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2), it means that the MySQL server daemon (Unix) or service (Windows) is not running.
  • 退出連接
QUIT 或者 Ctrl+D

查看、創(chuàng)建

默認(rèn)數(shù)據(jù)庫(kù):             mysql - 用戶(hù)權(quán)限相關(guān)數(shù)據(jù)             test - 用于用戶(hù)測(cè)試數(shù)據(jù)             information_schema - MySQL本身架構(gòu)相關(guān)數(shù)據(jù) 創(chuàng)建數(shù)據(jù)庫(kù):                    create database db1 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;     # utf8編碼
               create database db1 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; # gbk編碼
使用數(shù)據(jù)庫(kù):     use db1;
  • 顯示當(dāng)前使用的數(shù)據(jù)庫(kù)中所有表:SHOW TABLES;

用戶(hù)管理

創(chuàng)建用戶(hù)    create user '用戶(hù)名'@'IP地址' identified by '密碼';刪除用戶(hù)    drop user '用戶(hù)名'@'IP地址';修改用戶(hù)    rename user '用戶(hù)名'@'IP地址'; to '新用戶(hù)名'@'IP地址';;修改密碼    set password for '用戶(hù)名'@'IP地址' = Password('新密碼')

注:用戶(hù)權(quán)限相關(guān)數(shù)據(jù)保存在mysql數(shù)據(jù)庫(kù)的user表中,所以也可以直接對(duì)其進(jìn)行操作(不建議)

權(quán)限管理

mysql對(duì)于權(quán)限的限制:

  • 數(shù)據(jù)庫(kù)及內(nèi)部其他權(quán)限
            數(shù)據(jù)庫(kù)名.*           數(shù)據(jù)庫(kù)中的所有            數(shù)據(jù)庫(kù)名.表          指定數(shù)據(jù)庫(kù)中的某張表            數(shù)據(jù)庫(kù)名.存儲(chǔ)過(guò)程     指定數(shù)據(jù)庫(kù)中的存儲(chǔ)過(guò)程            *.*                所有數(shù)據(jù)庫(kù)
  • 用戶(hù)和IP的權(quán)限
            用戶(hù)名@IP地址         用戶(hù)只能在改IP下才能訪(fǎng)問(wèn)            用戶(hù)名@192.168.1.%   用戶(hù)只能在改IP段下才能訪(fǎng)問(wèn)(通配符%表示任意)            用戶(hù)名@%             用戶(hù)可以再任意IP下訪(fǎng)問(wèn)(默認(rèn)IP地址為%)
  • 查看權(quán)限
show grants for '用戶(hù)'@'IP地址' 
  • 授權(quán)
grant  權(quán)限 on 數(shù)據(jù)庫(kù).表 to   '用戶(hù)'@'IP地址'
  • 取消授權(quán)
revoke 權(quán)限 on 數(shù)據(jù)庫(kù).表 from '用戶(hù)'@'IP地址'

授權(quán)實(shí)例: grant all privileges on db1.tb1 TO '用戶(hù)名'@'IP' grant select on db1.* TO '用戶(hù)名'@'IP' grant select,insert on *.* TO '用戶(hù)名'@'IP' revoke select on db1.tb1 from '用戶(hù)名'@'IP'

mysql表操作

  • 查看表
show tables;                    # 查看數(shù)據(jù)庫(kù)全部表 select * from 表名;             # 查看表所有內(nèi)容
  • 創(chuàng)建表
create table 表名(    列名  類(lèi)型  是否可以為空,    列名  類(lèi)型  是否可以為空)ENGINE=InnoDB DEFAULT CHARSET=utf8
實(shí)例 CREATE TABLE `tab1` (  `nid` int(11) NOT NULL auto_increment,                   # not null表示不能為空,auto_increment表示自增  `name` varchar(255) DEFAULT zhangyanlin,                 # default 表示默認(rèn)值  `email` varchar(255),  PRIMARY KEY (`nid`)                                      # 把nid列設(shè)置成主鍵) ENGINE=InnoDB DEFAULT CHARSET=utf8;

注:

  1. 默認(rèn)值,創(chuàng)建列時(shí)可以指定默認(rèn)值,當(dāng)插入數(shù)據(jù)時(shí)如果未主動(dòng)設(shè)置,則自動(dòng)添加默認(rèn)值
  2. 自增,如果為某列設(shè)置自增列,插入數(shù)據(jù)時(shí)無(wú)需設(shè)置此列,默認(rèn)將自增(表中只能有一個(gè)自增列)注意:1、對(duì)于自增列,必須是索引(含主鍵)2、對(duì)于自增可以設(shè)置步長(zhǎng)和起始值
  3. 主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個(gè)列,則它的值必須唯一,如果是多列,則其組合必須唯一。
  • 刪除表
drop table 表名
  • 清空表內(nèi)容
delete from 表名truncate table 表名
  • 修改表

    添加列:   alter table 表名 add 列名 類(lèi)型刪除列:   alter table 表名 drop column 列名修改列:          alter table 表名 modify column 列名 類(lèi)型;  -- 類(lèi)型          alter table 表名 change 原列名 新列名 類(lèi)型; -- 列名,類(lèi)型  添加主鍵:          alter table 表名 add primary key(列名);刪除主鍵:          alter table 表名 drop primary key;          alter table 表名  modify  列名 int, drop primary key;  添加外鍵: alter table 從表 add constraint 外鍵名稱(chēng)(形如:FK_從表_主表) foreign key 從表(外鍵字段) references 主表(主鍵字段);刪除外鍵: alter table 表名 drop foreign key 外鍵名稱(chēng)  修改默認(rèn)值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;刪除默認(rèn)值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
    
推薦使用Navicat Premium。
  • 基本數(shù)據(jù)類(lèi)型

MySQL的數(shù)據(jù)類(lèi)型大致分為:數(shù)值、時(shí)間和字符串

mysql表內(nèi)容操作

1、增 insert into 表 (列名,列名...) values (值,值,...)insert into 表 (列名,列名...) values (值,值,...),(值,值,值...)insert into 表 (列名,列名...) select (列名,列名...) from 表例:    insert into tab1(name,email) values('zhangyanlin','zhangyanlin8851@163.com')2、刪 delete from 表                                      # 刪除表里全部數(shù)據(jù)delete from 表 where id=1 and name='zhangyanlin'   # 刪除ID =1 和name='zhangyanlin' 那一行數(shù)據(jù)3、改 update 表 set name = 'zhangyanlin' where id>14、查 select * from 表select * from 表 where id > 1select nid,name,gender as gg from 表 where id > 1a、條件判斷where     select * from 表 where id > 1 and name != 'aylin' and num = 12;    select * from 表 where id between 5 and 16;    select * from 表 where id in (11,22,33)    select * from 表 where id not in (11,22,33)    select * from 表 where id in (select nid from 表)b、通配符like     select * from 表 where name like 'zhang%'  # zhang開(kāi)頭的所有(多個(gè)字符串)    select * from 表 where name like 'zhang_'  # zhang開(kāi)頭的所有(一個(gè)字符)c、限制limit     select * from 表 limit 5;            - 前5行    select * from 表 limit 4,5;          - 從第4行開(kāi)始的5行    select * from 表 limit 5 offset 4    - 從第4行開(kāi)始的5行d、排序asc,desc     select * from 表 order by 列 asc              - 根據(jù) “列” 從小到大排列    select * from 表 order by 列 desc             - 根據(jù) “列” 從大到小排列    select * from 表 order by 列1 desc,列2 asc    - 根據(jù) “列1” 從大到小排列,如果相同則按列2從小到大排序 e、分組group by     select num from 表 group by num    select num,nid from 表 group by num,nid    select num,nid from 表  where nid > 10 group by num,nid order nid desc    select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid    select num from 表 group by num having max(id) > 10     特別的:group by 必須在where之后,order by之前
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容