1:虚机:centos8
2:mysql安装包下载地址:
https://downloads.mysql.com/archives/community/
3:上传安装包到服务器目录并解压
tar -xvf mysql-8.0.34-1.el7.x86_64.rpm-bundle.tar
解压后的目录:
4:安装
rpm -ivh mysql-community-{server,client,common,libs,icu,debuginfo}-*
若报错:
error: Failed dependencies: mariadb-libs is obsoleted by mysql-community-libs-8.0.34-1.el7.x86_64 mariadb-libs is obsoleted by mysql-community-libs-compat-8.0.34-1.el7.x86_64
就先卸载
yum remove mysql-libs
再次安装
rpm -ivh mysql-community-{server,client,common,libs,icu,debuginfo}-*
5:修改配置文件
vim /etc/my.cnf
# 根据自己的需求进行配置
[mysqld]
port=3306
character-set-server=utf8mb4 # 设置编码格式
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
sql-mode=ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION # 严格模式
lower_case_table_names=1 # 不区分大小写
[mysql]
default-character-set=utf8mb4
[client]
default-character-set=utf8mb4
注:mysql8 之后 lower_case_table_names=1 一定要在初始化时设置,否则报错且无效。也就是配置了再启动。
6:启动
systemctl start mysqld # 启动
systemctl status mysqld # 查看状态
systemctl stop mysqld # 停止
systemctl enable mysqld # 设置开机自启
# 启动失败错误日志查看
cat /var/log/mysqld.log
7:获取临时密码
必须启动成功,才会生成此文件
grep "password" /var/log/mysqld.log
8:登录
mysql -u root -p
9:遇到问题
mysql> alter user 'root'@'localhost' identified by 'root';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password_length=1;
ERROR 1193 (HY000): Unknown system variable 'validate_password_length'
mysql> show variables like 'validate_password%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
9.1:解决方法:
mysql> alter user 'root'@'localhost' identified by '12345678Cc!'; # 注意密码规则
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; # 刷新权限规则
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'validate_password%';
+-------------------------------------------------+--------+
| Variable_name | Value |
+-------------------------------------------------+--------+
| validate_password.changed_characters_percentage | 0 |
| 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 |
+-------------------------------------------------+--------+
mysql> set global validate_password.length=4; # 减少密码长度要求
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.policy=0; # 取消密码验证策略
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'validate_password%';
+-------------------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------------------+-------+
| validate_password.changed_characters_percentage | 0 |
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 4 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | LOW |
| validate_password.special_char_count | 1 |
+-------------------------------------------------+-------+
mysql> alter user 'root'@'localhost' identified by '新密码';
Query OK, 0 rows affected (0.06 sec)
mysql> exit # 退出
Bye
mysql -u root -p # 使用新密码进行登录
10:远程登录
远程访问报 is not allowed to connect to this MySQL server
解决:
mysql -u root -p # 在服务器登录
mysql> use mysql; # 切换数据库
mysql> select host from user where user='root'; #查看
+-----------+
| host |
+-----------+
| localhost |
+-----------+
1 row in set (0.00 sec)
mysql> update user set host = '%' where user ='root'; # 修改权限
Query OK, 1 row affected (0.06 sec)
mysql> flush privileges; # 刷新权限规则
11:卸载
# 停用服务
systemctl stop mysqld
# 查看 mysql安装情况
rpm -qa | grep -i mysql
# 有则移除
ps: yum remove mysql-community*
# 查看相关文件夹
find / -name mysql
# 有则移除
rm -rf /var/lib/mysql
# 排查是否删除干净
rpm -qa | grep -i mysql