目录
- 一、检查系统是否安装其他版本Mariadb数据库
- 二、下载mysql安装包,并安装
- 三、安装完成后,初次安装执行安全配置向导
- 四、设置UTF-8编码
- 五、修改默认端口
- 六、修改默认数据库存储路径
- 七、开启远程连接
- 八、开放防火墙端口
- 九、开机自启动
- 十、大小写敏感设置
- 十一、修改密码的强度
- 十二、生产环境优化
- 十三、常用命令
- 十四、卸载Mysql
- 十五、参考网址
一、检查系统是否安装其他版本Mariadb数据库
注意:在新版本的CentOS7中,默认的数据库已更新为了Mariadb,而非 MySQL。
# 查看已安装的 Mariadb 数据库版本
rpm -qa|grep -i mariadb
# 卸载已安装的 Mariadb 数据库
rpm -qa|grep mariadb|xargs rpm -e --nodeps
# 再次查看已安装的 Mariadb 数据库版本,确认是否卸载完成
rpm -qa|grep -i mariadb
二、下载mysql安装包,并安装
# 2.1下载安装包文件
wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
# 2.2执行rpm命令
rpm -ivh mysql57-community-release-el7-10.noarch.rpm
# 2.3执行yum命令查看可用的 mysql 安装文件
yum repolist all | grep mysql
# 2.4防止GPG key报错,执行这个命令
rm /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
# 2.5安装mysql
yum install -y mysql-community-server
# 2.6检查mysql是否安装成功
rpm -qa | grep mysql
注意!如果报错:源 “MySQL 5.7 Community Server” 的 GPG 密钥已安装,但是不适用于此软件包。请检查源的公钥 URL 是否配置正确。
解决办法:
rm /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
三、安装完成后,初次安装执行安全配置向导
3.1启动mysql服务
# 启动服务
systemctl start mysqld
# 查看是否启动成功
systemctl status mysqld
3.2查看临时密码,MySQL5.7为root用户随机生成了一个密码
grep 'temporary password' /var/log/mysqld.log
3.3配置root信息
安装完成后执行【mysql_secure_installation】,设置内容如下:
- 为root用户设置密码
- 删除匿名账号
- 取消root用户远程登录
- 删除test库和对test库的访问权限
- 刷新授权表使修改生效
[root@centosServer ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: ##<– 输入mysqld.log里的临时密码
The existing password for the user account root has expired. Please set a new password.
New password: ##<– 输入新密码。要求:大小写字母、数字、特殊字符组合
Re-enter new password: ##<– 输入新密码。要求:大小写字母、数字、特殊字符组合
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y ##<– 选择是
New password: ##<– 输入新密码。要求:大小写字母、数字、特殊字符组合
Re-enter new password: ##<– 输入新密码。要求:大小写字母、数字、特殊字符组合
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y ##<– 是否删除匿名用户,生产环境建议删除,所以直接回车
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N ##<– 是否禁止root远程登录,根据需求选择,建议禁止
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N ##<– 是否删除test数据库,直接回车
... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : ##<– 是否重新加载权限表,直接回车
... skipping.
All done!
四、设置UTF-8编码
4.1登录root用户,查看mysql默认编码
[root@centosServer ~]# mysql -uroot -p
mysql> show variables like 'character%';
4.2编辑mysql的my.cnf文件,设置UTF-8编码
# 备份my.cnf文件
[root@centosServer ~]# cp -a /etc/my.cnf /etc/my_back.cnf
# 编辑my.cnf文件
[root@centosServer ~]# vi /etc/my.cnf
#设置编码内容
[mysqld]
# 设置编码
character-set-server=utf8
collation-server=utf8_general_ci
# 1.默认采用InnoDB存储引擎
default-storage-engine=INNODB
# 2.设置大小写敏感
lower_case_table_names=1
# 3.sql_mode定义了支持的sql语法、数据校验等
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[mysql]
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set = utf8
4.3重启mysql服务,查看验证编码
[root@centosServer ~]# systemctl restart mysqld
[root@centosServer ~]# mysql -uroot -p
mysql> show variables like 'character%';
五、修改默认端口
Centos 7 修改 Mysql 5.7 默认端口号
六、修改默认数据库存储路径
Centos 7 修改 Mysql 5.7 默认数据库存储路径
七、开启远程连接
# 登录root用户建立远程连接
[root@centosServer ~]# mysql -uroot -p
# 授权命令
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你设置的密码' WITH GRANT OPTION;
mysql> flush privileges;
八、开放防火墙端口
# 查询3306端口是否开放
firewall-cmd --query-port=3306/tcp
# 开放3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 重启
firewall-cmd --reload
# 查看防火墙是否放行3306端口
firewall-cmd --zone=public --list-ports
九、开机自启动
#开启开机启动
systemctl enable mysqld.service
#关闭开机启动
systemctl disable mysqld.service
#查看开机启动
systemctl list-unit-files | grep mysqld.service
十、大小写敏感设置
# 编辑my.cnf文件
vi /etc/my.cnf
# 插入
lower_case_table_names=1
十一、修改密码的强度
# 登录root用户建立远程连接
[root@centosServer ~]# mysql -uroot -p
#修改密码长度限制策略
mysql> set global validate_password_policy=0; #--表示将密码安全等级设置为low
mysql> set global validate_password_length=1; #--表示将密码长度设置为最小6位
#修改root用户密码为root
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
#重新授权远程登录
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
十二、生产环境优化
Centos 7 生产环境优化 Mysql 5.7
十三、常用命令
启动:# systemctl start mysqld.service
关闭:# systemctl stop mysqld.service
重启:# systemctl restart mysqld.service
状态:# systemctl status mysqld.service
查看版本:mysql -V
查看进程:ps -ef | grep mysqld
如果有mysqld_safe和mysqld两个进程,说明MySQL服务当前在启动状态;
十四、卸载Mysql
rpm -qa | grep mysql
yum -y remove mysql57-community-release-el7-10.noarch
十五、参考网址
安装报错:https://www.isres.com/linux/4749.html