1. 程式人生 > >MySQL介紹及安裝(一)

MySQL介紹及安裝(一)

unit cal time 連接 rac efault mysql用戶 速度 welcome

一、關系型數據庫和非關系型數據庫

  1.1:關系型數據庫

    關系型數據庫是把復雜的數據結構歸結為簡單的二元關系(即二維表格的形式),在關系型數據庫中,對數據的操作幾乎全部建立在一個或多個關系表格上的,通過對這些關聯的表格分類、合並、連接或者選取等運算來實現數據的管理。我們常用的MySQL和oracle、MariaDB、SQL server都是關系型數據庫。

  1.2:非關系型數據庫

    非關系型數據庫被稱為"NoSQL",NoSQL的產生並不是要徹底的否定關系型數據庫,而是作為傳統關系型數據庫的一個有效的補充,NoSQL數據庫在特定的場景下可以發揮很難想象的高效率和高性能,NoSQL典型的產品memcached(純內存)、redis(持久化緩存)、mongodb、Hbase等等。

  1.3:非關系型數據庫的種類

    ① 鍵值對(key-value)存儲數據庫(memcached、redis)

    ② 列存儲(Column-oriented)數據庫(Cassandra、HBase)

    ③ 面向文檔(Document-Oriented)數據庫(mongodb)

    ④ 圖形(Graph)數據庫(我也不知道)

二、MySQL介紹

  MySQL是一種關系型數據庫管理系統,關系型數據庫的特點是將數據保存在不同的表中,在將這些表放入不同的數據庫中,而不是將所有的數據統一放在一個大的倉庫,這樣的設計增加了MySQL的讀取速度,靈活性可管理性也得到了大的提高,訪問MySQL數據庫最常用的標準語言是SQL結構化查詢語。

  選擇MySQL數據庫的幾個原因

  ① MySQL性能卓越,服務穩定,很少宕機

  ② MySQL開發源代碼無版權約束,自主性及成本低

  ③ 社區用戶活躍

  ④ MySQL體積小,安裝使用簡單,利於維護

  ⑤ MySQL口碑好,流行企業架構LAMP LNMP

  ⑥ MySQL支持多操作系統,提供多種API,支持多語言開發

三、如何選擇MySQL數據庫版本

   ① 穩定版:選擇開源社區辦的穩定版GA版本

  ② 產品線:可以選擇5.5或者5.7 互聯網現在主流5.5 其次是5.1和5.6或者5.7 最好不要選擇5.5之前的,因為這是個大坑

  ③ 選擇MySQL數據庫GA版本發布6月以後的

  ④ 要考慮開發人員開發的程序是否兼容

  ⑤ 還有就是根據實際情況而定,活靈活現

四、多種安裝方式及什麽時候使用什麽方式

  PS:所有的方式我們用的MySQL的版本是MySQL5.7的版本

4.1:yum安裝的方式(適合對數據庫要求不太高的場合,例如並發不大、公司內部、zabbix、自己的博客)

    ① 下載MySQL5.7的源

[root@rsync131 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

    ② 安裝yum倉庫

[root@rsync131 ~]# yum localinstall -y mysql57-community-release-el7-7.noarch.rpm

    ③ 安裝MySQL5.7

[root@rsync131 ~]# yum install -y mysql-community-server

    ④ 啟動MySQL服務

[root@rsync131 ~]# systemctl start mysqld.service

    ⑤ 查看隨機生成的密碼

[root@rsync131 ~]# grep ‘temporary password‘ /var/log/mysqld.log
2018-09-08T10:59:31.829660Z 1 [Note] A temporary password is generated for root@localhost: dKG,g/3l7*uI    # 這個就是隨機的密碼

    ⑥ 修改密碼

[root@rsync131 ~]# mysql -uroot -pdKG,g/3l7*uI
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘MyNewPass4!‘;
Query OK, 0 rows affected (0.00 sec)                # 第一種方式

mysql> set password for ‘root‘@‘localhost‘=password(‘MyNewPass4!‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)     # 第二種方式

mysql>flush privileges;

    ⑦ 修改密碼策略

    使用初始隨機密碼登錄後MySQL會強制要求修改密碼,否則無法正常使用,(密碼必須包含小寫、大寫字母及特殊字符),這個策略是可以修改的

# 查看策略相關信息
mysql> show variable‘%password%‘
    -> ;
+---------------------------------------+--------+
| Variable_name                         | Value  |
+---------------------------------------+--------+
| default_password_lifetime             | 0      |
| disconnect_on_expired_password        | ON     |
| log_builtin_as_identified_by_password | OFF    |
| mysql_native_password_proxy_users     | OFF    |
| old_passwords                         | 0      |
| report_password                       |        |
| sha256_password_proxy_users           | OFF    |
| validate_password_check_user_name     | OFF    |
| validate_password_dictionary_file     |        |
| validate_password_length              | 8      |
| validate_password_mixed_case_count    | 1      |
| validate_password_number_count        | 1      |
| validate_password_policy              | MEDIUM |
| validate_password_special_char_count  | 1      |
+---------------------------------------+--------+
14 rows in set (0.00 sec)

# 修改方法
在/etc/my.cnf文件添加validate_password_policy配置,指定密碼策略
# 選擇0(LOW),1(MEDIUM),2(STRONG)其中一種,選擇2需要提供密碼字典文件
validate_password_policy=0
如果不需要密碼策略,添加my.cnf文件中添加如下配置禁用即可:
validate_password = off

重新啟動mysql服務使配置生效:
systemctl restart mysqld

    ⑧ 遠程連接授權

mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘MyNewPass4!‘ WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

    ⑨ 設置防火墻

開通端口(默認3306):

firewall-cmd --add-port=3306/tcp

/sbin/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

    ⑩ 文件默認路徑

默認配置文件路徑:

配置文件:/etc/my.cnf 
日誌文件:/var/log//var/log/mysqld.log 
服務啟動腳本:/usr/lib/systemd/system/mysqld.service 
socket文件:/var/run/mysqld/mysqld.pid

4.2:編譯安裝的方式(在真正的場景下用的比較多的最好的方式)

    ① 安裝所需要的包及下載MySQL源碼包

[root@rsync131 ~]# yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
[root@rsync131 ~]# wget http://www.mysql.com/Downloads/MySQL-5.7/mysql-5.7.23.tar.gz

    ② 下載和解壓boost庫

[root@rsync131 ~]# wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

[root@rsync131 ~]# tar xf boost_1_59_0.tar.gz -C /usr/local/
[root@rsync131 ~]# cd /usr/local/
[root@rsync131 local]# mv boost_1_59_0 boost

    ③ 創建運行用戶

useradd -M -s /sbin/nologin mysql  # 創建用戶mysql,不創建家目錄,不允許登陸系統

    ④ 解壓、配置、安裝

[root@rsync131 ~]# tar xf mysql-5.7.23.tar.gz -C /opt/
[root@rsync131 ~]# cd /opt/mysql-5.7.23/
[root@rsync131 ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=/usr/local/boost -DWITH_SYSTEMD=1                                             # 配置
[root@rsync131 ~]# make && make install            # 安裝



# ==================================cmake解釋=====================================

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \                         	# 指定mysql數據庫安裝目錄
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \               		# 連接文件位置
-DSYSCONFDIR=/etc \                                                 # 指定配置文件目錄
-DSYSTEMD_PID_DIR=/usr/local/mysql \                                # 進程文件目錄
-DDEFAULT_CHARSET=utf8  \                                           # 指定默認使用的字符集編碼
-DDEFAULT_COLLATION=utf8_general_ci \                               # 指定默認使用的字符集校對規則
-DWITH_INNOBASE_STORAGE_ENGINE=1 \                         			# 存儲引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \                            		# 存儲引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \                     			# 存儲引擎
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \                 				# 存儲引擎
-DMYSQL_DATADIR=/usr/local/mysql/data \                             # 數據庫文件
-DWITH_BOOST=/usr/local/boost \                                     # 指定Boost庫的位置,mysql5.7必須添加該參數
-DWITH_SYSTEMD=1                                                    # 使系統支持MySQL數據庫

    ⑤ 修改數據庫目錄權限

[root@rsync131 mysql-5.7.23]# chown -R mysql:mysql /usr/local/mysql/

    ⑥ 設置配置文件

mysql 5.7 版本和以前的有所不同,如果配置文件不做修改,則服務啟動失敗

[root@rsync131 mysql-5.7.23]# vim /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

[root@rsync131 mysql-5.7.23]# chown mysql:mysql /etc/my.cnf   //修改配置文件的權限

    ⑦ 設置環境變量及初始化數據庫

echo ‘PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH‘ >> /etc/profile
echo ‘export PATH‘ >> /etc/profile
source /etc/profile   //使寫入生效


# 初始化數據庫
cd /usr/local/mysql/
bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data    



===================解釋================
cd /usr/local/mysql/
bin/mysqld --initialize-insecure \         # 生成初始化密碼(5.7版本才有),實際會生成空密碼
--user=mysql \                  # 指定管理用戶
--basedir=/usr/local/mysql \    # 指定工作目錄
--datadir=/usr/local/mysql/data # 指定數據文件目錄

    ⑧ 添加系統服務、開機自啟

cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload    //刷新識別mysqld.service服務
systemctl enable mysqld    //加入系統自啟動
systemctl start mysqld     //啟動服務
netstat -anpt | grep 3306  

    ⑨ 修改密碼及遠程授權

# 修改密碼
[root@rsync131 mysql]# mysqladmin -u root -p password "abc123"
Enter password: abc123

# 登錄
[root@rsync131 mysql]# mysql -u root -pabc23


# 設置遠程授權
grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘abc123‘ with grant option;
//第一個“*”代表所有數據庫,第二“*”代表所有表,賦予root權限 “%”代表所有服務器終端,可設為IP地址 密碼為“abc123”

    ⑩ 設置防火墻

開通端口(默認3306):

firewall-cmd --add-port=3306/tcp

/sbin/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

4.3:二進制安裝的方式(在真正的場景下用的比較多的第二好的方式)

    ① 下載二進制包

[root@rsync131 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz

    ② 卸載系統自帶的MariaDB

[root@rsync131 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@rsync131 ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
[root@rsync131 ~]# rpm -qa | grep mariadb
[root@rsync131 ~]#

    ③ 創建MySQL用戶

[root@rsync131 ~]# useradd -M -s /sbin/nologin mysql

    ④ 解壓安裝包,並且重命名

[root@rsync131 ~]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@rsync131 ~]# cd /usr/local/
[root@rsync131 local]# mv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql/

    ⑤ 將mysql添加為系統服務及設置開機自啟

[root@rsync131 local]# cp mysql/support-files/mysql.server /etc/init.d/mysql
[root@rsync131 local]# chkconfig --add mysql

    ⑥ 初始化數據庫

[root@rsync131 bin]# cd /usr/local/mysql/
[root@rsync131 mysql]# mkdir data -p     # 數據存放目錄

[root@rsync131 mmysql]# yum install -y libaio   # 必須安裝不然會報下面的錯誤
錯誤信息
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

# 初始化
[root@rsync131 mmysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-09-08T14:15:21.330311Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-08T14:15:21.488188Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-08T14:15:21.511408Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-08T14:15:21.565037Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9f0919fe-b371-11e8-92a6-000c292579e0.
2018-09-08T14:15:21.565691Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2018-09-08T14:15:21.566064Z 1 [Note] A temporary password is generated for root@localhost: sQ3LjJkq1Z(z    # 數據庫的初始化密碼

    ⑦ 啟動mysql服務,使用臨時密碼登錄mysql並修改登錄密碼

[root@rsync131 mysql]# systemctl start mysql
[root@rsync131 mysql]# ps -ef | grep mysql
root       1686      1  0 22:16 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/rsync131.pid
mysql      1771   1686  0 22:16 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=rsync131.err --pid-file=/usr/local/mysql/data/rsync131.pid
root       1823   1475  0 22:20 pts/0    00:00:00 grep --color=auto mysql

[root@rsync131 mysql]# ./bin/mysql -u root -p
Enter password: sQ3LjJkq1Z(z   # 初始化密碼
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> SET PASSWORD FOR ‘root‘@‘localhost‘ = PASSWORD(‘123456‘);   # 修改密碼
Query OK, 0 rows affected, 1 warning (0.00 sec)

    ⑧ 設置任何遠程主機都可以訪問數據庫

[root@rsync131 mysql]# ./bin/mysql -u root -p123456   # 修改後的密碼
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> grant all privileges on *.* to ‘root‘ @‘%‘ identified by ‘123456‘;  # 設置權限
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>  flush privileges;     # 刷新權限
Query OK, 0 rows affected (0.00 sec)

mysql>

    ⑨ 設置防火墻

開通端口(默認3306):

firewall-cmd --add-port=3306/tcp

/sbin/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

  

MySQL介紹及安裝(一)