目录
1.2安装完成后,使用mysqladmin --version 或者 mysql --version查看数据库版本如下:
环境概述:
- CentOS8.x采用最小安装
- 系统为:CentOS8.0.1905
- 最终安装MySQL版本:8.0.17
一、安装
1.1 使用MySQL的官方repo源,安装MySQL8
cd /usr/local/src
# 下载MySQL的官方repo源
wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
# 安装Yum Repository
rpm -ivh mysql57-community-release-el7-11.noarch.rpm
# 安装MySQL服务器
yum -y install mysql-server mysql-devel #使用该条命令!
1.2安装完成后,使用mysqladmin --version 或者 mysql --version
查看数据库版本如下:
[root@node2 src]# mysqladmin --version
mysqladmin Ver 8.0.21 for Linux on x86_64 (Source distribution)
1.3 连接数据库,第一次安装完后默认没有密码
[root@node2 src]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21 Source distribution
Copyright (c) 2000, 2020, 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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update user set authentication_string="" where user="root";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'tantop-scm-root';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| 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/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.01 sec)
mysql> quit
Bye
二、设置MySQL8数据库密码
1、启动MySQL
systemctl start mysqld
2、修改MySQL密码并允许远程连接
在
/etc/my.cnf
文件最后一行添加skip-grant-tables
,重启mysql(systemctl restart mysqld),跳过登入权限验证;
使用mysql -u root
登陆
mysql> use mysql;
mysql> update user set host='%' where user='root';
mysql> update user set authentication_string="" where user="root";
mysql> flush privileges;
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
mysql> flush privileges;
修改完成后,退出重新登陆MySQL时,即可使用新密码!
三、字符集和时区
1、使用新的密码登陆MySQL查看字符集与时区
# 查看字符集
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| 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/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
装完MySQL之后默认字符集为utf8mb4
2、修改时区
# 查看时区
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.01 sec)
CST
UTC是协调世界时(Universal Time Coordinated)
GMT(Greenwich Mean Time)是格林尼治平时
而CST却同时可以代表如下 4 个不同的时区:
美国中部时间:Central Standard Time (USA) UT-6:00
澳大利亚中部时间:Central Standard Time (Australia) UT+9:30
中国标准时间:China Standard Time UT+8:00
古巴标准时间:Cuba Standard Time UT-4:00
在该文件/etc/my.cnf.d/mysql-server.cnf
中的 [mysqld] 下面添加时区
# 东八区时区,北京时区
default-time-zone = '+8:00'
3、重启数据库
systemctl restart mysqld
再次查看时区如下:
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | +08:00 |
+------------------+--------+
2 rows in set (0.01 sec)
mysql> quit
东八区时区
四、移除MySQL的repo源
为了防止每次进行yum操作时其自动更新,移除MySQL的repo。
yum -y remove mysql57-community-release-el7-11.noarch
五、MySQL自启
# 查看MySQL是否自启
systemctl is-enabled mysqld
# 开启自启
systemctl enable mysqld
# 关闭自启
systemctl disable mysqld
如下如,为
enabled
时,MySQL为自启(安装MySQL之后,默认为不自启,需要手动开启)
[root@node2 src]# systemctl is-enabled mysqld
disabled
[root@node2 src]#
查看MySQL是否自启
[root@node2 src]# systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@node2 src]# systemctl is-enabled mysqld
enabled
[root@node2 src]#
六、防火墙放行3306端口
如果你的防火墙已经关闭,可忽略此操作。
# 永久开放3306端口
firewall-cmd --permanent --zone=public --add-port=3306/tcp
# 重启防火墙
systemctl restart firewalld
# 检测设定是否生效
iptables -L -n | grep 3306