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';

 

 

 

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