項(xiàng)目需求
Linux操作系統(tǒng)上安裝部署Mysql數(shù)據(jù)庫(kù),深入學(xué)習(xí)或企業(yè)生成環(huán)境沒有上網(wǎng)環(huán)境時(shí),無法通過yum方式安裝部署,可通過二進(jìn)制安裝部署所需要的數(shù)據(jù)庫(kù),選擇對(duì)應(yīng)的數(shù)據(jù)庫(kù)版本并完成數(shù)據(jù)庫(kù)的安裝,開始學(xué)習(xí)或使用數(shù)據(jù)庫(kù)。
(1)深入學(xué)習(xí)研究,渴望成為DBA。
(2)業(yè)務(wù)應(yīng)用需求,能夠在無上網(wǎng)環(huán)境下安裝部署數(shù)據(jù)庫(kù)。
(3)了解MySQL數(shù)據(jù)庫(kù)的安裝部署過程。
一、數(shù)據(jù)庫(kù)軟件的下載
1、安裝系統(tǒng)環(huán)境準(zhǔn)備
操作系統(tǒng):centos7.6
地址:172.21.209.125
關(guān)閉操作系統(tǒng)的防火墻并禁用Selinux。
2、二進(jìn)制安裝包下載。
2.1、mysql數(shù)據(jù)庫(kù)官網(wǎng)地址
https://www.mysql.com/
2.2、數(shù)據(jù)庫(kù)下載地址
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
2.3、版本選擇下載
下載后,將壓縮包上傳到服務(wù)器指定的目錄下。如data目錄下。
[root@mysql03 data]# pwd
/data
[root@mysql03 data]# ll mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
-rw-r--r-- 1 root root 660017902 Mar 24 2020 mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
二、數(shù)據(jù)庫(kù)軟件安裝部署
1、環(huán)境檢查。安裝服務(wù)器上是否存在Mariadb數(shù)據(jù)庫(kù)環(huán)境,如果存在需要卸載并清除默認(rèn)的數(shù)據(jù)庫(kù)配置文件。
檢查mariadb數(shù)據(jù)庫(kù)環(huán)境
[root@mysql03 ~]# rpm -qa|grep mariadb
卸載移除mariadb數(shù)據(jù)庫(kù)
[root@mysql03 ~]# yum remove -y mariadb-*
刪除數(shù)據(jù)庫(kù)默認(rèn)配置文件
[root@mysql03 ~]# rm -rf /etc/my.cnf
2、安裝依賴包,并解壓到指定目錄下,添加環(huán)境變量。
安裝環(huán)境依賴包
yum install -y libaio-devel
查看二進(jìn)制安裝包
[root@mysql03 data]# ll mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
-rw-r--r-- 1 root root 660017902 Mar 24 2020 mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
解壓到指定指定目錄下
[root@mysql03 data]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
創(chuàng)建軟連接
[root@mysql03 local]# ln -s mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
[root@mysql03 local]# ll mysql
lrwxrwxrwx 1 root root 36 Oct 21 15:17 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
添加環(huán)境變量
[root@mysql03 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile && source /etc/profile
在任意目錄下輸入mysql命令查看數(shù)據(jù)庫(kù)版本。
[root@mysql03 ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.30, for linux-glibc2.12 (x86_64) using EditLine wrapper
查看mysql的命令位置
[root@mysql03 ~]# which mysql
3、創(chuàng)建數(shù)據(jù)目錄和用戶。
創(chuàng)建數(shù)據(jù)存放目錄和配置文件存在目錄
[root@mysql03 mysql]# mkdir /data/mysql/{3306,conf} -p
注:3306是存放3306端口下的數(shù)據(jù)目錄,conf統(tǒng)一存放數(shù)據(jù)庫(kù)配置文件。
添加用戶
[root@mysql03 mysql]# useradd -M -s /sbin/nologin mysql
數(shù)據(jù)目錄授權(quán)
[root@mysql03 mysql]# chown -R mysql.mysql /data/mysql
4、數(shù)據(jù)庫(kù)初始化
數(shù)據(jù)庫(kù)數(shù)據(jù)初始化
[root@mysql03 mysql]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3306/data
顯示結(jié)果,表示完成數(shù)據(jù)庫(kù)的初始化。
[root@mysql03 3306]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3306/data
2020-10-21T07:32:53.611188Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-21T07:32:54.615541Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-10-21T07:32:54.760723Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-10-21T07:32:54.836833Z 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: a231470b-136f-11eb-9047-fa163e49eccc.
2020-10-21T07:32:54.842611Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-10-21T07:32:56.384194Z 0 [Warning] CA certificate ca.pem is self signed.
2020-10-21T07:32:56.725640Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
參數(shù)說明: MySQL數(shù)據(jù)庫(kù)5.7+ 版本初始化命令詳解
mysqld:數(shù)據(jù)庫(kù)后臺(tái)進(jìn)程
--initialize-insecure:初始化核心參數(shù)
--user:用戶
--basedir:數(shù)據(jù)庫(kù)軟件路徑:/usr/local/mysql
--datadir:數(shù)據(jù)存放目錄:/data/mysql/3306/data
初始化兩種方式:
方式1:
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3306/data ##創(chuàng)建一個(gè)空密碼,只能本地登錄,登錄mysql時(shí),不需要輸入密碼。
方式2:
2、mysqld --initialize--user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3306/data ##安全的模式,比較安全,生成一個(gè)臨時(shí)密碼。
--initialize-insecure參數(shù)有兩種方式,區(qū)別如下:
initialize-insecure:不安全,初始化完成后,登陸數(shù)據(jù)庫(kù)可無密碼登錄;沒有密碼,沒有密碼策略。
initialize:比較安全,初始化完成后會(huì)生成臨時(shí)隨機(jī)密碼,在第一次登錄數(shù)據(jù)庫(kù)后,需要立即修改初始密碼。
5、mysql配置文件
cat > /data/mysql/conf/my.cnf << EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/3306/data
socket=/data/mysql/3306/data/mysql.sock
server_id=8
log_bin=/data/mysql/binlog_3306
port=3306
[mysql]
default-character-set=utf8
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
socket=/data/mysql/3306/data/mysql.sock
[client]
default-character-set=utf8
EOF
6、復(fù)制拷貝啟動(dòng)腳本并啟動(dòng)數(shù)據(jù)庫(kù),查看數(shù)據(jù)進(jìn)程和監(jiān)聽端口。
數(shù)據(jù)庫(kù)啟動(dòng)腳本
[root@mysql03 mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
啟動(dòng)數(shù)據(jù)庫(kù),&表示后臺(tái)啟動(dòng)
[root@mysql03 mysql]# mysqld --defaults-file=/data/mysql/conf/my.3306.cnf --user=root &
檢查數(shù)據(jù)庫(kù)進(jìn)程
[root@mysql03 mysql]# ps -ef|grep mysql
mysql 12639 10814 0 Oct21 pts/2 00:00:31 mysqld --defaults-file=/data/mysql/conf/my.3306.cnf --user=root
root 15308 11290 0 10:18 pts/3 00:00:00 grep --color=auto 3306
檢查數(shù)據(jù)庫(kù)端口3306
[root@mysql03 mysql]# netstat -lntup|grep 3306
tcp6 0 0 :::3306 :::* LISTEN 12639/mysqld
7、登陸數(shù)據(jù)庫(kù)
登陸數(shù)據(jù)庫(kù)時(shí),需要密碼驗(yàn)證。
[root@mysql03 conf]# mysql -uroot -p -S /data/mysql/3310/data/mysql.sock
Enter password:
2020-10-22T02:26:02.100625Z 3 [Note] Access denied for user 'root'@'localhost' (using password: YES)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
數(shù)據(jù)庫(kù)登陸方式:
1、本地套接字登陸:mysql -uroot -p -S /data/mysql/3306/data/mysql.sock
2、遠(yuǎn)程登錄:mysql -h 127.0.0.1 -uroot -p
三、數(shù)據(jù)庫(kù)密碼修改
1、因在數(shù)據(jù)庫(kù)初始化完成后,登陸數(shù)據(jù)庫(kù)時(shí),由于數(shù)據(jù)庫(kù)密碼問題,需要跳過授權(quán)表并重新設(shè)置數(shù)據(jù)庫(kù)登陸密碼。
數(shù)據(jù)庫(kù)關(guān)閉
[root@mysql03 3306]# mysqladmin -uroot -p123456 -S /data/mysql/3306/data/mysql.sock shutdown
跳過授權(quán)表,后臺(tái)啟動(dòng)數(shù)據(jù)庫(kù)
[root@mysql03 3306]# mysqld --defaults-file=/data/mysql/conf/my.3307.cnf --user=root --skip-grant-tables &
登陸數(shù)據(jù)庫(kù),并完成密碼修改和授權(quán)
[root@mysql03 3306]# mysql -uroot -p -S /data/mysql/3306/data/mysql.sock
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
修改用戶密碼
mysql> update user set authentication_string=password('123456') where user='root' and host = 'localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
創(chuàng)建用戶并授權(quán)
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
刷新權(quán)限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
查看用戶權(quán)限列表
mysql> select host,user from mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
4 rows in set (0.00 sec)
mysql>
2、完成用戶密碼修改和授權(quán)后,重新關(guān)閉并啟動(dòng)數(shù)據(jù)庫(kù)。
關(guān)閉數(shù)據(jù)庫(kù)
[root@mysql03 3306]# mysqladmin -uroot -p123456 -S /data/mysql/3306/data/mysql.sock shutdown
啟動(dòng)數(shù)據(jù)庫(kù)
[root@mysql03 3306]# mysqld --defaults-file=/data/mysql/conf/my.3306.cnf --user=root &
登陸數(shù)據(jù)庫(kù),輸入用戶名密碼
[root@mysql03 3306]# mysql -uroot -p -S /data/mysql/3306/data/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30 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>
查看數(shù)據(jù)庫(kù)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
至此,Mysql5.7二進(jìn)制方式數(shù)據(jù)庫(kù)安裝完成。