Fork me on GitHub

MySQL单机版安装(yum)

CentOS7 64位下MySQL5.7安装与配置(YUM)

安装环境:Centos7(1708) 64位 MINI版,安装MySQL5.7
配置YUM源
在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/

下载mysql源安装包

1
2
[root@redis_cluster3 ~]# cd /home/soft/
[root@redis_cluster3 ~]# # wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

安装mysql源

1
[root@CENTOS7-TEST1 mysql]# yum localinstall mysql57-community-release-el7-11.noarch.rpm

检查mysql源是否安装成功

1
2
3
4
5
6
7
8
[root@CENTOS7-TEST1 mysql]# yum repolist enabled | grep "mysql.*-community.*"
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
mysql-connectors-community/x86_64 MySQL Connectors Community 45
mysql-tools-community/x86_64 MySQL Tools Community 59
mysql57-community/x86_64 MySQL 5.7 Community Server 247

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

安装MySQL

1
[root@CENTOS7-TEST1 mysql]# yum install mysql-community-server

启动MySQL服务

1
2
3
   [root@CENTOS7-TEST1 mysql]# systemctl status mysqld
[root@CENTOS7-TEST1 mysql]# systemctl start mysqld
[root@CENTOS7-TEST1 mysql]# systemctl stop mysqld

开启、关闭开机启动

设置开机自启

1
2
[root@CENTOS7-TEST1 mysql]# systemctl enable mysqld
[root@CENTOS7-TEST1 mysql]# systemctl daemon-reload

关闭开机自启

1
2
3
4
   [root@CENTOS7-TEST1 mysql]# systemctl list-unit-files
mysqld.service enabled
[root@CENTOS7-TEST1 mysql]# systemctl disable mysqld
Removed symlink /etc/systemd/system/multi-user.target.wants/mysqld.service.

修改root本地登录密码

mysql安装后密码,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:
1
2
  [root@CENTOS7-TEST1 ~]# grep 'temporary password' /var/log/mysqld.log
2018-03-11T19:46:51.452378Z 1 [Note] A temporary password is generated for root@localhost: aufAL9.gXgeU

登录mysql后设置新密码

1
2
3
4
5
6
7
[root@redis_cluster3 sbin]# mysql -u root -p aufAL9.gXgeU
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
ERROR:
Can't connect to the server
mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误

修改密码策略

在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略
如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:
    validate_password = off
重新启动mysql服务使配置生效:
[root@CENTOS7-TEST1 ~]# vi /etc/my.cnf
[root@CENTOS7-TEST1 ~]# systemctl restart mysqld

再次登录后设置密码

1
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
通过msyql环境变量可以查看密码策略的相关信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   mysql> show variables like '%password%';
+---------------------------------------+--------+
| Variable_name | Value |
+---------------------------------------+--------+
| default_password_lifetime | 0 |
| disconnect_on_expired_password | ON |
| log_builtin_as_identified_by_password | OFF |
| mysql_native_password_proxy_users | OFF |
| old_passwords | 0 |
| report_password | |
| sha256_password_proxy_users | OFF |
| validate_password_check_user_name | OFF |
| 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 |
+---------------------------------------+--------+
14 rows in set (0.00 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的密码检查规则。

添加远程登录用户

默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:
1
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

配置默认编码为utf8

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

1
2
3
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'

重新启动mysql服务,查看数据库默认编码如下所示:

添加 删除新用户、授权

创建数据库
1.create database [数据库名称] default character set utf8 collate utf8_general_ci;
   采用create schema和create database创建数据库的效果一样。 创建用户
2.create user ‘[用户名称]‘@’%’ identified by ‘[用户密码]’;
   %:匹配所有主机,该地方还可以设置成‘localhost’,代表只能本地访问,例如root账户默认为‘localhost‘ 用户授权数据库
3.grant select,insert,update,delete,create on [数据库名称]. to [用户名称];    代表整个数据库 立即启用修改
4.flush privileges ; 取消用户所有数据库(表)的所有权限
5.revoke all on . from tester; 删除用户
6.delete from mysql.user where user=’tester’; 删除数据库
7.drop database [schema名称|数据库名称];

默认配置文件路径:

配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid

------本文结束 感谢阅读------
鲁顺德 wechat
欢迎您扫一扫上面的微信公众号,订阅我的分享资源!
坚持原创技术分享,您的支持将鼓励我继续创作!