虽然很奇怪,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
其实二进制包也没什么安装概念,都是拿来就用
-注意:操作前要确保没有其他mysql发行版干扰,特别是yum安装的和rpm安装,
#查看已安装的rpm包中又没有mysql和mariadb的安装包 rpm -qa |grep -E 'mysql|mariadb' mariadb-libs-5.5.44-2.el7.centos.x86_64 #删除查找出来的rpm安装包 rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64 rpm -e --nodeps mysql.x86_64
初始化使用
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* #使用自带mysql启动脚本的启动方式启动 /usr/local/mysql80/support-files/mysql.server start #使用mysqld的启动方式 /usr/local/mysql80/bin/mysqld --defaults-file=/usr/local/mysql80/my.cnf & #切换为mysql账户并关闭numa启动mysql进程,如果是主库更推荐这种启动方式 sudo su - mysql -s /bin/bash -c "numactl --interleave=all /usr/local/mysql80/bin/mysqld --defaults-file=/usr/local/mysql80/my.cnf " &
先用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> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password; #刷新一下生效 mysql> FLUSH PRIVILEGES; #或者在授权的时候就指定好 CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'your 123123;
然后我们再新建一个用户,就可以看到是和以前一样了
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'; #或者指定密码策略 CREATE USER 'sroot'@'%' IDENTIFIED WITH mysql_native_password 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引擎,其实之前也在特性里看到了.