一、MySQL 5.7 主要特性:
原生支援 Systemd
更好的性能:對于多核 CPU、固态硬碟、鎖有着更好的優化更好的 InnoDB 存儲引擎
更為健壯的複制功能:複制帶來了資料完全不丢失的方案,傳統金融客戶也可以選擇使用 MySQL 資料庫。
注:mysql-5.6.3 已經支援了多線程的主從複制
新增 sys 庫:以後這會是 DBA 通路最頻繁的庫
二、安裝 mysql5.7.18
1、系統環境:centos7.2 x86_64
# uname -r
3.10.0-327.el7.x86_64
# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
因為 centos7.2 預設安裝了 mariadb-libs,是以先要解除安裝掉
檢視是否安裝 mariadb,有就解除安裝
# rpm -qa |grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
# rpm -e --nodeps mariadb-libs
# rpm -qa |grep mariadb
2、安裝依賴包
注:相關依賴包的作用
cmake:由于從 MySQL5.5 版本開始棄用了正常的 configure 編譯方法,是以需要 CMake 編譯器,用于設定 mysql 的編譯參數。如:安裝目錄、資料存放目錄、字元編碼、排序規則等。 Boost #從 MySQL 5.7.5 開始 Boost 庫是必需的,mysql 源碼中用到了 C++的 Boost 庫,要求必須安裝 boost1.59.0 或以上版本
GCC 是 Linux 下的 C 語言編譯工具,mysql 源碼編譯完全由 C 和 C++編寫,要求必須安裝
GCC
bison:Linux 下 C/C++文法分析器
ncurses:字元終端處理庫
軟體連結:
安裝 cmake,注意進行./bootstrap是報錯了,說的是缺少gcc的包
是以需要先安裝gcc
# yum -y install gcc gcc-c++ m4
# tar -xvf cmake-3.5.2.tar.gz
# cd cmake-3.5.2/
# ./bootstrap
# gmake && gmake install
cmake –version ---檢視 cmake 版本
# cmake -version
cmake version 3.5.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
安裝 ncurses
# tar -xvf ncurses-5.9.tar.gz
# cd ncurses-5.9/
# ./configure && make && make install
安裝 bison
# tar -xvf bison-3.0.4.tar.gz
# cd bison-3.0.4/
# ./configure && make && make install
安裝 bootst
# tar -xvf boost_1_59_0.tar.gz
# mv boost_1_59_0 /usr/local/boost
3)建立 mysql 使用者和使用者組及目錄
# groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql ---建立 msyql 組和 msyql 使用者禁止登入 shell
# mkdir /data/soft/mysql ---建立目錄
# mkdir /data/soft/mysql/mysqldb ---資料庫目錄
3、編譯安裝 mysql
解壓 mysql 源碼包:
# tar -xvf mysql-5.7.18.tar.gz
# cd mysql-5.7.18/
# cmake -DCMAKE_INSTALL_PREFIX=/data/soft/mysql -DMYSQL_DATADIR=/data/soft/mysql/mysqldb -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/data/soft/mysql/mysql.sock -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_c -DWITH-SYSTEMD=1 -DWITH_BOOST=/usr/local/boost
# make && make install
注 1:配置解釋:
-DCMAKE_INSTALL_PREFIX=/data/soft/mysql [MySQL 安裝的根目錄]
-DMYSQL_DATADIR=/data/soft/mysql/mysqldb [MySQL 資料庫檔案存放目錄]
-DSYSCONFDIR=/etc [MySQL 配置檔案所在目錄]
-DWITH_MYISAM_STORAGE_ENGINE=1 [添加 MYISAM 引擎支援]
-DWITH_INNOBASE_STORAGE_ENGINE=1 [添加 InnoDB 引擎支援]
-DWITH_ARCHIVE_STORAGE_ENGINE=1 [添加 ARCHIVE 引擎支援 ]
-DMYSQL_UNIX_ADDR=/usr/local/mysql /mysql.sock [指定 mysql.sock 位置 ]
-DWITH_PARTITION_STORAGE_ENGINE=1 [安裝支援資料庫分區 ]
-DEXTRA_CHARSETS=all [使 MySQL 支援所有的擴充字元]
-DDEFAULT_CHARSET=utf8 [設定 MySQL 的預設字元集為utf8]
-DDEFAULT_COLLATION=utf8_general_ci [設定預設字元集校對規則]
-DWITH-SYSTEMD=1 [可以使用 systemd 控制 mysql 服務]
-DWITH_BOOST=/usr/local/boost [指向 boost 庫所在目錄]
2:為了加快編譯速度可以按下面的方式編譯安裝
make -j $(grep processor /proc/cpuinfo | wc –l) && make install
-j 參數表示根據 CPU 核數指定編譯時的線程數,可以加快編譯速度。預設為 1 個線程編譯。 如果在測試時就不要用這個了,比較耗資源,估計兩下系統就會顯示make錯誤,我自己做了幾次,都是這樣提示,是以測試就老老實實make就行,别搞花裡胡哨的,在生産環境可以這樣用,因為伺服器配置比較好。
注 3:若要重新運作 cmake 配置,需要删除 CMakeCache.txt 檔案
# make clean
#rm -f CMakeCache.txt
優化 Mysql 的執行路徑
# vim /etc/profile
編輯變量路徑内容
export PATH=$PATH:/data/soft/mysql/bin
# source /etc/profile
4、設定權限并初始化 MySQL 系統授權表
# cd /data/soft/mysql/
# chown -R mysql:mysql .
# bin/mysqld --initialize --user=mysql --basedir=/data/soft/mysql --datadir=/data/soft/mysql/mysqldb
2018-05-10T05:20:41.713754Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-05-10T05:20:43.602801Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-05-10T05:20:43.738995Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-05-10T05:20:43.807591Z 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: e337ff8a-5411-11e8-b889-000c29560c16.
2018-05-10T05:20:43.814513Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-05-10T05:20:43.819425Z 1 [Note] A temporary password is generated for [email protected]: _ikVWLuiy0qJ
注意:如果使用–initialize 參數初始化系統資料庫之後,會生成 root 使用者的一個臨時密碼"_ikVWLuiy0qJ",如上所示。
注 1:以 root 初始化操作時要加--user=mysql 參數,生成一個随機密碼(注意儲存登入時用)
注 2:MySQL 5.7.6 之前的版本執行這個腳本初始化系統資料庫
/data/soft/mysql/bin/mysql_install_db --user=mysql --basedir=/data/soft/mysql --datadir=/data/soft/mysql/mysqldb
# 5.7.6 之後版本初始系統資料庫腳本(本文使用此方式初始化)
#/data/soft/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/data/soft/mysql --datadir=/data/soft/mysql/mysqldb
# chown -R mysql:mysql . ---改所有者,注意是 root .
5、建立配置檔案
由于在5.7.18開始,二進制包不再包含示例檔案my-default.cnf,是以我從5.7.17版本中提取了樣例,但是發現裡面也沒有太多項配置,my-default.cnf内容如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
自己寫的
# vim /etc/my.cnf
[mysqld]
basedir=/data/soft/mysql
datadir=/data/soft/mysql/mysqldb
pid-file=/data/soft/mysql/mysqldb/mysqld.pid
socket=/data/soft/mysql/mysqld.sock
log_error=/data/soft/mysql/mysqldb/mysqld.err
重新載入 systemd,掃描新的或有變動的單元
# systemctl daemon-reload
6、配置 mysql 自動啟動
# cp /data/soft/mysql/support-files/mysql.server /etc/init.d/mysqld
# chkconfig mysqld on
# systemctl start mysqld
# ps -ef |grep mysqld
root 65697 1 0 14:42 ? 00:00:00 /bin/sh /data/soft/mysql/bin/mysqld_safe --datadir=/data/soft/mysql/mysqldb --pid-file=/data/soft/mysql/mysqldb/mysqld.pid
mysql 65852 65697 6 14:42 ? 00:00:00 /data/soft/mysql/bin/mysqld --basedir=/data/soft/mysql --datadir=/data/soft/mysql/mysqldb --plugin-dir=/data/soft/mysql/lib/plugin --user=mysql --log-error=/data/soft/mysql/mysqldb/mysqld.err --pid-file=/data/soft/mysql/mysqldb/mysqld.pid --socket=/data/soft/mysql/mysql.sock
root 65883 62018 0 14:42 pts/0 00:00:00 grep --color=auto mysqld
注意:在 mysql.server啟動過程中如果出錯,那麼可能是pid的問題,把預設的 pid 檔案指定到了/var/run/mysqld/目錄,而并沒有事先建立該目錄,是以要手動建立該目錄并把權限賦給 mysql 使用者。
# mkdir /var/run/mysqld
# chown -R mysql:mysql /var/run/mysqld/
服務啟動成功
通路 MySQL 資料庫并修改密碼
# mysql -uroot -p_ikVWLuiy0qJ
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 8
Server version: 5.7.18
Copyright (c) 2000, 2017, 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 = password('abc.123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
使用新密碼登陸
# mysql -uroot -pabc.123
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 9
Server version: 5.7.18 Source distribution
Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql>
未完待續................................