LinuxCentos7下安装Mysql8.x以及密码修改
LinuxCentos7下安装Mysql以及密码修改
引言:
之前都是用Docker或者yum自动安装,这次主要是下载压缩包解压安装,中间也有些小波折,记录如下,以供参考:
1.删除旧的MySQL
检查MariaDB:
[root@localhost local]# rpm -qa|grep mariadb
mariadb-libs-5.5.65-1.el7.x86_64
删除MariaDB:
[root@localhost local]# rpm -e --nodeps mariadb-libs
[root@localhost local]# rpm -qa|grep mariadb
下载MySQL安装包
MySQL官网:https://dev.mysql.com/downloads/mysql/
点进去之后下载对应的版本,这儿需要注意自己Linux的版本以及CPU指令集
通常Centos都是Red Hat Enterprise公司的,Linux还有Ubuntu、Debian等公司也是Linux的版本,需要注意,X86是指X86指令集,也就是通常所说的32位,而X86-64又叫X64,是64位.需要了解清楚自己电脑的系统配置.
2、安装MySQL
下载之后,利用Xftp工具传输进Linux对应的文件夹,之后运行tar命令进行解压:
[root@localhost mysql]# tar -xvf mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.22-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm
mysql-community-common-8.0.22-1.el7.x86_64.rpm
mysql-community-devel-8.0.22-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.22-1.el7.x86_64.rpm
mysql-community-libs-8.0.22-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.22-1.el7.x86_64.rpm
mysql-community-server-8.0.22-1.el7.x86_64.rpm
mysql-community-test-8.0.22-1.el7.x86_64.rpm
安装这些rpm文件,他们之间有依赖关系,要注意先后顺序,我们主要安装的是这四个(如果有需要也可以一并安装其它的):
mysql-community-common-8.0.22-1.el7.x86_64.rpm
mysql-community-libs-8.0.22-1.el7.x86_64.rpm
mysql-community-client-8.0.22-1.el7.x86_64.rpm
mysql-community-server-8.0.22-1.el7.x86_64.rpm
安装过程:
shell> rpm -ivh mysql-community-common-8.0.22-1.el7.x86_64.rpm
shell> rpm -ivh mysql-community-libs-8.0.22-1.el7.x86_64.rpm
shell> rpm -ivh mysql-community-client-8.0.22-1.el7.x86_64.rpm
shell> rpm -ivh mysql-community-server-8.0.22-1.el7.x86_64.rpm
也可以用一种简单的方式,自动处理各个包之间的依赖关系并自动下载缺少的依赖:
shell> yum install mysql-community-{server,client,common,libs}-*
注意:上面的yum install
命令需要在tar解压之后的各个rpm包所在目录内执行,否则就变成yum方式安装了,需要配置MySQL的yum源并且速度很慢,还要当前机器支持外网访问
3.密码等相关设置
1.密码修改
Mysql安装之后会设置默认的密码,通过以下指令获得:
[root@localhost mysql]# sudo grep 'temporary password' /var/log/mysqld.log
2020-12-02T05:29:12.308459Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: QFl6TJm;-Pbx
后面的"QFl6TJm;-Pbx"就是默认登录密码,先登录进MySQL:
[root@localhost mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.22
这时候如果直接修改密码,会因为密码过于简单报错:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
如果直接查看修改密码检验规则也会报错:
mysql> SHOW VARIABLES LIKE 'validate_password%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
可以先修改默认密码,将"QFl6TJm;-Pbx"修改一个字符变成"QFl6TJm;-Pbb",然后再查看密码检验规则:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'QFl6TJm;-PbB';
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | MEDIUM |
| validate_password.special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
将检验规则进行修改:
mysql> SET GLOBAL validate_password.length = 5;
Query OK, 0 rows affected (0.00 sec)
mysql>SET GLOBAL validate_password.policy = low;
再修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
2.允许root远程访问
从MySQL 8开始,不再使用该GRANT
命令创建用户。改用CREATE USER,然后使用GRANT语句:
mysql> CREATE USER 'root'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
3.设置编码为utf8
mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.00 sec)
MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode。好在utf8mb4是utf8的超集,除了将编码改为utf8mb4外不需要做其他转换。当然,为了节省空间,一般情况下使用utf8足够,为此还是设置成utf8编码:
mysql> set character_set_client=utf8;
mysql> set character_set_connection=utf8;
mysql> set character_set_database=utf8;
mysql> set character_set_results =utf8;
mysql> set character_set_server=utf8;
关于utf8与utf8mb4区别,可以参考这篇文章:MySQL中 utf8与utf8mb4的区别
4.设置开机启动
shell> systemctl enable mysqld
shell> systemctl daemon-reload