centos install 命令安装 mysql数据库

时间:2023-03-09 17:23:52
centos  install 命令安装 mysql数据库

命令安装mysql就不需要自己去下载解压,超级方便

  下载:

wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

  

  安装MYSQL源:

yum localinstall mysql57-community-release-el7-.noarch.rpm

  安装途中会多次出现确认,输入'y'即可。

  检查mysql源是否安装成功:

[root@iZwz9i2qdajpoq1mydvp04Z ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community
mysql-tools-community/x86_64 MySQL Tools Community
mysql57-community/x86_64 MySQL 5.7 Community Server

  看到如上所示表示安装成功。

  安装MYSQL:

yum install mysql-community-server

  成功安装之后重启mysql服务:

service mysqld restar

  查看MySQL的启动状态:

[root@iZwz9i2qdajpoq1mydvp04Z ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue -- :: CST; 17s ago
Docs: man:mysqld()
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=/SUCCESS)
Process: ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=/SUCCESS)
Main PID: (mysqld)
CGroup: /system.slice/mysqld.service
└─ /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid Jul :: iZwz9i2qdajpoq1mydvp04Z systemd[]: Starting MySQL Server...
Jul :: iZwz9i2qdajpoq1mydvp04Z systemd[]: Started MySQL Server.

  设置开机启动:

systemctl enable mysqld
systemctl daemon-reload

  修改root默认密码

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

grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword!@#888';

  注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示错误:

ERROR  (HY000): Your password does not satisfy the current policy requirements

  通过msyql环境变量可以查看密码策略的相关信息:

mysql> show variables like '%password%';

  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
1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
2 or STRONG Length; numeric, 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),(MEDIUM),(STRONG)其中一种,选择2需要提供密码字典文件
validate_password_policy=

  如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:

validate_password = off

  重新启动 mysql 服务使配置生效:

systemctl restart mysqld

  

  添加远程登录用户:

  默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户。

  为了安全起见,添加一个新的帐户:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'test888'@'%' IDENTIFIED BY 'test888!' WITH GRANT OPTION;

  或者  

  修改root允许远程连接:

  选择mysql库:

use mysql;

  

  开启root远程访问:

  把root的host字段设置成%,表示所有ip都可以连接, 刷新生效:

mysql> update user set host='%' where user='root';

mysql> flush privileges;

  修改默认编码格式:utf8

  修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置

character_set_server=utf8
init_connect='SET NAMES utf8'

  重新启动 mysql 服务,用外网 check 一下吧