在 CentOS7 中默认安装有 MariaDB,这个是 MySQL 的分支,但为了需要,还是要在系统中安装 MySQL,而且安装完成之后可以直接覆盖掉 MariaDB。
1. 下载并安装 MySQL 官方的 Yum Repository
[root@zhouhuoc /]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
使用上面的命令就直接下载了安装用的 Yum Repository;
[root@zhouhuoc /]# yum -y localinstallinstall mysql57-community-release-el7-10.noarch.rpm
使用 yum 安装 MySQL;
[root@zhouhuoc /]# yum -y install mysql-community-server
2. MySQL 数据库设置
编辑/etc/my.cnf 配置文件
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# 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=/data/mysql
socket=/data/mysql/mysql.sock
#端口
port = 3308
#字符集
character_set_server = utf8
#表名忽略大小写
lower_case_table_names = 1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/data/mysql/mysql.sock
启动 MySQL
[root@zhouhuoc /]# systemctl start mysqld
查看 mysql 默认密码
[root@zhouhuoc /]# grep "password" /var/log/mysqld.log
登录到 MySQL 数据库
[root@zhouhuoc ~]# mysql -uroot -p
修改初始密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
当密码过于简单时会报如下错误
原因是因为 MySQL 有密码设置的规范,具体是与 validate_password_policy 的值有关:
MySQL 完整的初始密码规则可以通过如下命令查看:
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| 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 |
+--------------------------------------+--------+
6 rows in set (0.00 sec)
密码的长度是由 validate_password_length 决定的,而 validate_password_length 的计算公式是:
validate_password_length = validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)
解决方法就是修改密码为规范复杂的密码或者修改密码规则
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql>
卸载 MySQL yum Repository
[root@zhouhuoc /]# yum -y remove mysql57-community-release-el7-10.noarch
开启 MySQL 远程登录权限
mysql> grant all on *.* to root@'%' identified by '数据库密码';
如果已开启防火墙,则开放端口或者关闭防火墙
- 关闭防火墙
[root@zhouhuoc /]# systemctl stop firewalld
[root@zhouhuoc /]# systemctl disablefirewalld
- 开放端口
[root@zhouhuoc /]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@zhouhuoc /]# firewall-cmd --reload
引用自:MySQL 安装之 yum 安装
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于