Centos7安裝mysql5.7.21


1、卸載系統自帶的 MariaDB

[root@CentOS ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64
[root@CentOS ~]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64

2、下載 mysql5.7.21 安裝包

[root@CentOS local]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

3、創建 mysql 用戶組和 mysql 用戶

​[root@CentOS local]# useradd mysql

4、解壓安裝包,並將解壓好的文件夾重命名為mysql

​[root@CentOS local]# tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz 
mysql-5.7.21-linux-glibc2.12-x86_64/bin/myisam_ftdump
mysql-5.7.21-linux-glibc2.12-x86_64/bin/myisamchk
mysql-5.7.21-linux-glibc2.12-x86_64/bin/myisamlog
mysql-5.7.21-linux-glibc2.12-x86_64/bin/myisampack
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql_client_test_embedded
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql_config_editor
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql_embedded
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql_install_db
mysql-5.7.21-linux-glibc2.12-x86_64/bin/mysql_plugin
......(略去內容).....
[root@CentOS local]# mv mysql-5.7.21-linux-glibc2.12-x86_64 mysql

5、將 mysql 添加為系統服務

​[root@CentOS local]# cp mysql/support-files/mysql.server /etc/init.d/mysql
[root@CentOS local]# chkconfig --add mysql

6、初始化數據庫

​[root@CentOS mysql]# mkdir data
[root@CentOS mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-02-24T11:03:47.265873Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-02-24T11:03:48.343129Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-02-24T11:03:48.473269Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-02-24T11:03:48.541782Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 63b1ed0c-1952-11e8-aa97-000c29847b2d.
2018-02-24T11:03:48.542867Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-02-24T11:03:48.543383Z 1 [Note] A temporary password is generated for root@localhost: =u>yyVorV7g%
[root@CentOS mysql]# 

例如在 /usr/local/mysql 目錄下創建用於保存數據的data 目錄,初始化數據庫后獲得的臨時密碼為:=u>yyVorV7g%

7、啟動mysql服務,使用臨時密碼登錄mysql

​[root@CentOS mysql]# systemctl start mysql        # 使用 service mysql start 也可以
[root@CentOS mysql]# ps -ef | grep mysql
root       4654      1  0 19:07 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/CentOS.pid
mysql      4739   4654  4 19:07 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=CentOS.err --pid-file=/usr/local/mysql/data/CentOS.pid
root       4772   3618  0 19:07 pts/0    00:00:00 grep --color=auto mysql
[root@CentOS mysql]# ./bin/mysql -u root -p
Enter password:                     # 這里輸入剛獲取的臨時密碼
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21

Copyright (c) 2000, 2018, 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> 

8、修改登錄密碼

​mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');    # 將 root 登錄密碼修改為123456
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

9、將 mysql 服務設置為開機啟動

​[root@CentOS mysql]# chkconfig mysql on

10、設置任何遠程主機都可以訪問數據庫

​[root@CentOS bin]# ./mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> grant all privileges on *.* to 'root' @'%' identified by '123456';    # 設置任何遠程主機都可以訪問
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

mysql> 

11、開放 3306 端口(mysql 默認端口)

​[root@CentOS bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent    # 添加 3306 端口
success
[root@CentOS bin]# firewall-cmd --reload    # 重新載入
success
[root@CentOS bin]# 

12、使用 Sqlyog 客戶端進行測試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM