CentOS 7 安装配置MySQL
环境
- CentOS Linux release 7.5.1804 (Core)
- MySQL:mysql80-community-release-el7-1
检查:
在centos7中默认的是mariadb,先检查系统中是否有MySQL的安装
rpm -qa | grep mysql
[linga@localhost ~]$ rpm -qa | grep mysql
[linga@localhost ~]$
这里返回空值,说明是没有安装源
下载安装MySQL源
先进入本机的源文件目录:
cd /usr/local/src
下载MySQL的reop源(这里用的是MySQL8.0版本的,官网介绍说比7.0块2倍):
weget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
安装rpm包:
安装MySQL
使用yum源来安装mysql-server:
yum install -y mysql-server
启动mysql服务:
systemctl start mysqld
设置开机启动MySQL服务:
systemctl enable mysqld
修改root本地登录密码
在日志中查找密码
mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码:
cat /var/log/mysqld.log
[linga@localhost ~]$ cat /var/log/mysqld.log I grep temporary
[linga@localhost ~]$ 2018-08-28T14:51:41.543460Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: S0rlNA:RDvu#
找到默认密码是:S0rlNA:RDvu#,然后登陆数据库:
mysql -u root -p
修改密码
输入找到的默认密码,如果要操作数据库时提示:
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
提示通过使用ALTER USER 语法来修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxxxxxx';
或者:
set password for 'root'@'localhost'=password('xxxxxxxxxx');
注意:
mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误
通过msyql环境变量可以查看密码策略的相关信息:
show variables like '%password%';
mysql> show variables like '%password%';
+----------------------------------------------+-----------------+
| Variable_name | Value |
+----------------------------------------------+-----------------+
| caching_sha2_password_auto_generate_rsa_keys | ON |
| caching_sha2_password_private_key_path | private_key.pem |
| caching_sha2_password_public_key_path | public_key.pem |
| default_password_lifetime | 0 |
| disconnect_on_expired_password | ON |
| mysql_native_password_proxy_users | OFF |
| password_history | 0 |
| password_reuse_interval | 0 |
| report_password | |
| sha256_password_auto_generate_rsa_keys | ON |
| sha256_password_private_key_path | private_key.pem |
| sha256_password_proxy_users | OFF |
| sha256_password_public_key_path | public_key.pem |
| 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 |
+----------------------------------------------+-----------------+
20 rows in set (0.01 sec)
其中:
- validate_password_policy:密码策略,默认为MEDIUM策略
- validate_password_dictionary_file:密码策略文件,策略为STRONG才需要
- validate_password_length:密码最少长度
- validate_password_mixed_case_count:大小写字符长度,至少1个
- validate_password_number_count :数字至少1个
- validate_password_special_char_count:特殊字符至少1个
上述参数是默认策略MEDIUM的密码检查规则。
共有以下几种密码策略:
策略 | 检查规 |
---|---|
0 or LOW Length; | None |
1 or MEDIUM Length; | numeric, lowercase/uppercase, and special characters |
2 or STRONG Length; | lowercase/uppercase, and special characters; dictionary file |
MySQL官网密码策略详细说明:http://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy
修改密码策略:
在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略
选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件
- validate_password_policy=0
如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:
- validate_password = off
重新启动mysql服务使配置生效:
systemctl restart mysqld
添加远程登陆用户:
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:
GRANT ALL PRIVILEGES ON *.* TO 'yangxin'@'%' IDENTIFIED BY 'Yangxin0917!' WITH GRANT OPTION;
配置默认编码为utf8:
修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:
[linga@localhost ~]$ cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
# This is manual addition at 2018/8/28 23:55
character_set_server=utf8
init_connect='SET NAMES utf8'
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
重启mysql服务:
systemctl restart mysqld
看数据库默认编码:
mysql> show variables like '%character%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.01 sec)
默认配置文件路径:
- 配置文件:/etc/my.cnf
- 日志文件:/var/log//var/log/mysqld.log
- 服务启动脚本:/usr/lib/systemd/system/mysqld.service
- socket文件:/var/run/mysqld/mysqld.pid
作者: 咕咚!
出处: https://www.cnblogs.com/linga/
关于作者:专注虚拟化,运维开发,RPA,Rust,Go,Python!
本文版权归作者和博客园共有,禁止*.csdn.net转载,禁止以盈利为目的的转载,转载文章,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(oldsixa@163.com)咨询.