CentOS 7 安装MySQL

在centos7中默认的是mariadb,先检查系统中是否有MySQL的安装

1.检查:

rpm -qa | grep mysql

[linga@localhost ~]$ sudo rpm -qa | grep mysql

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for linga: 
[linga@localhost ~]$

这里返回空值,说明是没有安装

2.先进入本机的源文件目录:

cd /usr/local/src

3.下载MySQL的reop源(这里用的是MySQL8.0版本的,官网介绍说比7.0块2倍):

wget
https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

4.安装rpm包:

rpm -ivh mysql80-community-release-el7-1.noarch.rpm

5.使用yum源来安装mysql-server:

yum install -y mysql-server

6.启动mysql服务:

systemctl start mysqld

7.设置开机启动MySQL服务:

systemctl enable mysqld

  1. 修改root本地登录密码: mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码:

cat /var/log/mysqld.log

2018-08-28T14:51:34.815275Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.12) initializing of server in progress as process 1032
2018-08-28T14:51:41.543460Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: S0rlNA:RDvu#
2018-08-28T14:51:43.537344Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.12) initializing of server has completed
2018-08-28T14:51:46.384749Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.12) starting as process 1332
2018-08-28T14:51:47.708760Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2018-08-28T14:51:48.007536Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.12'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.

找到默认密码是: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错误

9.通过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

10.修改密码策略: 在/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

11.添加远程登陆用户:
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:

GRANT ALL PRIVILEGES ON
. TO ‘yangxin’@’%’ IDENTIFIED BY ‘Yangxin0917!’ WITH GRANT OPTION;

12.配置默认编码为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://zhuanlan.zhihu.com/p/43927979
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞