安裝MySQL8
安裝幾個常用的命令。
yum -y install wget vim xz lrzsz
安裝MySQL依賴包。
yum -y install libaio-devel numactl-devel
下載MySQL8並解壓
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz -C /usr/local/ #需要安裝xz
cd /usr/local/
mv mysql-8.0.13-linux-glibc2.12-x86_64 mysql
創建MySQL數據庫以及日誌的存放目錄:
mkdir -p /data/mysql/{data,tmp,binlog,log}
touch /data/mysql/log/mysqld-error.log
創建mysql用戶並給MySQL目錄授權。
MySQL數據庫需要以一個普通用戶去執行一些操作,因而需要創建一個普通用戶,
useradd mysql -s /sbin/nologin -M chown -R mysql.mysql /data/mysql chown -R mysql.mysql /usr/local/mysql
添加MySQL的環境變量
export PATH=$PATH:/usr/local/mysql/bin echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
初始化MySQL
mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql
初始化完成。
--initialize 生成隨機密碼,官方推薦使用
--initialize-insecure 生成空密碼,root用戶密碼默認爲空
--basedir MySQL的安裝目錄,一般放在/usr/local/mysql/
--datadir 數據庫的存放路徑, 放在比較安全的目錄
--user 指定用戶去初始化MySQL
#官方推薦使用--initialize,會在錯誤日誌中生成臨時密碼,我這裏使用的免密碼的方式。
#cat /data/mysql/error.log | grep -i password # 2018-11-29T02:06:41.253856+08:00 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: wquR3-Kxlg1d
創建MySQL配置文件
vim /etc/my.cnf
[mysqld]
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/data/mysql/log/mysqld-error.log
pid-file=/data/mysql/mysqld.pid
啓動MySQL
mysqld_safe --user=mysql &
MySQL啓動成功。
通過二進制包安裝MySQL8,MySQL啓動之後監聽了兩個端口3306和33060。 這是應爲MySQL5.7.12 之後新增了X plugin。
這個插件默認是啓用的,可以在配置配置文件/etc/my.cnf 添加mysqlx=0關閉X plugin。
也可以在啓動時指定 --mysqlx=0 或 --skip-mysqlx 來禁用X插件。
測試啓動成功之後,我們先停止MySQL 。使用MySQL自帶的啓動腳本來管啓動MySQL 並加入到開機自動運行, 方便以後維護。
# CentOS
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.server
sed -i "45,~50s#basedir=#basedir=/usr/local/mysql#g" /etc/init.d/mysql.server
sed -i "45,~50s#datadir=#datadir=/data/mysql/data#g" /etc/init.d/mysql.server
grep -E '^datadir=|^basedir=' /etc/init.d/mysql.server #確認一下我們是否修改成功
chmod +x /etc/init.d/mysql.server
service mysql.server start
chkconfig mysql.server on
#MySQL自帶的啓動腳本中 basedir和datadir默認都是在/usr/local/mysql/目錄下,所以我們要根據自己實際情況去修改。
到這裏MySQL就算是安裝完成了。
我們簡單的對MySQL權限做一下修改。
登錄MySQL :
由於我初始化的時候 --initialize-insecure 生成空密碼,所以MySQL-client就直接進入到MySQL 。
修改'root'@'localhost' 密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
創建'root'@'127.0.0.1'
CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root_password';
grant all privileges on *.* to 'root'@'127.0.0.1' ;
刷新平MySQL權限
flush privileges;
[root@localhost ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.13 MySQL Community Server - GPL Copyright (c) 2000, 2018, 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> show databases ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; Query OK, 0 rows affected (0.11 sec) mysql> CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root_password'; Query OK, 0 rows affected (0.01 sec) mysql> grant all privileges on *.* to 'root'@'127.0.0.1' ; Query OK, 0 rows affected (0.07 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> quit Bye
[root@localhost data]# mysql -uroot -hlocalhost -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 19 Server version: 8.0.13 MySQL Community Server - GPL Copyright (c) 2000, 2018, 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> show databases ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ rows in set (0.00 sec) mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; +---------------------------------------+ | query | +---------------------------------------+ | User: 'root'@'127.0.0.1'; | | User: 'mysql.infoschema'@'localhost'; | | User: 'mysql.session'@'localhost'; | | User: 'mysql.sys'@'localhost'; | | User: 'root'@'localhost'; | +---------------------------------------+ 5 rows in set (0.00 sec) mysql>