我是陳皮,一個在互聯網 Coding 的 ITer,微信搜索「陳皮的JavaLib」第一時間閱讀最新文章,回復【資料】,即可獲得我精心整理的技術資料,電子書籍,一線大廠面試資料和優秀簡歷模板。
引言
眾所周知,MySQL 8.x 版本做了一些比較大的改動優化,也希望大家能去了解下 MySQL 8 版本的一些新特性和優化點。工欲善其事必先利其器,那就必須要安裝上 MySQL 8 版本的數據庫才能體驗學習,所以下面介紹如何在 Linux 服務器上安裝 MySQL 8.X 版本數據庫。
此次演示在 CentOS 7.x
Linux 服務器上通過壓縮包的方式安裝 MySQL 8.x
數據庫。
安裝步驟
安裝包官方下載地址:https://dev.mysql.com/downloads/mysql/
將下載后的壓縮包上傳到 Linux 服務器,解壓縮。
tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
將解壓后的文件夾重命名為 mysql
,並且移動到 /usr/local/
目錄下。
mv mysql-8.0.26-linux-glibc2.12-x86_64 /usr/local/mysql
進入到 /usr/local/mysql
目錄下,創建存放數據庫數據的文件夾 data
。
cd /usr/local/mysql/
mkdir data
在 mysql 目錄下新建 tmp
文件夾,並修改文件夾的權限。
mkdir tmp
chmod 777 /tmp
創建 mysql 用戶組
和 mysql 用戶
,並且將 mysql 目錄極其下面所有文件權限分配給 mysql 用戶。
groupadd mysql
useradd -g mysql mysql
chown -R mysql.mysql /usr/local/mysql/
在 mysql 目錄下執行以下命令,進行數據庫初始化。初始化成功之后,會打印 root
用戶的臨時密碼,記住后面需要用到。
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
修改 mysql 的配置信息,打開 my.cnf
配置文件。
vim /etc/my.cnf
my.cnf 配置文件的原始內容如下:
[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
將配置文件的內容修改為如下所示:
[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
將 mysql 添加到開機自啟項中,復制 mysql.server
文件到 /etc/init.d/
目錄下並且重命名為 mysql,賦予運行權限,從而我們就可以使用 service mysql xx
命令進行啟停 mysql 服務。
cp ./support-files/mysql.server /etc//init.d/mysql
chmod +x /etc/init.d/mysql
將 mysql 注冊為服務。
chkconfig --add mysql
使用 chkconfig --list mysql
命令查看是否注冊成功。
[root@chenpi mysql]# chkconfig --list mysql
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]'.
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
啟動服務,並查看服務狀態。
[root@chenpi mysql]# service mysql start
Starting MySQL.Logging to '/usr/local/mysql/data/chenpi.err'.
. SUCCESS!
[root@chenpi mysql]# service mysql status
SUCCESS! MySQL running (1622)
將 mysql 命令軟連接到 /usr/bin
用戶執行目錄下。
ln -s /usr/local/mysql/bin/mysql /usr/bin
連接 mysql 服務,密碼輸入剛才安裝時生成的臨時密碼。
mysql -uroot -p
修改密碼。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密碼';
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.02 sec)
設置允許使用 root 用戶遠程連接,至此,mysql 服務安裝完成了。
use mysql
update user set host ='%' where user='root';
FLUSH PRIVILEGES;
退出 mysql 使用命令 exit
。