1. 程式人生 > >mysql8.0初探:(一)安裝和初始化使用

mysql8.0初探:(一)安裝和初始化使用

mysql 8.0

雖然很奇怪,mysql5.7的下一個版本居然不是5.8,而是直接跳到8.0,不過特性和功能優化改善很多.

之前看過很多文章都說mysql8.0變化很大,但是一頓測試下來,大部分操作還是比較一致,某些操作不一樣而已.

下面來一步步看有什麽大差別,本篇文章只說兩個方面.


安裝

因為mysql官方和以往一樣,給出二進制包的方式,所以和以往安裝方式並沒有太大差別

首先官網下載二進制包,然後就可以正常解壓縮安裝了,我習慣了下面的方式

tar xf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
mv mysql-8.0.11-linux-glibc2.12-x86_64 /usr/local/
cd /usr/local/
ln -sf mysql-8.0.11-linux-glibc2.12-x86_64/ mysql80.
mkdir -p /data/mysql/data80
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql

其實二進制包也沒什麽安裝概念,都是拿來就用


初始化使用

1.初始化

其實和mysql5.7沒什麽差別

/usr/local/mysql80/bin/mysqld --defaults-file=/usr/local/mysql80/my.cnf --initialize

然後密碼也在之前一樣的地方

sed -n '/password/p' /data/mysql/data80/mysql.err
.....A temporary password is generated for root@localhost: %pqGI?ATQ4fg,

記得啟動前要把權限設置一下再啟動

chown -R mysql:mysql /data/mysql/data*
/usr/local/mysql80/support-files/mysql.server start

先用mysql8.0自帶的mysql客戶端和初始化密碼登進去,後面會說為什麽要這麽幹

/usr/local/mysql80/bin/mysql -uroot -p'%pqGI?ATQ4fg' -S /tmp/mysql80.sock  -P3308

改密碼方式也是和5.7一樣的

mysql> alter user 'root'@'localhost' identified by '123';
mysql> set password for 'root'@'localhost'=password('123');
mysql> update mysql.user set authentication_string=password('123') where user='root' and Host = 'localhost';
mysql> flush privileges;

這個時候,你如果用默認的mysql客戶端登陸,可能就會有這個報錯

mysql -uroot -p123 -P3308 -S /tmp/mysql80.sock 
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/local/mysql/lib/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory

在mysql5.7環境裏,雖然可以設置default_authentication_plugin來改變認證加密方式,但是一般人不會去設置.在mysql8.0下,默認變成了default_authentication_plugin=caching_sha2_password,包括你剛初始化的root用戶也是這個認證加密方式,這樣的結果是讓你除非用新的協議驅動,例如必須用8.0自帶的mysql客戶端才行,不然就連接不上數據庫.這樣就必然造成不兼容的情況,幸好,是可以改回舊的方式的.

vim my.cnf
[mysqld]
default_authentication_plugin=mysql_native_password

但是,這個參數只對新授權的用戶生效,舊的root密碼還是用舊的方式,類似下面這樣

#先用新客戶端登陸
/usr/local/mysql80/bin/mysql -uroot -p123 -P3308 -S /tmp/mysql80.sock
#查看各用戶情況
mysql> select Host,User,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| Host      | User             | plugin                | authentication_string                                                  |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE                              |
| localhost | mysql.session    | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE                              |
| localhost | mysql.sys        | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE                              |
| localhost | root             | caching_sha2_password | $A$005$.Sf}Q/Jh>3b6CjI5/vMJVDIQbS1dbFWaCVuiby7aX3ZY4lB6/M7Vvny5DUA |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)

可以看到plugin是不一樣的,密碼的加密方式也很明顯不一致造成差異.

然後我們再新建一個用戶,就可以看到是和以前一樣了

mysql> select Host,User,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| Host      | User             | plugin                | authentication_string                                                  |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| %         | sroot            | mysql_native_password | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1                              |
| localhost | mysql.infoschema | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE                              |
| localhost | mysql.session    | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE                              |
| localhost | mysql.sys        | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE                              |
| localhost | root             | caching_sha2_password | $A$005$.Sf}Q/Jh>3b6CjI5/vMJVDIQbS1dbFWaCVuiby7aX3ZY4lB6/M7Vvny5DUA |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)

再次用默認的mysql客戶端登陸

mysql -usroot -p123123 -P3308 -S /tmp/mysql80.sock
mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.20, for linux-glibc2.12 (x86_64) using  EditLine wrapper

Connection id:        17
Current database:    
Current user:        sroot@localhost
SSL:            Not in use
Current pager:        stdout
Using outfile:        ''
Using delimiter:    ;
Server version:        8.0.11 MySQL Community Server - GPL
Protocol version:    10
Connection:        Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /tmp/mysql80.sock
Uptime:            1 hour 25 min 18 sec

Threads: 2  Questions: 120  Slow queries: 0  Opens: 230  Flush tables: 2  Open tables: 203  Queries per second avg: 0.023
--------------

解決了一個兼容性問題了.


2.授權

上面說了創建一個用戶,在mysql8.0創建用戶和授權和之前不太一樣.其實也不能說不一樣,而是說更嚴格,需要先創建用戶和設置密碼,然後才能授權.

#先創建一個用戶
create user 'sroot'@'%' identified by '123123';
#再進行授權
grant all privileges on *.* to 'sroot'@'%' with grant option;

如果按以前的方式來做,那就會報語法錯誤

grant all privileges on *.* to 'sroot'@'%' identified by '123123';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '123123'' at line 1

所以授權用戶要註意方法了


3.參數變更

因為版面有限,我沒有貼出my.cnf文件,而由於一些特性的原因,mysql8.0一些配置和之前的不太一樣,下面來列舉我發現的.

query_cache徹底消失了,在mysql5.7還需要手動設置為關閉,現在可以不用理會了,所以相應的兩個參數就可以註釋掉了

#query_cache_size=0
#query_cache_type=0

innodb_undo_logs不能再設置了,在mysql8.0裏,undo獨立表空間是默認開啟的,而且值為2,也就是默認兩個,但是不能設innodb_undo_logs這個參數來指定回滾段大少,回滾段默認就是128

#innodb_undo_logs

其實暫時沒發現,待發現再補上

然後也發現系統表現在都是innodb引擎,其實之前也在特性裏看到了.








mysql8.0初探:(一)安裝和初始化使用