1. 程式人生 > >mysql5.7安裝的實踐過程與錯誤排查參考

mysql5.7安裝的實踐過程與錯誤排查參考

dem 情況下 rod 添加 first row sco assume export

了解LAMP和安裝mysql服務

LAMP=Linux+Apache+Mysql+PHP四種組成的一個系統
Apache和php必須在一臺服務器上才可使用,mysql可以通過網絡方式來提供服務
Apache不能直接存儲數據到mysql,必須通過php Module的解析模塊來往數據庫中存儲數據

安裝mysql服務

安裝mysql的先前操作

創建mysql運行的用戶和組
groupadd mysql
useradd -M -s /sbin/nologin -g mysql mysql

在官網或鏡像站下載mysql的編譯包

安裝前首先安裝運行編譯的環境
yum install gcc cpp glibc glibc-devel gcc-c++ ncurses-devel

下載cmake編譯包進行編譯安裝,附件中為3.12.0版本;官網地址:www.cmake.org
./configure;make;make install 安裝cmake編譯,所有安裝路徑默認即可

編譯mysql報錯boost不存在

mysql 5.7版本後需要安裝一個boost包,並把包放在編譯指定的目錄下/usr/local/boost/

CMake Error at cmake/boost.cmake:81 (MESSAGE):
  You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

  This CMake script will look for boost in <directory>.  If it is not there,
  it will download and unpack it (in that directory) for you.

  If you are inside a firewall, you may need to use an http proxy:

  export http_proxy=http://example.com:80

Call Stack (most recent call first):
  cmake/boost.cmake:238 (COULD_NOT_FIND_BOOST)
  CMakeLists.txt:455 (INCLUDE)

-- Configuring incomplete, errors occurred!

報錯信息大概如上;

報錯信息解決方案

先到官網下載的是boost_1_64_0.tar.gz,然後把壓縮包放到了/usr/local/boost目錄下,再次cmake但是報錯繼續。經過百度一番後,說不需要解壓縮只需要把壓縮包放到指定目錄就可以了
把boost_1_64_0.tar.gz放到目錄後,再次cmake後發現識別不到,他會再次下載另外一個版本,結果下載超時。
然後boost官網下載了需要的版本boost_1_59_0.tar.gz

boost的官網歷史版本下載地址:http://www.boost.org/users/history/

然後把下載好的tar包放到/usr/local/boost目錄下然後繼續cmake就可以了

對mysql的編譯參數參考和編譯結果:

[root@localhost mysql-5.7.22]# cmake . -DCMALE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/var/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH-SYSTEMD=1 -DWITH_BOOST=/usr/local/boost

-----------------------省略過程----------------------
-- CMAKE_C_LINK_FLAGS: 
-- CMAKE_CXX_LINK_FLAGS: 
-- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/mysql-5.7.22
[root@localhost mysql-5.7.22]# echo $?
0

如果結果跟我一樣無報錯的話,繼續make編譯,如有報錯請看報錯信息,大部分是沒有安裝某些軟件包造成的,請將錯誤信息粘貼至搜索引擎查詢


[root@localhost mysql-5.7.22]# make
Scanning dependencies of target abi_check
[ 0%] Built target abi_check
Scanning dependencies of target INFO_SRC
[ 0%] Built target INFO_SRC
Scanning dependencies of target INFO_BIN
[ 0%] Built target INFO_BIN
Scanning dependencies of target zlib
----------------省略過程------------------------
[ 99%] Building C object libmysqld/examples/CMakeFiles/mysql_client_test_embedded.dir/__/__/testclients/mysql_client_test.c.o
[ 99%] Linking CXX executable mysql_client_test_embedded
[ 99%] Built target mysql_client_test_embedded
Scanning dependencies of target my_safe_process
[100%] Building CXX object mysql-test/lib/My/SafeProcess/CMakeFiles/my_safe_process.dir/safe_process.cc.o
[100%] Linking CXX executable my_safe_process
[100%] Built target my_safe_process
[root@localhost mysql-5.7.22]# aecho $?
0

安裝無報錯,完成最後安裝make install,編譯結果成功

-----------------------過程省略---------------------
-- Installing: /usr/local/mysql/support-files/mysqld_multi.server
-- Installing: /usr/local/mysql/support-files/mysql-log-rotate
-- Installing: /usr/local/mysql/support-files/magic
-- Installing: /usr/local/mysql/share/aclocal/mysql.m4
-- Installing: /usr/local/mysql/support-files/mysql.server
[root@localhost mysql-5.7.22]# echo $?
0
基本的安裝完成,接下來就是配置mysql的啟動和配置項

遞歸指定mysql所屬用戶和所屬組的權限

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

數據庫的初始化

編譯好的數據庫進行初始化,其中mysql初始化信息中包含一個隨機生成的密碼

[root@localhost mysql-5.7.22]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-07-28T09:08:42.088874Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-07-28T09:08:42.590615Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-07-28T09:08:42.861362Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-07-28T09:08:42.955937Z 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: d342a57c-9245-11e8-bf5e-080027a7f9c2.
2018-07-28T09:08:42.965634Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2018-07-28T09:08:42.966858Z 1 [Note] A temporary password is generated for root@localhost: tHuQg=%M!12j  --這裏是初始生成的密碼

my.cnf和/etc/init.d/mysqld文件

拷貝服務啟動文件,並修改my.cnf配置文件。指定mysql安裝路徑和存儲數據的路徑及添加系統服務

[root@localhost ]# cp /usr/local/src/mysql-5.7.22/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ]# vim /etc/init.d/mysqld 
[root@localhost ]# chmod +x /etc/init.d/mysqld 
[root@localhost ]# ls -l /etc/init.d/mysqld 
-rwxr-xr-x 1 root root 10609 7月 28 17:21 /etc/init.d/mysqld
[root@localhost ]# chkconfig --add mysqld
[root@localhost ]# chkconfig --list 

註意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      如果您想列出 systemd 服務,請執行 ‘systemctl list-unit-files‘。
      欲查看對特定 target 啟用的服務請執行
      ‘systemctl list-dependencies [target]‘。

mysqld 0:關  1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關  1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關

編輯my.cnf的mysql配置文件

[root@localhost ]# less /etc/my.cnf

[mysqld]
basedir = /usr/local/mysql/  
datadir = /usr/local/mysql/data  
port = 3306  
character-set-server = utf8  
explicit_defaults_for_timestamp = true  
# socket = /var/run/mysqld/mysqld.sock 
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

mysql的啟動過程

添加完成跟隨系統啟動後,啟動mysql服務:

[root@localhost ]# /etc/init.d/mysqld start
Starting MySQL.2018-07-28T09:23:23.822700Z mysqld_safe error: log-error set to ‘/var/log/mariadb/mariadb.log‘, however file don‘t exists. Create writable for user ‘mysql‘.
 ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.pid).

根據報錯提示創建了mysql的日誌文件,然後再嘗試啟動

[root@localhost ]# mkdir -p /var/log/mariadb/mariadb
[root@localhost ]# touch /var/log/mariadb/mariadb.log
[root@localhost ]# chown -R mysql:mysql /var/log/mariadb/mariadb

再次嘗試啟動mysql服務,並啟動成功

[root@localhost support-files]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 
[root@localhost support-files]# ps -aux |grep mysql
root 21812 0.0 0.1 113312 1636 pts/0 S 17:28 0:00 /bin/sh /usr/local/mysql//bin/mysqld_safe --datadir=/usr/local/mysq/data --pid-file=/usr/local/mysql/data/localhost.pid
mysql 22000 1.7 16.3 1112372 166604 pts/0 Sl 17:28 0: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=/var/log/mariadb/mariadb.log --pid-file=/usr/local/mysql/data/localhost.pid --port=3306
root 22030 0.0 0.0 112724 976 pts/0 R+ 17:28 0:00 grep --color=auto mysql

登入mysql,修改默認生成的密碼

mysql在初始化的時候生成了一個隨機高強度的密碼,這個密碼非我們自己設定的,接下來就是把默認密碼修改成一個符合規定的密碼,這裏修改密碼為pwd@123
mysql5.7和5.5及5.6不同,成功啟動服務時就會攜帶一個隨機密碼,必須通過其他方式修改默認的密碼,以方便以後來使用

[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -u root -h localhost passwrod ‘NOWpassword‘ -p
Enter password: 
mysqladmin: connect to server at ‘localhost‘ failed
error: ‘Your password has expired. To log in you must change it using a client that supports expired passwords.‘

這種修改密碼提示密碼過期,需要在一個客戶端上進行密碼修改

以下是mysql密碼修改的方法:

[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.22

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 ‘pwd@123‘;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

官方對mysql5.7默認密碼的解釋

為了提高安全性 mysql5.7中user表的password字段已被取消,取而代之的事 authentication_string 字段,當然我們更改用戶密碼也不可以用原來的修改user表來實現了
網絡上對mysql5.7密碼修改的方法,適用於mysql5.7

方法一:
mysql> alter user ‘root‘@‘localhost‘ identified by ‘pwd@123‘;
方法二:
mysql> set password for ‘root‘@‘localhost‘=password(‘pwd@123‘);
修改完成後刷新數據庫表內容
mysql> flush privileges;

mysql在沒有啟動文件的情況下啟動服務

[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my,cnf --user=mysql --detadir=/usr/local/mysql/data/mysql &
[1] 22873
[root@localhost mysql]# Logging to ‘/usr/local/mysql/data/localhost.err‘.
2018-07-28T10:06:55.233375Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
2018-07-28T10:06:55.242274Z mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.pid ended

[1]+ 完成 /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my,cnf --user=mysql --detadir=/usr/local/mysql/data/mysql
[root@localhost mysql]# ps -aux |grep mysql

mysql5.7安裝的實踐過程與錯誤排查參考