Linux(CentOS)安装MySQL5.7(yum)及用户授权

首先官网下载mysql5.7的yum源

https://dev.mysql.com/downloads/repo/yum/

安装yum源

yum localinstall mysql80-community-release-el7-3.noarch.rpm 

指定安装版本-----编辑mysql-community.repo文件

vi /etc/yum.repos.d/mysql-community.repo

打开后设置如下

# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

其中,enabled=0表示不安装,我们找到mysql5.7然后将enabled设置为1,其余设置为0(以防默认)。

设置好安装版本后,然后安装

yum install mysql-community-server

安装完成后,登录mysql,因在安装过程中,没有设置密码,第一次登录用我们直接回车看提示进行试探。

第一次登录出现如下情况 :

[root@master yum.repos.d]# mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

解决办法第一步,查看mysql服务状态

systemctl status mysqld
[root@master]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
[root@master]# 

可以看出服务没有激活

解决办法第二步,开启mysql服务

[root@master]# systemctl start mysqld
[root@master]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2019-09-25 22:06:12 CST; 2s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1995 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 1922 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 1998 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─1998 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid...

Sep 25 22:06:05 master systemd[1]: Starting MySQL Server...
Sep 25 22:06:12 master systemd[1]: Started MySQL Server.
[root@master]#

服务已经开启,我们再次无密码登录

[root@master yum.repos.d]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

发现没有密码被拒绝,此时应该想到好看日志,首先查看mysql的错误日志在那个文件

查看mysql错误日志在那个文件夹下

[root@master yum.repos.d]# cat /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=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# 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
[root@master yum.repos.d]# cat /var/log/mysqld.log 

从上面看有注意到mysql的错误日志位置(log-error=/var/log/mysqld.log)

查看mysql.log

cat /var/log/mysqld.log

复制密码再次登录

[root@master yum.repos.d]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.27

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

登录成功!查看数据库试试

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> 

提示要重置密码

修改密码第一步:设置密码安全检验(降低)

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)

修改密码第二步:修改密码(xxx为设置的密码)

mysql> alter user 'root'@'localhost' identified by 'xxxx';
Query OK, 0 rows affected (0.00 sec)

重新登录

[root@master yum.repos.d]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

查看策略表

可以看出,密码长度最小设置文4位,设置密码要求已经最低。到了这个阶段,mysql5.7已经安装好了。

为了方便远程连接,现给root用户授权。

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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章