1. 程式人生 > >Centos7.4 安裝MySQL 5.7.21 (通用二進制包)

Centos7.4 安裝MySQL 5.7.21 (通用二進制包)

efi table eric 下載 logic ant ras modify 軟件

1.下載安裝包

MySQL 官方下載地址:https://dev.mysql.com/downloads/mysql/
MySQL 5.7官方安裝文檔:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

本文完全按照官方步驟配置安裝

選擇Linux - generic 64位安裝包
技術分享圖片
MySQL 5.7.21 二進制包下載地址:https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

wget --no-check-certificate https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

2.安裝依賴包

MySQL依賴於libaio 庫。如果這個庫沒有在本地安裝,數據目錄初始化和後續的服務器啟動步驟將會失敗。請使用適當的軟件包管理器進行安裝。例如,在基於Yum的系統上:

shell> yum search libaio  
shell> yum install libaio 

註意
SLES 11:從MySQL 5.7.19開始,Linux通用tar包的格式是EL6而不是EL5。以致於MySQL客戶端bin / mysql需要libtinfo.so.5。

解決方法是創建軟鏈接,例如64位系統上的ln -s libncurses.so.5.6 /lib64/libtinfo.so.5

或32 位系統上的ln -s libncurses.so.5.6 /lib/libtinfo.so.5

3.創建一個mysql用戶和組

shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql

註意
此用戶僅用於運行mysql服務,而不是登錄,因此使用useradd -r和-s /bin/false命令選項來創建對服務器主機沒有登錄權限的用戶。

4.解壓到指定目錄

shell> tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz -C /opt
shell> cd /opt
shell> mv mysql-5.7.21-linux-glibc2.12-x86_64 mysql-5.7.21

5.配置環境變量

echo "export PATH=$PATH:/opt/mysql-5.7.21/bin" >> /etc/profile

建議進入/etc/profile文件裏去添加執行 並有source生效

6.配置數據庫目錄

數據目錄:/opt/mysql-5.7.21/data
參數文件my.cnf:/opt/mysql-5.7.21/etc/my.cnf
錯誤日誌log-error:/opt/mysql-5.7.21/log/mysql_error.log
二進制日誌log-bin:/opt/mysql-5.7.21/log/mysql_bin.log
慢查詢日誌slow_query_log_file:/opt/mysql-5.7.21/log/mysql_slow_query.log
套接字socket文件:/opt/mysql-5.7.21/run/mysql.sock
pid文件:/opt/mysql-5.7.21/run/mysql.pid
創建目錄:

shell> mkdir -p /opt/mysql-5.7.21/{data,log,etc,run}
shell> chown -R mysql:mysql /opt/mysql-5.7.21
shell> chmod 750 /opt/mysql-5.7.21/{data,log,etc,run}

7.配置my.cnf文件

在/opt/mysql-5.7.21/etc/下創建my.cnf文件,加入如下參數,其他參數根據需要配置

shell> touch /opt/mysql-5.7.21/etc/my.cnf
shell> chown mysql:mysql /opt/mysql-5.7.21/etc/my.cnf

[client]
port = 3306
socket = /opt/mysql-5.7.21/run/mysql.sock

[mysqld]
port = 3306
socket = /opt/mysql-5.7.21/run/mysql.sock
pid_file = /opt/mysql-5.7.21/run/mysql.pid
datadir = /opt/mysql-5.7.21/data
default_storage_engine = InnoDB
max_allowed_packet = 128M
max_connections = 2048
open_files_limit = 65535

skip-name-resolve
lower_case_table_names=1

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect=‘SET NAMES utf8mb4‘


innodb_buffer_pool_size = 128M
innodb_log_file_size = 128M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0


key_buffer_size = 16M

log-error = /opt/mysql-5.7.21/log/mysql_error.log
log-bin = /opt/mysql-5.7.21/log/mysql_bin.log
slow_query_log = 1
slow_query_log_file = /opt/mysql-5.7.21/log/mysql_slow_query.log
long_query_time = 5


tmp_table_size = 16M
max_heap_table_size = 16M
query_cache_type = 0
query_cache_size = 0

server-id=1

8.初始化

shell> mysqld --initialize --user=mysql --basedir=/opt/mysql-5.7.21 --datadir=/opt/mysql-5.7.21/data

此時會生成一個臨時密碼,可以在mysql_error.log文件找到

shell> grep ‘temporary password‘ /opt/mysql-5.7.21/log/mysql_error.log 

生成ssl

shell> mysql_ssl_rsa_setup --basedir=/opt/mysql-5.7.21 --datadir=/opt/mysql-5.7.21/data/

9.配置服務,使用systemctl管理

shell> cd /usr/lib/systemd/system
shell> touch mysqld.service 

文件內容如下

# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# systemd service file for MySQL forking server
#

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/opt/mysql-5.7.21/run/mysql.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd

# Start main service
ExecStart=/opt/mysql-5.7.21/bin/mysqld --daemonize --pid-file=/opt/mysql-5.7.21/run/mysql.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 65535

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

讓systemctl加載配置服務

shell> systemctl daemon-reload
shell> systemctl enable mysqld.service
shell> systemctl is-enabled mysqld

10.啟動MySQL服務

shell> systemctl start mysqld.service 

11.MySQL用戶初始化

重置密碼(上一步已經重置過了 這次可以忽略)
刪除匿名用戶
關閉root用戶的遠程登錄
刪除測試數據庫

shell> /usr/local/mysql/bin/mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: Y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.


Normally, root should only be allowed to connect from
‘localhost‘. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

By default, MySQL comes with a database named ‘test‘ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done!

12.導入時區(根據實際情況操作)

shell> mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

13.驗證安裝

shell> mysqladmin version -u root -p

-------------------更改密碼------------------------


#my.cnf

[root@bogon ~]# cat /usr/local/mysql/etc/my.cnf
[client]
port = 3306
socket = /usr/local/mysql/run/mysql.sock

[mysqld]
#skip-grant-tables
port = 3306
socket = /usr/local/mysql/run/mysql.sock
pid_file = /usr/local/mysql/run/mysql.pid
datadir = /usr/local/mysql/data
default_storage_engine = InnoDB
max_allowed_packet = 128M
max_connections = 2048
open_files_limit = 65535

skip-name-resolve
lower_case_table_names=1

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect=‘SET NAMES utf8mb4‘


innodb_buffer_pool_size = 128M
innodb_log_file_size = 128M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0


key_buffer_size = 16M

log-error = /usr/local/mysql/log/mysql_error.log
log-bin = /usr/local/mysql/log/mysql_bin.log
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/log/mysql_slow_query.log
long_query_time = 5


tmp_table_size = 16M
max_heap_table_size = 16M
query_cache_type = 0
query_cache_size = 0

server-id=1


-------

#更改密碼

Mysql 5.7版本:

i、mysql> UPDATE user SET authentication_string = password ( ‘new-password‘ ) WHERE User = ‘root‘ ;

Query OK, 0 rows affected (0.00 sec)

ii、將配置文件my.cnf 中 skip-grant-tables 註釋掉,重啟 mysql

iii、使用第一步設置的密碼登錄mysql,再次修改密碼:

mysql> set password = password(‘xxxxxxxx‘); (新密碼必須包含大小寫字母,特殊字符,數字)

mysql> flush privileges ;

Query OK, 0 rows affected (0.01 sec)

-------------------更改密碼------------------------

[root@bogon ~]# ps -ef | grep mysql
mysql 10562 1 0 14:58 ? 00:00:00 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysql.pid
root 10637 9747 0 15:10 pts/2 00:00:00 grep --color=auto mysql
[root@bogon ~]#
[root@bogon ~]#
[root@bogon ~]# mysql -uroot -p111111
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 5
Server version: 5.7.22-log 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>

------------------------------------

Mysql 5.7版本:

i、mysql> UPDATE user SET authentication_string = password ( ‘new-password‘ ) WHERE User = ‘root‘ ;

Query OK, 0 rows affected (0.00 sec)

ii、將配置文件my.cnf 中 skip-grant-tables 註釋掉,重啟 mysql

iii、使用第一步設置的密碼登錄mysql,再次修改密碼:

mysql> set password = password(‘xxxxxxxx‘); (新密碼必須包含大小寫字母,特殊字符,數字)

mysql> flush privileges ;

Query OK, 0 rows affected (0.01 sec)

mysql> quit



-參考

1.https://www.jianshu.com/p/0d628b2f7476
2.https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

Centos7.4 安裝MySQL 5.7.21 (通用二進制包)