卸载 MariaDB

centos7中默认安装了数据库MariaDB,如果直接安装MySQL的话,会直接覆盖掉这个数据库,也可以手动删除一下:

[root@localhost ~]# rpm -qa|grep mariadb  // 查询出来已安装的mariadb
[root@localhost ~]# rpm -e --nodeps 文件名  // 卸载mariadb,文件名为上述命令查询出来的文件

设置 MySQL 清华镜像站 yum

  • 本站下载 - MySQL 5.7

# wget -O /etc/yum.repos.d/mysql-community.repo https://file.cnxiaobai.com/Linux/MySQL/mysql-community-5.7.repo
  • 本站下载 - MySQL 8.0

# wget -O /etc/yum.repos.d/mysql-community.repo https://file.cnxiaobai.com/Linux/MySQL/mysql-community-8.0.repo
  • 或手动编辑

# vim /etc/yum.repos.d/mysql-community.repo
# 写入以下内容

[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-connectors-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql

[mysql-tools-community]
name=MySQL Tools Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql

[mysql-5.7-community]
name=MySQL 5.7 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql

image.png

  • 更新 yum 缓存

# yum check-update

安装 MySQL 服务

  • 安装MySQL RPM GPG KEY

# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
  • 安装MySQL 服务

# yum -y install mysql-community-server

修改 my.cnf 配置

  • 按实际修改即可

# vim /etc/my.cnf
binlog_cache_size = 192K
thread_stack = 384K
join_buffer_size = 4096K
key_buffer_size = 512M
sort_buffer_size = 2048K
read_buffer_size = 2048K
read_rnd_buffer_size = 1024K
thread_cache_size = 192
query_cache_size = 256M
tmp_table_size = 1024M
max_connections = 500
innodb_buffer_pool_size = 1024M
innodb_log_buffer_size = 512M

启停 MySQL 服务

  • 启动

# systemctl start mysqld
  • 关闭

# systemctl stop mysqld
  • 重启

# systemctl restart mysqld
  • 开机自启

# systemctl enable mysqld

登录MySQL

  第一次启动MySQL后,就会有临时密码,这个默认的初始密码在/var/log/mysqld.log文件中,可以用这个命令来查看:

# grep "password" /var/log/mysqld.log
# mysql -uroot -p

image.png

  • 用该密码登录后,必须马上修改新的密码,不然会报如下错误:

mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
  • 修改密码

  • 如果你想要设置一个简单的测试密码的话,比如设置为123456,会提示这个错误,报错的意思就是你的密码不符合要求

mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
  • validate_password_policy有以下取值:

905161539422.png

 默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。

 有时候,只是为了自己测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。

 必须修改两个全局参数:

  • 首先,修改validate_password_policy参数的值

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
  • validate_password_length(密码长度)参数默认为8,修改为1

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.00 sec)
  • 远程连接设置

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Your-password' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

人生如戏,全靠演技!