什麼是數據庫
存儲數據用文件就可以了,爲什麼還要弄個數據庫?
文件保存數據有以下幾個缺點:
文件的安全性問題
文件不利於數據查詢和管理
文件不利於存儲海量數據
文件在程序中控制不方便
數據庫存儲介質:
磁盤 內存
爲了解決上述問題,專家們設計出更加利於管理數據的東西——數據庫,它能更有效的管理數據。
數據庫的水平是衡量一個程序員水平的重要指標。
主流數據庫
SQL Sever: 微軟的產品,.Net程序員的最愛,中大型項目。
Oracle: 甲骨文產品,適合大型項目,複雜的業務邏輯,併發一般來說不如MySQL。
MySQL:世界上最受歡迎的數據庫,屬於甲骨文,併發性好,不適合做複雜的業務。主要用在電商,SNS,
論壇。對簡單的SQL處理效果好。
PostgreSQL :加州大學伯克利分校計算機系開發的關係型數據庫,不管是私用,商用,還是學術研究使用,可
以免費使用,修改和分發。
SQLite: 是一款輕型的數據庫,是遵守ACID的關係型數據庫管理系統,它包含在一個相對小的C庫中。它的
設計目標是嵌入式的,而且目前已經在很多嵌入式產品中使用了它,它佔用資源非常的低,在嵌入式設備
中,可能只需要幾百K的內存就夠了。
H2: 是一個用Java開發的嵌入式數據庫,它本身只是一個類庫,可以直接嵌入到應用項目中。
MySQL安裝
基於centos 7的安裝方法
進入系統並切換到root
安裝
安裝 mariadb 服務
yum install -y mariadb-server
安裝 mariadb 命令行客戶端
yum install -y mariadb
安裝 mariadb C library
yum install -y mariadb-libs
安裝 mariadb 開發包
yum install -y mariadb-devel
更改配置
注意:在對相關文件修改時可能會遇到權限問題,使用chmod 777 文件名修改權限在修改文件內容,修改完成後在用chmod恢復到原來的權限
更改 /etc/my.cnf.d/client.cnf 文件
[client] 下加一行配置 default-character-set=utf8
最終內容
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#
[client]
default-character-set = utf8
# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
更改 /etc/my.cnf.d/mysql-clients.cnf 文件
[mysql] 下加一行配置 default-character-set=utf8
最終內容
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
[mysql]
default-character-set = utf8
[mysql_upgrade]
[mysqladmin]
[mysqlbinlog]
[mysqlcheck]
[mysqldump]
[mysqlimport]
[mysqlshow]
[mysqlslap]
更改 /etc/my.cnf.d/server.cnf 配置
[mysqld] 下加配置
collation-server = utf8_general_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
sql-mode = TRADITIONAL
最終內容
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8
sql-mode = TRADITIONAL
# this is only for embedded server
[embedded]
# This group is only read by MariaDB-5.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mysqld-5.5]
# These two groups are only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
[mariadb-5.5]
啓動
啓動服務
systemctl start mariadb
設置服務開啓自啓動
systemctl enable mariadb
查看服務狀態
systemctl status mariadb
可能的輸出爲,注意到 Active 狀態爲 active (running)
測試連接
使用命令行客戶端嘗試連接
mysql -uroot
可能的輸出爲
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
目前我們的 mariadb 用戶是沒有密碼的,是非常不安全的,需要執行以下步驟
mysql_secure_installation
選擇 Y/N 的時候都選 Y,
在
New password:
Re-enter new password:
時設置你自己的 root 密碼
使用命令行客戶端嘗試連接(帶密碼)
mysql -uroot -p
進入後的界面顯示
數據庫安裝結束