MySQL 8 正式版 8.0.11 已發佈,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,還帶來了大量的改進和更快的性能!node
一. Mysql8.0版本相比以前版本的一些特性mysql
1) 默認編碼utf8
默認編碼使用utf8mb4, utf8mb4編碼是utf8編碼的超集,兼容utf8,而且能存儲4字節的表情字符歷史,MySQL數據庫的 "utf8"並非真正概念裏的 UTF-8。MySQL中的"utf8"編碼只支持最大3字節每字符。真正的你們正在使用的UTF-8編碼是應該能支持4字節每一個字符。MySQL的開發者沒有修復這個bug。他們在2010年增長了一個變通的方法:一個新的字符集"utf8mb4"另外,utf-32編碼固定使用4字節,32bit存儲相比utf8浪費空間。sql
2) 降序索引
在以前的版本中能夠建立,可是實際建立的仍是升序索引數據庫
mysql> create table t1(id1 int,id2 int,key(id1,id2 desc)); Query OK, 0 rows affected (0.10 sec) mysql> show create table t1 \G; *************************** 1. row *************************** Table: t1 Create Table: CREATE TABLE `t1` ( `id1` int(11) DEFAULT NULL, `id2` int(11) DEFAULT NULL, KEY `id1` (`id1`,`id2` DESC) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.00 sec) ERROR: No query specified
另外,因爲降序索引的引入,MySQL 8.0不再會對group by操做進行隱式排序json
3) 隱藏索引
隱藏索引的特性對於性能調試很是有用。當一個索引隱藏時,它不會被查詢優化器所使用。也就是說,咱們能夠隱藏一個索引,而後觀察對數據庫的影響。若是數據庫性能有所降低,就說明這個索引是有用的,因而將其「恢復顯示」便可;若是數據庫性能看不出變化,說明這個索引是多餘的,能夠刪掉了.vim
mysql> create index idx2 on t1(id1); Query OK, 0 rows affected (0.35 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table t1 alter index idx2 invisible; Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from t1 where key_name='idx2' \G; *************************** 1. row *************************** Table: t1 Non_unique: 1 Key_name: idx2 Seq_in_index: 1 Column_name: id1 Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: Visible: NO 1 row in set (0.01 sec) ERROR: No query specified
當索引被隱藏時,它的內容仍然是和正常索引同樣實時更新的,此特性是專門爲優化調試使用。若是長期隱藏一個索引,那還不如干脆刪掉,由於畢竟索引存在會影響插入、更新和刪除的性能.centos
4) 設置持久化
在oracle中可使用scope設置參數肯定是否到spfile, 如今mysql8.0也能夠放在文件中了,使用方法:數組
mysql> show variables like 'max_connects'; Empty set (0.07 sec) mysql> show variables like 'max_connections'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 500 | +-----------------+-------+ 1 row in set (0.01 sec) mysql> set persist max_connections=1000; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'max_connections'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 1000 | +-----------------+-------+ 1 row in set (0.01 sec)
能夠查看在數據目錄datadir中的一個mysqld-auto.cnf文件中保存了設置,下次啓動時候將用此配置覆蓋默認配置參數
{ "Version" : 1 , "mysql_server" : { "max_connections" : { "Value" : "1000" , "Metadata" : { "Timestamp" : 1540437420567571 , "User" : "root" , "Host" : "" } } } }緩存
5) 通用表表達式(Common Table Expressions)
也能夠稱爲虛擬視圖,大大簡化複雜查詢安全
mysql> WITH -> t1 AS (SELECT * FROM t1), -> t2 AS (SELECT * FROM t1) -> SELECT t1.*, t2.* -> FROM t1, t2; +------+------+------+------+ | id1 | id2 | id1 | id2 | +------+------+------+------+ | 1 | 2 | 1 | 2 | +------+------+------+------+ 1 row in set (0.00 sec)
6) 窗口函數(Window Functions)
MySQL 被吐槽最多的特性之一就是缺乏 rank() 函數,當須要在查詢當中實現排名時,必須手寫 @ 變量。可是從 8.0 開始,MySQL 新增了一個叫窗口函數的概念,它能夠用來實現若干新的查詢方式
說明,窗口能夠單首創建。
mysql> create table tbl2(name varchar(10),amount int); Query OK, 0 rows affected (0.16 sec) mysql> insert into tbl2 values('usa',100),('china',101),('japan',103),('russian',99); Query OK, 4 rows affected (0.13 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select t1.*,rank() over w as 'rank' from tbl2 as t1 window w as (order by amount); +---------+--------+------+ | name | amount | rank | +---------+--------+------+ | russian | 99 | 1 | | usa | 100 | 2 | | china | 101 | 3 | | japan | 103 | 4 | +---------+--------+------+ 4 rows in set (0.00 sec) mysql> select t1.*,rank() over() as 'rank' from tbl2 as t1; +---------+--------+------+ | name | amount | rank | +---------+--------+------+ | usa | 100 | 1 | | china | 101 | 1 | | japan | 103 | 1 | | russian | 99 | 1 | +---------+--------+------+ 4 rows in set (0.00 sec) mysql> select t1.*,rank() over(order by amount) as 'rank' from tbl2 as t1; +---------+--------+------+ | name | amount | rank | +---------+--------+------+ | russian | 99 | 1 | | usa | 100 | 2 | | china | 101 | 3 | | japan | 103 | 4 | +---------+--------+------+ 4 rows in set (0.00 sec) mysql> select t1.*,sum() over() as 'rank' from tbl2 as t1; 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 ') over() as 'rank' from tbl2 as t1' at line 1 mysql> select t1.*,sum(amount) over() as 'sum' from tbl2 as t1; +---------+--------+------+ | name | amount | sum | +---------+--------+------+ | usa | 100 | 403 | | china | 101 | 403 | | japan | 103 | 403 | | russian | 99 | 403 | +---------+--------+------+ 4 rows in set (0.00 sec)
7) 安全性
對 OpenSSL 的改進、新的默認身份驗證、SQL 角色、密碼強度、受權。MySQL如今維護關於密碼歷史的信息,容許對之前密碼的重用進行限制
好比常見的一個鏈接錯誤,參考:https://blog.csdn.net/jc_benben/article/details/80652897
8) 增長JSON (json enhancements)AND OpenGIS spatial types
mysql提供了不少json相關的函數和API接口,添加了基於路徑查詢參數從 JSON 字段中抽取數據的 JSON_EXTRACT() 函數,以及用於將數據分別組合到 JSON 數組和對象中的 JSON_ARRAYAGG() 和 JSON_OBJECTAGG() 聚合函數
9) 原子數據定義語句(原子DDL)(Atomic Data Definition Statements (Atomic DDL))
10) 資源管理(Resource management)
MySQL如今支持資源組的建立和管理,並容許將服務器內運行的線程分配給特定組,以便線程根據組可用的資源執行。組屬性能夠控制其資源,以啓用或限制組中線程的資源消耗
11) Data dictionary
MySQL如今包含一個事務數據字典,用於存儲有關數據庫對象的信息,In previous MySQL releases, dictionary data was stored in metadata files and nontransactional tables
好比myisam和innodb表定義frm文件等.
12) innodb的加強
1 每次值更改時,當前最大自動增量計數器值將寫入重作日誌,並保存到每一個檢查點上的引擎專用系統表中。這些更改使當前最大自動增量計數器值在服務器從新啓動時保持不變。
2 遇到索引樹損壞時, InnoDB將損壞標誌寫入重作日誌,這會使損壞標誌崩潰安全。InnoDB還將內存中損壞標誌數據寫入每一個檢查點上的引擎專用系統表。在恢復期間, InnoDB從兩個位置讀取損壞標誌並在將內存表和索引對象標記爲損壞以前合併結果。
3 InnoDB 分佈式緩存插件支持多個get操做(讀取在一個單一的多鍵/值對分佈式緩存查詢)和範圍查詢;
4 新的動態配置選項 innodb_deadlock_detect可用於禁用死鎖檢測。在高併發系統上,當許多線程等待同一個鎖時,死鎖檢測會致使速度減慢。有時,禁用死鎖檢測可能更有效,而且在innodb_lock_wait_timeout發生死鎖時依賴於事務回滾的設置。
5 INFORMATION_SCHEMA.INNODB_CACHED_INDEXES 表報告InnoDB每一個索引緩衝池中緩存的索引頁數 。
6 InnoDB如今,在共享臨時表空間中建立臨時表ibtmp1。
7 InnoDB 表空間加密功能支持重作日誌的加密和撤消日誌數據;
8 InnoDB支持 NOWAIT和SKIP LOCKED選項SELECT ... FOR SHARE以及SELECT ... FOR UPDATE鎖定讀取語句。 NOWAIT若是請求的行被另外一個事務鎖定,則會當即返回該語句。SKIP LOCKED從結果集中刪除鎖定的行SELECT ... FOR SHARE替換 SELECT ... LOCK IN SHARE MODE,但LOCK IN SHARE MODE仍可用於向後兼容;
9 支持ADD PARTITION,DROP PARTITION,COALESCE PARTITION,REORGANIZE PARTITION,和REBUILD PARTITION ALTER TABLE選項;
10 InnoDB存儲引擎如今使用MySQL的數據字典,而不是它本身的存儲引擎特定的數據字典;
11 mysql系統表和數據字典表如今在MySQL數據目錄中InnoDB命名的單個表空間文件建立mysql.ibd。之前,這些表是InnoDB在mysql數據庫目錄中的各個表空間文件中建立的;
二. Mysql8.0安裝 (YUM方式)
1) 首先刪除系統默認或以前可能安裝的其餘版本的mysql [root@DB-node01 ~]# for i in $(rpm -qa|grep mysql);do rpm -e $i --nodeps;done [root@DB-node01 ~]# rm -rf /var/lib/mysql && rm -rf /etc/my.cnf 2) 安裝Mysql8.0 的yum資源庫 mysql80-community-release-el7-1.noarch.rpm 下載地址: https://pan.baidu.com/s/1QzYaSnzAQeTqAmk8FE9doA 提取密碼: 2maw [root@DB-node01 ~]# yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm 3) 安裝Mysql8.0 [root@DB-node01 ~]# yum install mysql-community-server 啓動MySQL服務器和MySQL的自動啓動 [root@DB-node01 ~]# systemctl start mysqld [root@DB-node01 ~]# systemctl enable mysqld 4) 使用默認密碼初次登陸後, 必需要重置密碼 查看默認密碼, 以下默認密碼爲"e53xDalx.*dE" [root@DB-node01 ~]# grep 'temporary password' /var/log/mysqld.log 2019-03-06T01:53:19.897262Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: e53xDalx.*dE [root@DB-node01 ~]# mysql -pe53xDalx.*dE ............ mysql> select version(); ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. 報錯提示必需要重置初始密碼, 下面開始重置mysql登陸密碼(注意要切換到mysql數據庫,使用use mysql) mysql> use mysql; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 這個其實與validate_password_policy的值有關, mysql8.0更改了validate_password_policy相關的配置名稱, 這跟Mysql5.7有點不同了. mysql> set global validate_password.policy=0; Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password.length=1; Query OK, 0 rows affected (0.00 sec) 接着再修改密碼 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.05 sec) mysql> flush privileges; Query OK, 0 rows affected (0.03 sec) 退出, 從新使用新密碼登陸mysql [root@DB-node01 ~]# mysql -p123456 ........... mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.15 | +-----------+ 1 row in set (0.00 sec) 查看服務端口 mysql> show global variables like 'port'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ 1 row in set (0.01 sec) 查看mysql鏈接的受權信息 mysql> select host,user,password from mysql.user; ERROR 1054 (42S22): Unknown column 'password' in 'field list' 上面這是mysql5.6及如下版本的查看命令, mysql5.7以後的數據庫裏mysql.user表裏已經沒有password這個字段了,password字段改爲了authentication_string。 mysql> select host,user,authentication_string from mysql.user; +-----------+------------------+------------------------------------------------------------------------+ | host | user | authentication_string | +-----------+------------------+------------------------------------------------------------------------+ | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | root | $A$005${7J0=4Dc7Jym8eI/FU4jimKWFvkD9XmoAkF1ca5.Un0bc6zgmPtU.0 | +-----------+------------------+------------------------------------------------------------------------+ 4 rows in set (0.00 sec) =============================mysql8.0修改用戶密碼命令================================ mysql> use mysql; mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; mysql> flush privileges;
三. Mysql8.0安裝 (二進制方式)
1) 首先刪除系統默認或以前可能安裝的其餘版本的mysql [root@mysql8-node ~]# for i in $(rpm -qa|grep mysql);do rpm -e $i --nodeps;done [root@mysql8-node ~]# rm -rf /var/lib/mysql && rm -rf /etc/my.cnf 2) 安裝須要的軟件包 [root@mysql8-node ~]# yum -y install libaio [root@mysql8-node ~]# yum -y install net-tools 3) 下載並安裝Mysql8.0.12 下載地址: https://pan.baidu.com/s/1LyXrkrCPP7QKLrWYbLRBlw 提取密碼: emmf [root@mysql8-node ~]# groupadd mysql [root@mysql8-node ~]# useradd -g mysql mysql [root@mysql8-node ~]# cd /usr/local/src/ [root@mysql-node src]# ll -rw-r--r-- 1 root root 620389228 Aug 22 2018 mysql8.0.12_bin_centos7.tar.gz [root@mysql-node src]# tar -zvxf mysql8.0.12_bin_centos7.tar.gz [root@mysql-node src]# mv mysql /usr/local/ [root@mysql-node src]# chown -R mysql.mysql /usr/local/mysql [root@mysql-node src]# vim /home/mysql/.bash_profile export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH [root@mysql-node src]# source /home/mysql/.bash_profile [root@mysql-node src]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile [root@mysql-node src]# source /etc/profile 4) 建立數據目錄 [root@mysql-node src]# mkdir -p /data/mysql/{data,log,binlog,conf,tmp} [root@mysql-node src]# chown -R mysql.mysql /data/mysql 5) 配置mysql [root@mysql-node src]# su - mysql [mysql@mysql-node ~]$ vim /data/mysql/conf/my.cnf [mysqld] lower_case_table_names = 1 user = mysql server_id = 1 port = 3306 default-time-zone = '+08:00' enforce_gtid_consistency = ON gtid_mode = ON binlog_checksum = none default_authentication_plugin = mysql_native_password datadir = /data/mysql/data pid-file = /data/mysql/tmp/mysqld.pid socket = /data/mysql/tmp/mysqld.sock tmpdir = /data/mysql/tmp/ skip-name-resolve = ON open_files_limit = 65535 table_open_cache = 2000 #################innodb######################## innodb_data_home_dir = /data/mysql/data innodb_data_file_path = ibdata1:512M;ibdata2:512M:autoextend innodb_buffer_pool_size = 12000M innodb_flush_log_at_trx_commit = 1 innodb_io_capacity = 600 innodb_lock_wait_timeout = 120 innodb_log_buffer_size = 8M innodb_log_file_size = 200M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 85 innodb_read_io_threads = 8 innodb_write_io_threads = 8 innodb_thread_concurrency = 32 innodb_file_per_table innodb_rollback_on_timeout innodb_undo_directory = /data/mysql/data innodb_log_group_home_dir = /data/mysql/data ###################session########################### join_buffer_size = 8M key_buffer_size = 256M bulk_insert_buffer_size = 8M max_heap_table_size = 96M tmp_table_size = 96M read_buffer_size = 8M sort_buffer_size = 2M max_allowed_packet = 64M read_rnd_buffer_size = 32M ############log set################### log-error = /data/mysql/log/mysqld.err log-bin = /data/mysql/binlog/binlog log_bin_index = /data/mysql/binlog/binlog.index max_binlog_size = 500M slow_query_log_file = /data/mysql/log/slow.log slow_query_log = 1 long_query_time = 10 log_queries_not_using_indexes = ON log_throttle_queries_not_using_indexes = 10 log_slow_admin_statements = ON log_output = FILE,TABLE master_info_file = /data/mysql/binlog/master.info 6) 初始化 (稍等一下子, 能夠到/data/mysql/log/mysqld.err日子裏查看初始化過程, 看看有沒有error信息) [mysql@mysql-node ~]$ mysqld --defaults-file=/data/mysql/conf/my.cnf --initialize-insecure --user=mysql 7) 啓動mysqld [mysql@mysql-node ~]$ mysqld_safe --defaults-file=/data/mysql/conf/my.cnf & [mysql@mysql-node ~]$ lsof -i:3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 24743 mysql 23u IPv6 23132988 0t0 TCP *:mysql (LISTEN) 8) 登陸mysql, 重置密碼 本地首次使用sock文件登陸mysql是不須要密碼的 [mysql@mysql-node ~]# mysql -S /data/mysql/tmp/mysqld.sock ............. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.07 sec) mysql> flush privileges; Query OK, 0 rows affected (0.03 sec) mysql> select host,user,authentication_string from mysql.user; +-----------+------------------+------------------------------------------------------------------------+ | host | user | authentication_string | +-----------+------------------+------------------------------------------------------------------------+ | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +-----------+------------------+------------------------------------------------------------------------+ 4 rows in set (0.00 sec) 退出, 此時密碼重置後, 就不能使用sock文件無密碼登陸了 [root@mysql-node ~]# mysql -S /data/mysql/tmp/mysqld.sock ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) [root@mysql-node ~]# mysql -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 作sock文件的軟連接 [root@mysql-node ~]# ln -s /data/mysql/tmp/mysqld.sock /tmp/mysql.sock [root@mysql-node ~]# mysql -p123456 或者 [root@mysql-node ~]# mysql -uroot -S /data/mysql/tmp/mysqld.sock -p123456 ............. mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.12 | +-----------+ 1 row in set (0.00 sec) #授予用戶權限. 必須先要建立用戶, 才能受權!! (建立用戶時要帶@並指定地址, 則grant受權時的地址就是這個@後面指定的!, 不然grant受權就會報錯!) mysql> create user 'kevin'@'%' identified by '123456'; Query OK, 0 rows affected (0.11 sec) mysql> grant all privileges on *.* to 'kevin'@'%' with grant option; Query OK, 0 rows affected (0.21 sec) mysql> select host,user,authentication_string from mysql.user; +-----------+------------------+------------------------------------------------------------------------+ | host | user | authentication_string | +-----------+------------------+------------------------------------------------------------------------+ | % | kevin | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +-----------+------------------+------------------------------------------------------------------------+ 5 rows in set (0.00 sec) mysql> update mysql.user set host='172.16.60.%' where user="kevin"; Query OK, 1 row affected (0.16 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.05 sec) mysql> select host,user,authentication_string from mysql.user; +-------------+------------------+------------------------------------------------------------------------+ | host | user | authentication_string | +-------------+------------------+------------------------------------------------------------------------+ | 172.16.60.% | kevin | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +-------------+------------------+------------------------------------------------------------------------+ 5 rows in set (0.00 sec) mysql> create user 'bobo'@'172.16.60.%' identified by '123456'; Query OK, 0 rows affected (0.09 sec) mysql> grant all privileges on *.* to 'bobo'@'172.16.60.%'; Query OK, 0 rows affected (0.17 sec) mysql> flush privileges; Query OK, 0 rows affected (0.04 sec) mysql> select host,user,authentication_string from mysql.user; +-------------+------------------+------------------------------------------------------------------------+ | host | user | authentication_string | +-------------+------------------+------------------------------------------------------------------------+ | 172.16.60.% | bobo | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | 172.16.60.% | kevin | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +-------------+------------------+------------------------------------------------------------------------+ 6 rows in set (0.00 sec) mysql> show grants for kevin@'172.16.60.%'; +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Grants for kevin@172.16.60.% | +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `kevin`@`172.16.60.%` WITH GRANT OPTION | +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
MySQL單機多實例安裝配置
經過上面二進制部署可知, 已經起來一個3306端口的MySQL實例, 如今須要再起來兩個實例, 分別爲3307, 3308. 操做以下:
建立實例的數據目錄 [root@mysql-node ~]# mkdir -p /data/mysql3307/{data,log,binlog,conf,tmp} [root@mysql-node ~]# mkdir -p /data/mysql3308/{data,log,binlog,conf,tmp} [root@mysql-node ~]# chown -R mysql.mysql /data/mysql3307 [root@mysql-node ~]# chown -R mysql.mysql /data/mysql3308 配置mysql [root@mysql-node ~]# cp -r /data/mysql/conf/my.cnf /data/mysql3307/conf/ [root@mysql-node ~]# cp -r /data/mysql/conf/my.cnf /data/mysql3308/conf/ [root@mysql-node ~]# sed -i 's#/data/mysql/#/data/mysql3307/#g' /data/mysql3307/conf/my.cnf [root@mysql-node ~]# sed -i 's#/data/mysql/#/data/mysql3308/#g' /data/mysql3308/conf/my.cnf [root@mysql-node ~]# sed -i 's/3306/3307/g' /data/mysql3307/conf/my.cnf [root@mysql-node ~]# sed -i 's/3306/3308/g' /data/mysql3308/conf/my.cnf [root@mysql-node ~]# chown -R mysql.mysql /data/mysql* 進行初始化兩個實例 [root@mysql-node ~]# mysqld --defaults-file=/data/mysql3307/conf/my.cnf --initialize-insecure --user=mysql [root@mysql-node ~]# mysqld --defaults-file=/data/mysql3308/conf/my.cnf --initialize-insecure --user=mysql 接着啓動mysqld [root@mysql-node ~]# mysqld_safe --defaults-file=/data/mysql3307/conf/my.cnf & [root@mysql-node ~]# mysqld_safe --defaults-file=/data/mysql3308/conf/my.cnf & [root@mysql-node ~]# ps -ef|grep mysql mysql 23996 1 0 14:37 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/conf/my.cnf mysql 24743 23996 0 14:38 ? 00:00:17 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/conf/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/data/mysql/log/mysqld.err --open-files-limit=65535 --pid-file=/data/mysql/tmp/mysqld.pid --socket=/data/mysql/tmp/mysqld.sock --port=3306 root 30473 23727 0 15:33 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql3307/conf/my.cnf mysql 31191 30473 17 15:33 pts/0 00:00:02 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql3307/conf/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql3307/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql3307/log/mysqld.err --open-files-limit=65535 --pid-file=/data/mysql3307/tmp/mysqld.pid --socket=/data/mysql3307/tmp/mysqld.sock --port=3307 root 31254 23727 0 15:33 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql3308/conf/my.cnf mysql 31977 31254 39 15:33 pts/0 00:00:02 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql3308/conf/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql3308/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql3308/log/mysqld.err --open-files-limit=65535 --pid-file=/data/mysql3308/tmp/mysqld.pid --socket=/data/mysql3308/tmp/mysqld.sock --port=3308 root 32044 23727 0 15:34 pts/0 00:00:00 grep --color=auto mysql [root@mysql-node ~]# lsof -i:3307 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 31191 mysql 22u IPv6 23144844 0t0 TCP *:opsession-prxy (LISTEN) [root@mysql-node ~]# lsof -i:3308 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 31977 mysql 22u IPv6 23145727 0t0 TCP *:tns-server (LISTEN) [root@mysql-node ~]# lsof -i:3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 24743 mysql 23u IPv6 23132988 0t0 TCP *:mysql (LISTEN) 登陸3307端口實例, 並設置密碼 [root@mysql-node ~]# mysql -S /data/mysql3307/tmp/mysqld.sock ............ mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.11 sec) mysql> flush privileges; Query OK, 0 rows affected (0.11 sec) 退出, 使用新密碼登陸 [root@mysql-node ~]# mysql -uroot -S /data/mysql3307/tmp/mysqld.sock -p123456 ............. mysql> 同理, 登陸3308端口實例, 並設置密碼 [root@mysql-node ~]# mysql -S /data/mysql3308/tmp/mysqld.sock ........... mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.13 sec) mysql> flush privileges; Query OK, 0 rows affected (0.03 sec) 退出, 使用新密碼登陸 [root@mysql-node ~]# mysql -uroot -S /data/mysql3308/tmp/mysqld.sock -p123456 .................... mysql> ========================================== 3306, 3307, 3308三個端口實例的啓動命令分別爲: mysqld_safe --defaults-file=/data/mysql/conf/my.cnf & mysqld_safe --defaults-file=/data/mysql3307/conf/my.cnf & mysqld_safe --defaults-file=/data/mysql3308/conf/my.cnf & 登陸命令分別爲: mysql -uroot -S /data/mysql/tmp/mysqld.sock -p123456 mysql -uroot -S /data/mysql3307/tmp/mysqld.sock -p123456 mysql -uroot -S /data/mysql3308/tmp/mysqld.sock -p123456
編寫mysql多端口實例的登陸腳本
上面的多端口實例配置以後, 查看一個登陸腳本:myin (注意將腳本中的密碼改成本身的密碼) [root@mysql-node ~]# ll /usr/local/mysql/bin/myin -rwxrwxr-x 1 mysql mysql 161 Aug 22 2018 /usr/local/mysql/bin/myin [root@mysql-node ~]# cat /usr/local/mysql/bin/myin #!/bin/bash p=$1 shift mysql -h"127.0.0.1" -P"$p" --default-character-set=utf8mb4 --show-warnings -uroot -p'123456' -A --prompt="(\u@\p)[\d]> " "$@" 嘗試登陸 [mysql@mysql-node ~]# myin 3306 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1130 (HY000): Host '127.0.0.1' is not allowed to connect to this MySQL server 這是由於各個端口實例中的root用戶受權的是localhost, 而不是127.0.0.1, 則能夠再次添加一個針對127.0.0.1的root用戶受權 或者將myin腳本中的127.0.0.1 修改成 localhost [root@mysql-node ~]# sed -i 's/127.0.0.1/localhost/g' /usr/local/mysql/bin/myin [root@mysql-node ~]# cat /usr/local/mysql/bin/myin #!/bin/bash p=$1 shift mysql -h"localhost" -P"$p" --default-character-set=utf8mb4 --show-warnings -uroot -p'123456' -A --prompt="(\u@\p)[\d]> " "$@" 則如今能夠登陸各個實例了, 登陸命令爲"myin port" 登陸3306端口實例 [root@mysql-node ~]# myin 3306 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 27 Server version: 8.0.12 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. (root@mysql.sock)[(none)]> 登陸3307端口實例 [root@mysql-node ~]# myin 3307 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 28 Server version: 8.0.12 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. (root@mysql.sock)[(none)]> 登陸3308端口實例 [root@mysql-node ~]# myin 3308 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 29 Server version: 8.0.12 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. (root@mysql.sock)[(none)]>
生產環境下的思路: 同一個業務庫單獨放在一個實例下, 不要混用數據庫. 單機多實例間能夠作主從或主主同步, 也能夠在多機多實例之間坐主從或主主同步.
好比:
A機器有三個實例: 3310, 3311, 3312
B機器有三個實例: 3310, 3311, 3312
C機器有三個實例: 3310, 3311, 3312
則三臺機器的每一個對應的端口實例之間作主從或主主同步(GTID, MGR), 一主兩從, 或三主模式. (多實例環境最好不要用3306端口, 不然常常會出現一些誤操做)
四. Mysql8.0使用過程當中踩過的一些坑
1) 建立用戶和受權
在mysql8.0建立用戶和受權和以前不太同樣了,其實嚴格上來說,也不能說是不同, 只能說是更嚴格, mysql8.0須要先建立用戶(建立用戶時要帶@並指定地址, 則grant受權時的地址就是這個@後面指定的!, 不然grant受權就會報錯!)和設置密碼,而後才能受權。
mysql> create user 'kevin'@'%' identified by '123456'; Query OK, 0 rows affected (0.04 sec) mysql> grant all privileges on *.* to 'kevin'@'%' with grant option; Query OK, 0 rows affected (0.04 sec) mysql> create user 'bobo'@'%' identified by '123456'; Query OK, 0 rows affected (0.06 sec) mysql> grant all privileges on *.* to 'bobo'@'%' with grant option; Query OK, 0 rows affected (0.03 sec) mysql> flush privileges; Query OK, 0 rows affected (0.04 sec) mysql> select host,user,authentication_string from mysql.user; +-----------+------------------+------------------------------------------------------------------------+ | host | user | authentication_string | +-----------+------------------+------------------------------------------------------------------------+ | % | bobo | $A$005$1VY")q?G6<^X@-6LsXrPt5C0TwlTuvHbaOa3sYF0DKViIGoRPuCF8AzwiFcim1 | | % | kevin | $A$005$hy`U}ZB#R::rA8W0y2rmwgySqzv0rmR1eTeNDSaXfQPWIsrh7ytbVdi85 | | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | root | $A$005$/VO_y^7,]6;2qxggBLmJzhA0Qylu5/AHuRScZ/ykKedgZKh/6krOIzPs2 | +-----------+------------------+------------------------------------------------------------------------+
若是仍是用Mysql5.7及以前版本的直接受權的方法, 會有報錯:
mysql> grant all privileges on *.* to 'shibo'@'%' identified by '123456'; 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 '123456'' at line 1
2) Mysql8.0默認是不能使用root帳號進行遠程登陸的! root帳號只能本地登陸!
mysql> select host,user,authentication_string from mysql.user; +-----------+------------------+------------------------------------------------------------------------+ | host | user | authentication_string | +-----------+------------------+------------------------------------------------------------------------+ | % | bobo | $A$005$1VY")q?G6<^X@-6LsXrPt5C0TwlTuvHbaOa3sYF0DKViIGoRPuCF8AzwiFcim1 | | % | kevin | $A$005$hy`U}ZB#R::rA8W0y2rmwgySqzv0rmR1eTeNDSaXfQPWIsrh7ytbVdi85 | | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | root | $A$005$/VO_y^7,]6;2qxggBLmJzhA0Qylu5/AHuRScZ/ykKedgZKh/6krOIzPs2 | +-----------+------------------+------------------------------------------------------------------------+ 6 rows in set (0.00 sec) 若是想要遠程登陸, 則須要進行update更新下root帳號的權限 mysql> update mysql.user set host='%' where user="root"; Query OK, 1 row affected (0.10 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.14 sec) mysql> select host,user,authentication_string from mysql.user; +-----------+------------------+------------------------------------------------------------------------+ | host | user | authentication_string | +-----------+------------------+------------------------------------------------------------------------+ | % | bobo | $A$005$1VY")q?G6<^X@-6LsXrPt5C0TwlTuvHbaOa3sYF0DKViIGoRPuCF8AzwiFcim1 | | % | kevin | $A$005$hy`U}ZB#R::rA8W0y2rmwgySqzv0rmR1eTeNDSaXfQPWIsrh7ytbVdi85 | | % | root | $A$005$/VO_y^7,]6;2qxggBLmJzhA0Qylu5/AHuRScZ/ykKedgZKh/6krOIzPs2 | | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | +-----------+------------------+------------------------------------------------------------------------+ 6 rows in set (0.00 sec)
這樣就能在遠程使用root帳號登陸該mysql8.0的數據庫了
3) 修改root帳號權限, 容許root帳號遠程登陸後, 用navicat進行mysql的遠程鏈接時,出現了彈窗報錯:
出現這個緣由是mysql8 以前的版本中加密規則是mysql_native_password, 而在mysql8以後,加密規則是caching_sha2_password, 解決問題方法有兩種:
1) 一種是升級navicat驅動;
2) 一種是把mysql用戶登陸密碼加密規則還原成mysql_native_password;
這裏選擇第二種方法來解決:
#修改加密規則 mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; Query OK, 0 rows affected (0.16 sec) #更新一下用戶的密碼 mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; Query OK, 0 rows affected (0.08 sec) #刷新權限 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.03 sec)
這樣問題就解決了。
4) sqlyog連接時出現2058異常
1) 使用sqlyog連接時會出現2058的異常,此時咱們須要修改mysql,命令行登陸mysql(與修改密碼中登陸相同,使用修改後的密碼),而後執行下面的命令: mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; 其中password爲本身修改的密碼。而後SQLyog中從新鏈接,則可鏈接成功,OK。 2) 若是報錯:ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'則使用下面命令: mysql > ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
5) 修改默認編碼方式
mysql8.0默認編碼方式爲utf8mb4,所以使用時不須要修改,可以使用以下命令查看: mysql > SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%'; 若是須要修改其餘編碼方式,好比須要修改成utf8mb4,可使用以下方式: 修改mysql配置文件my.cnf, 找到後請在如下三部分裏添加以下內容: [client] default-character-set = utf8mb4 [mysql] default-character-set = utf8mb4 [mysqld] character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4' 而後重啓mysqld服務便可, 其中: character_set_client (客戶端來源數據使用的字符集) character_set_connection (鏈接層字符集) character_set_database (當前選中數據庫的默認字符集) character_set_results (查詢結果字符集) character_set_server (默認的內部操做字符集) ======================================== 數據庫鏈接參數中: characterEncoding=utf8 會被自動識別爲utf8mb4,也能夠不加這個參數,會自動檢測。 而autoReconnect=true 是必須加上的。
6) 部分參數配置查詢命令
#查詢mysql最大鏈接數設置 mysql> show global variables like 'max_conn%'; mysql> SELECT @@MAX_CONNECTIONS AS 'Max Connections'; # 查看最大連接數 mysql> show global status like 'Max_used_connections'; # 查看慢查詢日誌是否開啓以及日誌位置 mysql> show variables like 'slow_query%'; # 查看慢查詢日誌超時記錄時間 mysql> show variables like 'long_query_time'; # 查看連接建立以及如今正在連接數 mysql> show status like 'Threads%'; # 查看數據庫當前連接 mysql> show processlist; # 查看數據庫配置 mysql> show variables like '%quer%';