1. 程式人生 > >二進位制包安裝MySQL8

二進位制包安裝MySQL8

安裝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

image

初始化完成。


--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 [email protected]: 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 &

image

MySQL啟動成功。

通過二進位制包安裝MySQL8,MySQL啟動之後監聽了兩個埠3306和33060。 這是應為MySQL5.7.12 之後新增了X plugin。
這個外掛預設是啟用的,可以在配置配置檔案/etc/my.cnf 新增mysqlx=0關閉X plugin。
也可以在啟動時指定 --mysqlx=0 或 --skip-mysqlx 來禁用X外掛。


image



測試啟動成功之後,我們先停止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/目錄下,所以我們要根據自己實際情況去修改。

image


到這裡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';

'root'@'127.0.0.1'   授權

grant all privileges on *.* to 'root'@'127.0.0.1' ;

重新整理平MySQL許可權

flush privileges;


[[email protected] ~]# 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



[[email protected] 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>