MySQL基礎知識總結(一)

 

數據(data):描述事物的符號記錄,種類,文字,圖像,聲音。是數據庫中存儲的基本對象。

特點:數據與其語義是不可分的。

數據模型:數據的組織方式,並可以用某種數據結構來描述。層次數據模型,網狀數據模型,關係數據模型,對象-關係模型,非關係模型。

數據庫(DataBase,DB)是一個有結構的,集成的,可共享的,統一管理的數據集合。。以關係模型組織起來的數據庫稱爲關係數據庫。

數據庫管理系統(DBMS)是一種用來管理數據庫的商品化軟件。所有訪問數據庫的請求都是由DBMS來完成的。DBMS提供數據定義(數據定義語言Data Definition Language DDL),數據操縱(數據操縱語言Data Manipulation Language DML),安全控制(數據控制語言Data Control Language DCL)和併發控制,數據備份與恢復功能。

Managing storage           存儲管理

Maintaining security         安全管理

Maintaining metadata        數據管理

Managing transactions        事務管理

Supporting connectivity       支持連接性

Optimizing performance       性能優化

Providing back-up and recovery mechanisms          備份和恢復

Processing requests for data retrieval and modification

一個完整的數據庫系統由數據庫(DB),數據庫管理系統(DBMS),應用程序,用戶和硬件組成。

關係模型:

關係:可以理解爲一張滿足某些約束條件的二維表。

元組:關係中的一行稱爲一個元組,存放的是客觀世界的一個實例。

屬性:關係中的一列稱爲屬性。一個屬性必須有唯一的屬性名。

域:  關係中一個屬性取值範圍。

關係性質:

關係中每個屬性的每一個值都是單值的;

關係中同一屬性的所有屬性值都具有相同的數據類型;

關係中的屬性名不能重複;

關係中屬性位置從左到右出現的順序無關緊要;

關係中任意兩個元組不能完全相同;

關係中的元組從上到下出現的順序無關緊要。

關係數據庫:相互關聯的表或者關係的集合。

候選鍵:由一個屬性名或者多個屬性名組成。表中任意一行在候選鍵屬性上的值是唯一的;不存在更小的真子集(更小的屬性或者屬性組),其不同的取值也能用來區分不同的行。

主鍵:如果一個表中有多個候選鍵,就從其中選擇一個作爲區分行的唯一性標識符,這個鍵便稱爲主鍵(Primary Key,PK)。唯一標識當前表中記錄的一個字段或者多個字段的集合。

外鍵:若表R1中的一個列或者列祖對應另一個表R2的主鍵,那麼該列或者列祖稱爲表R1的外鍵。

實體完整性:表中的任意一行的主鍵值不能爲空值並且是唯一的。

參照完整性:一個表的外鍵值可以爲空值。若不爲空值,則每一個外鍵值必須等於另一個表中主鍵的某個值。

SQL :結構化查詢語言

常見的關係型數據庫:

大型數據庫Oracle ,Sybase,Informix(IBM收購),DB2(IBM),EnterpriseDB(PostgreSQL企業版)

SQL Server,Access

MySQL (全球最流行的關係型數據庫)

PostgreSQL

memoryDB(內存數據庫):Altibase,TimesTen

NoSQL(非關係型數據庫)

MySQL 關係型數據庫: 管理數據儲存的工具,特點:多線程,多任務。

開源數據庫,遵循GUN規範。

版本:Community社區版 Enterprise企業版

特性:速度(Speed) 可靠性(Reliability) 可擴展性(Scalability)

      易用(Ease of  Use)   

      可移植性和兼容性 (Portability  and Standards  Compliance)

      支持多用戶(Multiuser  Support)

      國際化 (Internationalization)

      支持廣泛的應用程序(Wide Application Support)

      開源 (Open-Source Code)

相關組件:

MySQL Server 核心組件,提供MySQL服務

MySQL Cluster

MySQL Proxy 代理服務器組件,提供重寫查詢,修改結果集,實現查詢,流量分析,報表

             查詢,負載均衡等功能

MySQL Administrator   圖形化界面管理MySQL

MySQL Query Browser  查詢瀏覽器

MySQL Workbench     設計數據庫的平臺

MySQL Migration Toolkit  將其他數據庫中的數據遷移到MySQL中,實現數據遷移的功能

MySQL Embedded Server  嵌入式MySQL Server

MySQL Drivers  and Connectors  驅動和各種語言的連接器

MySQL架構

Memory and Connection Management   內存和連接管理

Query Parsing and Execution 查詢分析和執行:詞法分析和查詢優化

Data Storage   數據存儲 :靠存儲引擎來實現

Connectivity 連接器:客戶端服務器端不在同一臺主機,基於TCP/IP 網絡;客戶端和服務                                          器端在同一臺主機,基於Unix Socket(Linux/Unix)或管道(windows)實現。

Standards Compliance  兼容

Transaction     事務

Query Caching   查詢緩存

Extensiblity     可擴展性

Symmetric  Multiprocessing  Support   多對稱

Security      安全性

Application  Programming Interface  提供編程接口

總體架構:

wps_clip_image-3517

安裝方式:rpm(red hat,mysql)

          binary format(綠色安裝)

          source code (編譯安裝)

安裝目錄:

wps_clip_image-6139

主配置文件(按次序來尋找)

1  /etc/my.cnf  集中式配置文件,爲多個命令提供配置

2  /etc/mysql/my.cnf

3  $MYSQL_HOME/my.cnf

4  /path/to/file when defaults-extra-file=/path/to/file is specified

5   ~/.my.cnf

PS:如果發現多個配置文件, MySQL會將所有的配置文件組合起來,將他們的合合集作  

     爲配置文件來使用 。如果配置出現衝突,則以讀取最後一個配置文件中的配置爲準。          

PS:如果啓動服務的時候想不使用默認的數據存放目錄,需要在初始化的時候使用datadir指令來定義。

PS:讓mysql服務啓動的時候讀取額外的配置文件:

mysqld_safe --defaults-extra-file=/etc/mysql/my_instance.cnf。

再來回顧下綠色安裝方式:

[root@server27 ~]# tar -zxvf mysql-5.1.50-linux-i686-glibc23.tar.gz -C /usr/local/

[root@server27 ~]# cd /usr/local/

[root@server27 local]# ln -sv mysql-5.1.50-linux-i686-glibc23 mysql

[root@server27 local]# cd mysql

[root@server27 mysql]# groupadd mysql

[root@server27 mysql]# useradd -g mysql mysql

[root@server27 mysql]# chown -R mysql:mysql .

PS:最好將MySQL數據放在LVM上,可以利用LVM的快照功能實現MySQL備份。

[root@server27 mysql]# mkdir /data

[root@server27 mysql]# chown mysql:mysql /data -R

[root@server27 mysql]# ./scripts/mysql_install_db --datadir=/data --user=mysql

[root@server27 mysql]# chown -R root .

[root@server27 mysql]# cd support-files/

[root@server27 support-files]# cp my-large.cnf /etc/my.cnf

[root@server27 support-files]# vim /etc/my.cnf

datadir = /data      //** line 41

啓動MySQL:

[root@server27 mysql]# bin/mysqld_safe --user=mysql &

wps_clip_image-26699

提供MySQL啓動腳本:

[root@server27 mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[root@server27 mysql]# chkconfig --add mysqld

添加環境變量:

[root@server27 mysql]# vim /etc/profile

PATH=$PATH:/usr/local/mysql/bin   //** line 45

[root@server27 mysql]# . /etc/profile

導出頭文件:

[root@server27 mysql]# ln -sv /usr/local/include  /usr/include/mysql

create symbolic link `/usr/include/mysql' to `/usr/local/include'

導出庫文件:

[root@server27 mysql]# vim /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib

重讀庫文件:

[root@server27 mysql]# ldconfig -v

導出man目錄:

[root@server27 mysql]# vim /etc/man.config

MANPATH /usr/local/mysql/man   //** line 48

或者將/usr/local/mysql/man 目錄連接到/usr/man 下。

PS:對於MySQL用戶而言, 識別用戶賬號的用戶名既包括本身,又包括來源主機的IP地址,即:username@hostname,password

系統默認提供了三個用戶賬號和兩個匿名賬號:[email protected], root@localhost, root@hostname ,兩個匿名賬號:‘ ’@localhost and ‘ ‘@hostname。

mysql> use mysql;

mysql> show tables;

mysql> DESC user;

mysql> SELECT user,host,password from user;

+------+-------------------------------+----------+

| user | host                  | password |

+------+-------------------------------+-----------+

| root | localhost              |         |

| root | localhost.localdomain  |         |

| root | 127.0.0.1              |         |

|    | localhost              |         |

|    | localhost.localdomain  |         |

+------+-------------------------------+------------+

5 rows in set (0.00 sec)

修改用戶密碼:

[root@localhost ~]# mysqladmin -uroot -p password '123456';

Enter password:

mysqladmin -u root -h this_host_name password ’new-password’

重新登錄:

[root@localhost ~]# mysql -uroot -p

Enter password:

如果使用管理員賬號能夠登錄進去,則:

mysql> SET PASSWORD FOR ’root’@’host_name’ = PASSWORD(’new_password’);

或者

mysql> UPDATE mysql.user SET Password = PASSWORD(’new_password’)

WHERE User = ’root’;

mysql> FLUSH PRIVILEGES;

mysql> set PASSWORD FOR 'root'@'127.0.0.1'=PASSWORD('123456');

mysql> UPDATE mysql.user SET password=PASSWORD('redhat') WHERE user='root' and host='localhost.localdomain';

重讀授權表:

mysql> FLUSH PRIVILEDGES;

wps_clip_image-305

刪除一個用戶:

mysql> DROP USER 'root'@'localhost.localdomain';

MySQL字符的意義:

wps_clip_image-21550

Leave the mysql interactive shell (command only, exit, quit or \q)

退出MySQL使用\q,exit或者quit。

Specify the use feature to change databases within mysql, the short form is \u

語句在非完整的情況下要取消執行使用\c。

mysql keeps an in-memory list of the commands typed in the session

Some shell interpreter keys retain their function on the mysql command line, For example:

Ctrl-w   deletes the previous word and stores it in a buffer.

Ctrl-y  retrieves (yanks) the contents of buffers filled with Ctrl-u or Ctrl-w

Ctrl-u  deletes the entire line prior to the cursor position and stores it in a buffer

Ctrl-a  moves the cursor to the beginning of the current line

Ctrl-e  moves the cursor to the end of the current line

命令歷史保存在~/.mysql_history文件中。

?         (\?) Synonym for `help'.

clear     (\c) Clear the current input statement.

connect   (\r) Reconnect to the server. Optional arguments are db and host.

delimiter  (\d) Set statement delimiter.

edit      (\e) Edit command with $EDITOR.

ego       (\G) Send command to mysql server, display result vertically.

exit      (\q) Exit mysql. Same as quit.

go        (\g) Send command to mysql server.

help      (\h) Display this help.

nopager   (\n) Disable pager, print to stdout.

notee     (\t) Don't write into outfile.

pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.

print      (\p) Print current command.

prompt    (\R) Change your mysql prompt.

quit      (\q) Quit mysql.

rehash    (\#) Rebuild completion hash.

source    (\.) Execute an SQL script file. Takes a file name as an argument.

status    (\s) Get status information from the server.

system    (\!) Execute a system shell command.

tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.

use       (\u) Use another database. Takes database name as argument.

charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte

charsets.

warnings  (\W) Show warnings after every statement.

nowarning (\w) Don't show warnings after every statement.

顯示服務器端的幫助信息:HELP keyword。

wps_clip_image-4245

啓動的時候不讀取用戶表中的信息(繞過密碼):

/usr/local/mysql/bin/mysqld --user=mysql --skip-grant-tables &

進入之後修改密碼:

mysql > UPDATE user SET password=PASSWORD('123456') where user='root' and host='localhost';

mysql > FLUSH PRIVILEGES;

mysqladmin相關參數:

wps_clip_image-20473

wps_clip_image-13813

wps_clip_image-20920

[root@server27 mysql]# mysqladmin -uroot -p create testdb

[root@server27 mysql]# mysql -e 'show databases;'

wps_clip_image-9917

[root@server27 mysql]# mysqladmin -uroot -p extended-status

wps_clip_image-14962

相關GUI工具:

SQLyog           Windows上基於圖形界面連接遠程MySQL進行數據庫管理的工具

MySQL Front      數據庫管理工具

phpMyAdmin      數據庫管理工具

MySQL Query Browser   查詢瀏覽器,發送查詢請求到服務器端並取回執行結果

MySQL Administrator  數據庫管理工具

MySQL Workbench     數據庫設計工具

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章