CentOS-7.5 安裝 社區版 MySQL-5.7

1、實驗環境

服務器類型:VMware虛擬機
服務器操作系統:CentOS-7.5
服務器名稱:CentOS-1
服務器IP:192.168.218.128
防火牆+selinux均已關閉
網絡連通狀況:服務器可以上外網

2、MySQL-5.7 安裝源的下載

1)先卸載系統自帶的mariadb
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# rpm -qa mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# rpm -qa mariadb

[root@CentOS-1 ~]#

2)下載並安裝mysql-5.7的安裝源
yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm #安裝mysql-5.7的安裝源

[root@CentOS-1 ~]# ll /etc/yum.repos.d/ | grep 'mysql-community*'
CentOS-7.5 安裝 社區版 MySQL-5.7
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# yum list mysql-community-server
CentOS-7.5 安裝 社區版 MySQL-5.7

3、刪除Linux系統原來自帶的 my.cnf 文件(如果存在的話)

[root@CentOS-1 ~]# rm -rf /etc/my.cnf
[root@CentOS-1 ~]#

4、安裝 MySQL-5.7
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# yum -y install mysql-community-server #安裝mysql
[root@CentOS-1 ~]# rpm -q mysql-community-server
mysql-community-server-5.7.29-1.el7.x86_64
[root@CentOS-1 ~]#

5、啓動MySQL-5.7
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# systemctl start mysqld.service #啓動mysql
[root@CentOS-1 ~]# systemctl enable mysqld.service #設置mysql服務爲自啓動
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# lsof -i:3306
CentOS-7.5 安裝 社區版 MySQL-5.7
[root@CentOS-1 ~]#

6、修改配置文件
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# mkdir /var/lib/mysql/binary_log #創建用於存放binlog日誌的目錄'binary_log'
[root@CentOS-1 ~]# ls /var/lib/mysql/binary_log
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# ll /var/lib/mysql | grep 'binary_log' #查看'binary_log'目錄的權限
CentOS-7.5 安裝 社區版 MySQL-5.7
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# chown -R mysql.mysql /var/lib/mysql/binary_log #修改該目錄權限
[root@CentOS-1 ~]# ll /var/lib/mysql | grep 'binary_log'
CentOS-7.5 安裝 社區版 MySQL-5.7

[root@CentOS-1 ~]# vim /etc/my.cnf #編輯修改mysql服務器的配置文件
說明:因爲後期要配置主從同步,所以配置文件中設置server-id,以及binlog存儲文件以master_a-binlog開頭(master_a表示第一個主服務器)
CentOS-7.5 安裝 社區版 MySQL-5.7

[root@CentOS-1 ~]# cat /etc/my.cnf #查看修改後的配置文件
CentOS-7.5 安裝 社區版 MySQL-5.7
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# systemctl restart mysqld.service #重啓mysql服務,使配置生效
[root@CentOS-1 ~]#
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# ll /var/lib/mysql/binary_log/ #查看此時是否生成了binlog文件
CentOS-7.5 安裝 社區版 MySQL-5.7

7、MySQL登錄測試
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# grep 'temporary' /var/log/mysqld.log #查看系統分配的mysql臨時登錄密碼
CentOS-7.5 安裝 社區版 MySQL-5.7

[root@CentOS-1 ~]# mysql -uroot -p'piDr2,8x#W6S' #使用臨時密碼登錄mysql並做查詢操作,發現查詢失敗
CentOS-7.5 安裝 社區版 MySQL-5.7

[root@CentOS-1 ~]#
[root@CentOS-1 ~]# mysql -uroot -p'piDr2,8x#W6S' #再次登錄mysql,根據錯誤提示進行排錯
mysql>
mysql> show databases; #查看錯誤提示
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mysql@123'; #根據錯誤提示進行排錯
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> flush privileges; #刷新權限
Query OK, 0 rows affected (0.00 sec)

mysql>
CentOS-7.5 安裝 社區版 MySQL-5.7

8、使用安全配置嚮導設置MySQL(MySQL初始化)
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# mysql_secure_installation #安全配置嚮導
Securing the MySQL server deployment.

Enter password for user root: #輸入root用戶密碼'Mysql@123'
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 #預計密碼強度爲100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y #更改root用戶密碼(是或者否)

New password: #輸入新root密碼,此處新密碼設置爲“Mysql.2020”

Re-enter new password: #再次輸入新root密碼“Mysql.2020”

Estimated strength of the password: 100 #預計密碼強度爲100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y #您是否要繼續使用提供的root密碼(是或者否)
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y #是否刪除匿名用戶
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y #禁止root用戶從遠程登錄mysql服務器
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y #是否刪除測試數據庫

  • Dropping test database...
    Success.

  • Removing privileges on test database...
    Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y #現在重新加載授權表
Success.

All done!
[root@CentOS-1 ~]#

9、MySQL登錄
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# mysql -uroot -p'Mysql.2020'
CentOS-7.5 安裝 社區版 MySQL-5.7

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