Linux 安裝 MySQL 8.0.26 超詳細圖文步驟

1、MySQL 8.0.26 下載

官方網站下載 MySQL 8.0.26 安裝包,下載地址:

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

1642316596(1).jpg

需要注意:Linux操作系統是32位還是64位,本案例採用Linux 64位操作系統進行講解,通過wget命令下載安裝包。

使用df -lh命令查看,磁盤大小,儘量安裝在比較大的磁盤下,防止空間不夠使用。

[root@VM-0-4-centos home]# df -lh
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G   24K  3.9G   1% /dev/shm
tmpfs           3.9G  604K  3.9G   1% /run
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1        50G  3.8G   44G   9% /
tmpfs           783M     0  783M   0% /run/user/0

通過上述命令可以看出根目錄空間比較充足,那麼就把安裝包下載到home目錄了,執行wget命令。

[root@VM-0-4-centos home]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz

  

2、解壓縮文件

解壓 mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz 文件,使用tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz 命令。

[root@VM-0-4-centos home]# tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz  
mysql-8.0.26-linux-glibc2.12-x86_64/bin/
mysql-8.0.26-linux-glibc2.12-x86_64/bin/myisam_ftdump
mysql-8.0.26-linux-glibc2.12-x86_64/bin/myisamchk
mysql-8.0.26-linux-glibc2.12-x86_64/bin/myisamlog
mysql-8.0.26-linux-glibc2.12-x86_64/bin/myisampack
mysql-8.0.26-linux-glibc2.12-x86_64/bin/mysql
....
mysql-8.0.26-linux-glibc2.12-x86_64/share/
mysql-8.0.26-linux-glibc2.12-x86_64/share/install_rewriter.sql
mysql-8.0.26-linux-glibc2.12-x86_64/share/uninstall_rewriter.sql

  

3、移動文件

將壓縮包移動到usr/local目錄下,並重命名文件爲mysql,使用mv /home/mysql-8.0.26-linux-glibc2.12-x86_64  /usr/local/mysql命令。

[root@VM-0-4-centos home]# mv mysql-8.0.26-linux-glibc2.12-x86_64 /usr/local/mysql
[root@VM-0-4-centos home]# cd /usr/local/mysql/
[root@VM-0-4-centos mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@VM-0-4-centos mysql]# cd ..
[root@VM-0-4-centos local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  qcloud  sbin  share  src  yd.socket.server
[root@VM-0-4-centos local]#

  

4、創建數據存放目錄

在mysql根目錄下新建一個目錄data,主要用於存放數據庫數據文件,使用mkdir data命令。

[root@VM-0-4-centos local]# cd mysql/
[root@VM-0-4-centos mysql]# mkdir data
[root@VM-0-4-centos mysql]# ls
bin  data  docs  include  lib  LICENSE  man  README  share  support-files
[root@VM-0-4-centos mysql]#

  

5、創建用戶組和用戶

創建mysql用戶組和mysql用戶,使用groupadd mysql和useradd -g mysql mysql命令。

[root@VM-0-4-centos mysql]# groupadd mysql
[root@VM-0-4-centos mysql]# useradd -g mysql mysql

  

6、改變mysql目錄權限

修改mysql目錄權限,可以使用chown -R mysql.mysql /usr/local/mysql/命令。

image.png

修改mysql目錄權限也可以通過chown -R mysql .和chgrp -R mysql .兩個命令。注意:這兩個命令都需要執行的,還有那個點不要忽視掉。

 

7、數據庫初始化

數據庫初始化./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize命令,得到臨時密碼。

[root@VM-0-2-centos mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
2022-01-16T07:32:18.729960Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2022-01-16T07:32:18.729960Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.26) initializing of server in progress as process 7691
2022-01-16T07:32:18.740975Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-01-16T07:32:19.800287Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-01-16T07:32:21.721672Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2022-01-16T07:32:21.722106Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2022-01-16T07:32:21.787669Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: j-6lA2aXv=Pz

需要注意:記錄一下mysql數據庫的臨時密碼 j-6lA2aXv=Pz,後面安裝步驟是需要使用的,否則需要重新安裝數據庫或其他方式獲取密碼,此處問題省略。另外,關於更多其他軟件安裝配置步驟,如redis、kafka、fastdfs、elasticsearch等中間件安裝配置圖文步驟,公衆號Java精選,回覆中間件三個漢字,獲取所有軟件安裝步驟。切勿亂回覆,否則什麼也沒有!!!

 

8、修改my.cnf文件

修改my.cnf文件,使用vim /etc/my.cnf命令。

[mysqld]
    basedir = /usr/local/mysql
    datadir = /usr/local/mysql/data
    socket = /usr/local/mysql/mysql.sock
    character-set-server=utf8
    port = 3306
   sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
 [client]
   socket = /usr/local/mysql/mysql.sock
   default-character-set=utf8
#[mysqld]
#datadir=/var/lib/mysql
#socket=/var/lib/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
 
#[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
 
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d

直接將上述配置內容複製到my.cnf文件中,或者自行修改,然後執行:wq命令,保存並退出。

 

9、創建mysql服務

1)將mysql.server啓動文件複製到/etc/init.d目錄,使用cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld命令。

2)賦予權限,使用chmod +x /etc/rc.d/init.d/mysqld命令;

3)使用chkconfig --add mysqld創建mysql服務。

[root@VM-0-4-centos mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@VM-0-4-centos mysql]# chmod +x /etc/rc.d/init.d/mysqld 
[root@VM-0-4-centos mysql]# chkconfig --add mysqld

檢查mysql服務是否生效,使用chkconfig  --list mysqld命令。

[root@VM-0-4-centos mysql]# 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

  

10、配置全局環境變量

編輯/etc/profile文件,使用vim /etc/profile命令,在profile文件中添加如下兩行配置,使用:wq命令保存後退出。

export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
export PATH

image.png

設置環境變量立即生效使用source /etc/profile命令。

[root@VM-0-4-centos ~]# source /etc/profile
[root@VM-0-4-centos ~]#

  

11、啓動mysql服務

啓動mysql服務,使用service mysql start命令;使用service mysql status命令,查看是否啓動成功。

[root@VM-0-4-centos ~]# service mysql start
Redirecting to /bin/systemctl start mysql.service
[root@VM-0-4-centos ~]# service mysql status
Redirecting to /bin/systemctl status mysql.service
● mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
   Active: active (running) since Sun 2022-01-16 17:17:55 CST; 8s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 27231 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mysqld.service
           ├─27242 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/VM-0-4-centos.pid
           └─27408 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=VM-0-4-cent...
 
Jan 16 17:17:54 VM-0-4-centos systemd[1]: Starting LSB: start and stop MySQL...
Jan 16 17:17:54 VM-0-4-centos mysqld[27231]: Starting MySQL.Logging to '/usr/local/mysql/data/VM-0-4-centos.err'.
Jan 16 17:17:55 VM-0-4-centos mysqld[27231]: SUCCESS!
Jan 16 17:17:55 VM-0-4-centos systemd[1]: Started LSB: start and stop MySQL.

  

12、登錄mysql修改密碼

登錄mysql數據庫,使用mysql -uroot -p密碼命令,臨時密碼是j-6lA2aXv=Pz。

[root@VM-0-4-centos ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.26
 
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
 
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臨時密碼,也就是將第七步數據庫初始化生成的臨時密碼修改成自己需要設置的密碼。

修改mysql數據庫密碼,使用ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';命令。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql>

注意:此處123456修改爲自己的需要密碼即可。

 

13、設置mysql遠程登錄

1)切換數據庫,使用use mysql;命令。

2)修改mysql庫中host值,使用update user set host='%' where user='root' limit 1;命令。

3)刷新mysql權限,使用flush privileges;命令。

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 host='%' where user='root' limit 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql>

  

14、mysql客戶端連接數據庫

客戶端連接mysql數據庫,連接名(自定義名稱)、主機(IP)、端口號及用戶名和密碼,點擊測試連接按鈕,顯示連接成功即可。

image.png

注意:若提示無法連接,需要測試服務器3306端口是否開放。若服務器端可以通過查看防火牆情況,此時方式驗證省略;而從客戶端測試可以使用“telnet IP 端口號”命令。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章