CentOS7二進位制方式安裝MySQL5.7

1 )安裝二進位制包需要的依賴包:

yum install ncurses-devel libaio-devel libaio ncurses tree telnet tree lrzsz -y

rpm -qa | grep -E ‘mariadb|mysql’

#檢查作業系統是否自帶mysql和mariadb

rpm -e ——nodeps mariadb-libs ——nodeps

2 ) 下載並解壓縮安裝包:

cd /soft && tar -zxvf mysql-5。7。35-linux-glibc2。12-x86_64。tar。gz -C /usr/local

cd /usr/local/ && mv mysql-5。7。35-linux-glibc2。12-x86_64/ mysql-5。7。35

ln -sv /usr/local/mysql-5。7。35 /usr/local/mysql

3) 新增mysql使用者組:

groupadd mysql

useradd -r -g mysql -s /bin/false mysql

useradd mysql -s /sbin/nologin -g mysql -M

4) 建立資料目錄:

mkdir -p /data/mysqldata

chown mysql:mysql /data/mysqldata

chmod 755 /data/mysqldata

5)初始化資料:

初始化示例:這裡會生成mysql第一次登入用的臨時密碼,需要儲存下來

[root@dbserver mysql]#

/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysqldata

[root@dbserver mysql]# /usr/local/mysql/bin/mysqld ——defaults-file=/etc/my。cnf ——user=mysql ——basedir=/usr/local/mysql ——datadir=/data/mysqldata ——initialize

2020-07-16T11:47:46。382183Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated。 Please use ——explicit_defaults_for_timestamp server option (see documentation for more details)。

2020-07-16T11:47:47。546132Z 0 [Warning] InnoDB: New log files created, LSN=45790

2020-07-16T11:47:47。712170Z 0 [Warning] InnoDB: Creating foreign key constraint system tables。

2020-07-16T11:47:47。770600Z 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: 2b6c088e-c75a-11ea-8cdc-000c29b468d1。

2020-07-16T11:47:47。787739Z 0 [Warning] Gtid table is not ready to be used。 Table ‘mysql。gtid_executed’ cannot be opened。

2020-07-16T11:47:49。446456Z 0 [Warning] CA certificate ca。pem is self signed。

2020-07-16T11:47:49。680683Z 1 [Note] A temporary password is generated for root@localhost: :a6k%rh*THsk

[root@dbserver mysql]#

6)手工寫一個my.cnf配置檔案:

vim /etc/my。cnf

[client]

default-character-set = utf8mb4

socket = /tmp/mysql。sock

[mysqld]

#skip-grant-tables=1

default-storage-engine = INNODB

character-set-server = utf8mb4

collation-server = utf8_general_ci

port=3306

socket=/tmp/mysql。sock

user=mysql

basedir=/usr/local/mysql

datadir=/data/mysqldata

7)建立RSA private key:

/usr/local/mysql/bin/mysql_ssl_rsa_setup ——datadir=/data/mysqldata

指令碼執行過程:

[root@dbserver mysqldata]# /usr/local/mysql/bin/mysql_ssl_rsa_setup ——datadir=/data/mysqldata

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’

——-

[root@dbserver mysqldata]# cd 。。

[root@dbserver data]#

[root@dbserver data]# ll

總用量 36

-rw————- 1 root root 1679 7月 17 00:38 ca-key。pem

-rw-r——r—— 1 root root 1107 7月 17 00:38 ca。pem

-rw-r——r—— 1 root root 1107 7月 17 00:38 client-cert。pem

-rw————- 1 root root 1675 7月 17 00:38 client-key。pem

drwxr-xr-x 5 mysql mysql 4096 7月 16 19:51 mysqldata

-rw————- 1 root root 1675 7月 17 00:38 private_key。pem

-rw-r——r—— 1 root root 451 7月 17 00:38 public_key。pem

-rw-r——r—— 1 root root 1107 7月 17 00:38 server-cert。pem

-rw————- 1 root root 1679 7月 17 00:38 server-key。pem

[root@dbserver data]#

檔案解釋:

ca。pem Self-signed CA certificate

ca-key。pem CA private key

server-cert。pem Server certificate

server-key。pem Server private key

client-cert。pem Client certificate

client-key。pem Client private key

8)配置開機啟動:

啟動命令(這一步要修改啟動檔案裡的basedir=,datadir=選項, 全都改。):

cd /usr/local/mysql

cp support-files/mysql。server /etc/init。d/mysql

chkconfig ——add mysql

chkconfig mysql on

/etc/init。d/mysql restart

service mysql restart

basedir=/usr/local/mysql ### ——basedir mysql基礎目錄

datadir=/data/mysqldata ### ——datadir mysql資料存放目錄

9)修改root 密碼(如果不能登入,採用破解root許可權的方法):

mysql>

select user,host,authentication_string from mysql.user;

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement。

mysql>

update mysql.user set authentication_string=password('mysql123') where user='root' and Host = 'localhost';

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement。

mysql>

alter user 'root'@'localhost' identified by 'mysql' password expire never;

Query OK, 0 rows affected (0。00 sec)

mysql>

select user,host,authentication_string from mysql.user;

+————————-+——————-+——————————————————————-+

| user | host | authentication_string |

+————————-+——————-+——————————————————————-+

| root | localhost | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |

| mysql。session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |

| mysql。sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |

+————————-+——————-+——————————————————————-+

3 rows in set (0。00 sec)

mysql>

update mysql.user set password_expired='N' where user='root';

Query OK, 0 rows affected (0。00 sec)

Rows matched: 1 Changed: 0 Warnings: 0

mysql>

flush privileges;

Query OK, 0 rows affected (0。00 sec)

mysql>

經過一個步驟就已經安裝完成了, 可以正常登入。

[root@dbserver ~]#

service mysql restart

Shutting down MySQL。。。。 SUCCESS!

Starting MySQL。 SUCCESS!

[root@dbserver ~]#

10)新增配置mysql環境變數:

vim /etc/profile

PATH=$PATH:$HOME/bin:/usr/local/mysql:/usr/local/mysql/bin/

export PATH

載入環境變數:

source /etc/profile #配置立即生效

11)登陸mysql、檢視程序。

[root@dbserver ~]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor。 Commands end with ; or \g。

Your MySQL connection id is 2

Server version: 5。7。35 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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>

[root@dbserver ~]# ps -ef |grep mysql |grep -v ‘grep’

root 3442 3373 0 18:59 pts/2 00:00:00 mysql -uroot -p

root 4003 1 0 19:13 pts/3 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe ——datadir=/data/mysqldata ——pid-file=/data/mysqldata/dbserver。pid

mysql 4183 4003 0 19:13 pts/3 00:00:00 /usr/local/mysql/bin/mysqld ——basedir=/usr/local/mysql ——datadir=/data/mysqldata ——plugin-dir=/usr/local/mysql/lib/plugin ——user=mysql ——log-error=dbserver。err ——pid-file=/data/mysqldata/dbserver。pid ——socket=/tmp/mysql。sock ——port=3306

[root@dbserver ~]#

至此 mysql-5。7二進位制包安裝完成: