由于 CentOS6 自带的 MySQL 5.1 性能不行,使用过程中遇到了很多问题,(误)最关键的是,都不支持 utf8mb4,后台去检查数据老是遇到乱码,烦死了!mysql 5.5.3+ 才支持 utf8mb4,(逃…)因此我决定安装 MySQL 5.7 。

设置国内源

最近几年里,我给好几个系统安装过 MySQL,可是国外官网的速度实在是太瘆人了;后来才知道这软件居然也有国内源,用了之后心情都好很多了。

gedit /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-el6-$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-el6-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql

[mysql-5.6-community]
name=MySQL 5.6 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el6-$basearch/
enabled=0
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-el6-$basearch/
#baseurl=https://mirrors4.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el6-x86_64/
baseurl=https://mirrors.ustc.edu.cn/mysql-repo/yum/mysql-5.7-community/el/6/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql

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

安装程序

使用 yum 进行安装 MySQL

yum makecache
yum install mysql-community-libs-compat*
yum install mysql-community-server

一般情况下,MySQL包括以下各个包:

名字 摘要说明 备注
mysql-community-client MySQL客户端应用程序和工具
mysql-community-common 服务器和客户端库的通用文件
mysql-community-devel MySQL数据库客户端应用程序的开发头文件和库
mysql-community-embedded-compat MySQL服务器作为嵌入式库,与使用库版本18的应用程序兼容
mysql-community-libs MySQL数据库客户端应用程序的共享库
mysql-community-libs-compat 以前的MySQL安装的共享兼容性库
mysql-community-server 数据库服务器和相关工具
mysql-community-server-debug 调试服务器和插件二进制文件
mysql-community-test MySQL服务器的测试套件
mysql-community RPM的源代码类似于mysql-community-8.0.20-1.el7.src.rpm,具体取决于所选的OS

安装还会在系统上创建一个名为的用户 mysql和一个名为的组 mysql。

MySQL 的初始化配置

初始化配置

gedit /etc/my.cnf 设定 MySQL 的编码参数以及默认端口

[mysqld]
port    = 6033
character-set-server=utf8mb4
collation_server=utf8mb4_general_ci
default-time-zone = '+8:00'
explicit_defaults_for_timestamp=true

[client]
port    = 6033
default-character-set=utf8mb4

修改服务

设定 MySQL 开机启动

su
chkconfig --add mysqld
chkconfig mysqld on

# 启动 MySQL 服务
service mysqld start

账户和密码管理

  1. 查看 MySQL 默认密码
grep "temporary password" /var/log/mysqld.log
  1. 设定 root 密码,并添加普通用户 david
mysql -uroot -p
    # 本地
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'fs@DB135';
        # SET PASSWORD FOR 'root'@'localhost' = PASSWORD('fs@DB135');   
    # 远程
    ALTER USER 'root'@'%' IDENTIFIED BY 'fs@ABCD.135';

    # 普通用户 david
    CREATE USER 'david'@'localhost' IDENTIFIED BY 'dav#D2468';
    CREATE USER 'david'@'%' IDENTIFIED BY 'dav#D-124857';

    # 查看用户
    select user,host,password from mysql.user;

    exit

如果由于默认的密码强度校验导致本地使用的简单密码无法通过,可以在 MySQL 中使用 set global validate_password_policy=0; 临时关闭,不过,应当谨慎使用哦!

  1. 如果在使用过程中,需要修改账户的本地、远方访问权限,示例如下:
# 局域网访问:主库
grant replication slave on *.* to 'david'@'192.168.88.%' identified by 'dav#D2468' with grant option;
# 局域网访问:从库(在对端主机配置)
grant all privileges on *.* to 'david'@'192.168.88.%' identified by 'dav#D2468' with grant option;

# 远程访问(db1、db2为系统中的数据库名)
GRANT ALL PRIVILEGES ON db1.* TO 'david'@'%' IDENTIFIED BY 'dav.D2468-fs1' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON db2.* TO 'david'@'%' IDENTIFIED BY 'dav#D-124857' WITH GRANT OPTION;

# 本地访问(临时性修改密码策略,从而在保留原有授权的基础下,增加密码较为简单的访问)
set global validate_password_policy=0;
GRANT ALL ON db1.* TO 'david'@'localhost'   IDENTIFIED BY 'david.1357' WITH GRANT OPTION;
GRANT ALL ON db2.* TO 'david'@'127.0.0.%'   IDENTIFIED BY 'david.2468' WITH GRANT OPTION;

# 执行生效
flush privileges;
  1. 如果需要清除权限,示例如下:
mysql -u root -p
    select user,host, authentication_string from mysql.user;

    # 清除  delete 权限
    REVOKE delete ON *.* FROM 'david'@'localhost';
    revoke delete on *.* from 'david'@'%';

    # 清除 all 权限
    revoke all on *.* from 'david'@'localhost';
    REVOKE ALL PRIVILEGES ON *.* FROM 'david'@'%';

    # 执行生效
    FLUSH PRIVILEGES;
  1. 配套修改防火墙确保远程主机的访问,如下:
gedit /etc/sysconfig/iptables
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
    #-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 6033 -j ACCEPT
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
service iptables restart

安装故障排查

查看错误日志是最直接有效的,如果不指定,文件名则为主机名(此处为 hhjos6 ),如:

tail -f /var/lib/mysql/hhjos6.err

参考文献

  1. TUNA.Mysql Community Edition 镜像使用帮助[EB/OL].https://mirrors.cnnic.cn/help/mysql/, 2021/03/05.

  2. Oracle Corporation and/or its affiliates.MySQL Product Archives[EB/OL].https://downloads.mysql.com/archives/community/, 2021/03/05.