MySQL5.7部署可以分爲rpm包部署、源碼包部署、二進制包部署。
源碼包部署:優點是靈活性較高,可以自定義編譯參數,效率高,缺點是部署時間較長
二進制包部署:優點是事先已經編譯好,部署較快,可以自定義一部分參數,缺點是時間相比使用yum安裝,還是較長
rpm包部署:優點是部署速度快,缺點是定製性差
一般沒有特殊要求時,可以使用yum包部署
下面開始使用二進制包部署MySQL5.7.26二進制包
下載地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
在Select Operating System選項中選中”Linux Generic",在最下方的Linux - Generic (glibc 2.12) (x86, 64-bit), TAR項後方的download即可下載
1.添加MySQL數據庫運行的用戶和用戶組
groupadd -r mysql
useradd -r -g mysql mysql
2.此次MySQL部署的目錄爲/application,將MySQL安裝包解壓到/application/mysql-5.7.26-linux-glibc2.12-x86_64目錄,併爲此目錄創建軟鏈接
ln -s /application/mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql
更改目錄權限chown -R mysql:mysql /application/mysql-5.7.26-linux-glibc2.12-x86_64
3.初始化數據庫
/application/mysql-5.7.26-linux-glibc2.12-x86_64/bin/mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
[root@chanye application]# /application/mysql-5.7.26-linux-glibc2.12-x86_64/bin/mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
2019-06-18T08:35:49.103727Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-18T08:35:49.885283Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-06-18T08:35:49.944856Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-06-18T08:35:50.002905Z 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: 138aa81a-91a4-11e9-8f49-005056813525.
2019-06-18T08:35:50.004044Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-06-18T08:35:50.004493Z 1 [Note] A temporary password is generated for root@localhost: O;iSA6z>SieK
記住最後一行的臨時密碼
4.創建RSA private key
/application/mysql/bin/mysql_ssl_rsa_setup --datadir=/application/mysql/data
Generating a 2048 bit RSA private key
..........................................................................................+++
....+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
............................+++
.+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
..........................................................................+++
...........................................................................................+++
writing new private key to 'client-key.pem'
5.添加mysql到開機自啓動
cp /application/mysql-5.7.26-linux-glibc2.12-x86_64/support-files/mysql.server /etc/init.d/mysqld
修改/etc/init.d/mysqld中的basedir和datadir
46 basedir=/application/mysql
47 datadir=/application/mysql/data
加入開機自啓動
chkconfig mysqld on
chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
修改全局配置文件將mysql可執行路徑加入到PATH,在/etc/profile最後一行添加:
export PATH=/application/mysql/bin:$PATH
使用source /etc/profile使配置生效
刪掉系統自帶的/etc/my.cnf或者改名,在/application/mysql/目錄新建my.cnf,並且將文件屬性調整爲mysql:mysql
[mysqld]
character-set-server=utf8
datadir=/application/mysql/data
socket=/application/mysql/mysql.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
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[client]
character-set-server=utf8
socket=/application/mysql/mysql.sock
[mysql]
character-set-server=utf8
socket=/application/mysql/mysql.sock
6.啓動mysql
啓動mysql
/etc/init.d/mysqld start
此時使用初始化提供的密碼進行登陸
# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
系統默認的客戶端使用的mysql.sock文件是基於unix sock的,此時最簡便的辦法是創建一個軟鏈接
ln -s /application/mysql/mysql.sock /tmp/mysql.sock
如果不創建軟鏈接,使用mysql命令登陸的時候使用-S選項,手工指定mysql.sock的位置也是可以登陸的
登陸成功後需要更改管理員密碼
alter user 'root'@'localhost' identified by '598545$kdOctamvsU';
flush privileges;
查看mysql狀態
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
Connection id: 4
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.26 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /application/mysql/mysql.sock
Uptime: 7 min 56 sec
Threads: 2 Questions: 14 Slow queries: 0 Opens: 116 Flush tables: 1 Open tables: 109 Queries per second avg: 0.029
--------------