centos7下安裝MySQL 5.7.26 二進制版本(免安裝綠色版)


MySQL 5.7.26 二進制版本安裝(免安裝綠色版)

下載地址

 

https://downloads.mysql.com/archives/community/

 

https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

 

 PS:下載一些國外站點軟件,用迅雷還是比較管用

 

下載並上傳軟件至/opt/software

[root@mysql01 ~]# mkdir -p /opt/software

[root@mysql01 ~]# cd /opt/software/

[root@mysql01 software]# yum install -y lrzsz #文件拖拽軟件

[root@mysql01 software]# rz -E

rz waiting to receive.

[root@mysql01 software]# ll

總用量 629756

-rw-r--r-- 1 root root 644869837 4月  18 23:48 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

 

解壓軟件

[root@mysql01 software]# tar -xvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

[root@mysql01 software]# mkdir /application

[root@mysql01 software]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql

[root@mysql01 software]# cd /application/mysql/

[root@mysql01 mysql]# ls

bin  COPYING  docs  include  lib  man  README  share  support-files

 

處理原始環境,刪除系統自帶mariadb-libs,創建mysql用戶

[root@mysql01 ~]# rpm -qa | grep mariadb

mariadb-libs-5.5.64-1.el7.x86_64

[root@mysql01 ~]# yum remove mariadb-libs.x86_64 -y

 

[root@mysql01 ~]# useradd -s /sbin/nologin mysql

[root@mysql01 ~]# id mysql

uid=1001(mysql) gid=1001(mysql) =1001(mysql)

 

設置環境變量

[root@mysql01 ~]# vim /etc/profile

export PATH=/application/mysql/bin:$PATH

[root@mysql01 ~]# source /etc/profile

 

查看MySQL版本

[root@mysql01 ~]# mysql -V

mysql  Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using  EditLine wrapper

[root@mysql01 ~]# mysql --version

mysql  Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using  EditLine wrapper

 

創建數據路徑並授權

1.添加一塊新磁盤模擬數據盤

2.格式化並掛載

[root@mysql01 ~]# fdisk -l #查看磁盤、分區信息

 

[root@mysql01 ~]# mkfs.xfs /dev/sdb

 

[root@mysql01 ~]# blkid #查看磁盤UUID

/dev/sdb: UUID="5b995ceb-96be-4408-9125-51b931c5c543" TYPE="xfs"

 

[root@mysql01 ~]# vim /etc/fstab

UUID="5b995ceb-96be-4408-9125-51b931c5c543"     /data   xfs     defaults        0       0

[root@mysql01 ~]# mount -a #是將/etc/fstab的所有內容重新加載

 

3.對MySQL軟件和數據目錄進行授權

[root@mysql01 ~]# chown -R mysql.mysql /application/*

[root@mysql01 ~]# chown -R mysql.mysql /data

 

4.初始化數據(創建系統數據)

# 5.6 版本 初始化命令  /application/mysql/scripts/mysql_install_db

# 5.7 版本

[root@mysql01 ~]# mkdir -p /data/mysql/data #創建初始化數據路徑

[root@mysql01 ~]# chown -R mysql.mysql /data

方法一

[root@mysql01 ~]# mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data

2020-04-19T14:27:50.401324Z 1 [Note] A temporary password is generated for root@localhost: dr7uTgZ/q!JI

5.7說明:

--initialize 參數:

1. 對於密碼復雜度進行定制:12位,4

2. 密碼過期時間:180

3. root@localhost用戶設置臨時密碼

方法二

--initialize-insecure 參數:

無限制,無臨時密碼

[root@mysql01 ~]# rm -rf /data/mysql/data/* #先刪除方法一初始化信息

[root@mysql01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data

2020-04-19T15:24:41.446386Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

 

5.配置文件准備

cat >/etc/my.cnf <<EOF

[mysqld]

user=mysql

basedir=/application/mysql

datadir=/data/mysql/data

socket=/tmp/mysql.sock

server_id=6

port=3306

[mysql]

socket=/tmp/mysql.sock

EOF

 

6.啟動數據庫

方法一:

sys-v #centos6中使用

[root@mysql01 ~]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@mysql01 ~]# service mysqld restart

 ERROR! MySQL server PID file could not be found!

Starting MySQL.Logging to '/data/mysql/data/mysql01.err'.

.. SUCCESS!

 

[root@mysql01 ~]# netstat -lnp | grep 3306 #通過端口查看是否啟動

tcp6       0      0 :::3306                 :::*                    LISTEN      4982/mysqld  

方法二:

systemd #centos7中使用

 

[root@mysql01 ~]# /etc/init.d/mysqld stop #先關閉方法一中啟動的MySQL

Shutting down MySQL.. SUCCESS!

 

[root@mysql01 ~]# cat >/etc/systemd/system/mysqld.service <<EOF

[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

ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf

LimitNOFILE = 5000

EOF

 

[root@mysql01 ~]# systemctl start mysqld.service

[root@mysql01 ~]# netstat -nlp | grep 3306

tcp6       0      0 :::3306                 :::*                    LISTEN      5134/mysqld

 

7.如何分析MySQL數據庫無法啟動情形

 

查看日志:

在哪?

/data/mysql/data/主機名.err

[ERROR] 上下文

可能情況:

/etc/my.cnf 路徑不對等

/tmp/mysql.sock文件修改過 或 刪除過

數據目錄權限不是mysql

參數改錯了

 

8.修改數據庫密碼

[root@mysql01 ~]# mysqladmin -uroot -p password

Enter password: #輸入舊密碼,第一次使用密碼為空

New password: #輸入新密碼

Confirm new password: #再次確認新密碼

Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

 

9.管理員用戶密碼忘記了

--skip-grant-tables  #跳過授權表

--skip-networking    #跳過遠程登錄

1)關閉數據庫

[root@mysql01 ~]# /etc/init.d/mysqld stop

Shutting down MySQL.. SUCCESS!

 

2)啟動數據庫到維護模式

[root@mysql01 ~]# mysqld_safe --skip-grant-tables --skip-networking &

 

3)登錄並修改密碼

[root@mysql01 ~]# mysql

mysql> select user,host from mysql.user; #查看用戶信息

mysql> select user,host,authentication_string from mysql.user; #查看用戶和密碼字段信息

 

mysql> alter user root@'localhost' identified by '123456'; #關閉認證后無法使用這條命令

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

 

mysql> flush privileges; #手動加載刷新授權表

 

mysql> alter user root@'localhost' identified by '123456'; #再次執行命令重置密碼成功

Query OK, 0 rows affected (0.00 sec)

mysql> exit #退出數據庫

Bye

 

4)停止數據庫,再正常啟動 登錄驗證修改密碼是否成功

 

[root@mysql01 ~]# /etc/init.d/mysqld stop

Shutting down MySQL..2020-04-20T15:55:40.277521Z mysqld_safe mysqld from pid file /data/mysql/data/mysql01.pid ended

 SUCCESS!

[1]+  完成                  mysqld_safe --skip-grant-tables --skip-networking

[root@mysql01 ~]# /etc/init.d/mysqld start

Starting MySQL. SUCCESS!

 

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

Enter password: #輸入修改后密碼驗證


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM