安裝包下載
https://dev.mysql.com/downloads/mysql/
根據系統版本下載
下載安裝包: mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
安裝新版mysql前,需將系統自帶的mariadb-lib卸載 [root@hadoop01 ~]# rpm -qa|grep mariadb mariadb-libs-5.5.56-2.el7.x86_64 [root@hadoop01 ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64 [root@hadoop01 ~]# rpm -qa|grep mariadb 或者
yum remove mariadb
解壓安裝包 [root@hadoop01 mysql-5.7]# tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar mysql-community-embedded-devel-5.7.26-1.el7.x86_64.rpm mysql-community-libs-5.7.26-1.el7.x86_64.rpm mysql-community-embedded-5.7.26-1.el7.x86_64.rpm mysql-community-test-5.7.26-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.26-1.el7.x86_64.rpm mysql-community-common-5.7.26-1.el7.x86_64.rpm mysql-community-devel-5.7.26-1.el7.x86_64.rpm mysql-community-client-5.7.26-1.el7.x86_64.rpm mysql-community-server-5.7.26-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm
安裝mysql-community-common-5.7.26-1.el7.x86_64.rpm [root@hadoop01 mysql-5.7]# rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm warning: mysql-community-common-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:mysql-community-common-5.7.26-1.e################################# [100%]
安裝mysql-community-libs-5.7.26-1.el7.x86_64.rpm [root@hadoop01 mysql-5.7]# rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm warning: mysql-community-libs-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:mysql-community-libs-5.7.26-1.el7################################# [100%]
安裝mysql-community-client-5.7.26-1.el7.x86_64.rpm [root@hadoop01 mysql-5.7]# rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm warning: mysql-community-client-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:mysql-community-client-5.7.26-1.e################################# [100%]
安裝mysql-community-server-5.7.20-1.el7.x86_64.rpm 在安裝之前需要安裝libaio [root@hadoop01 mysql-5.7]# rpm -qa|grep libaio libaio-0.3.109-13.el7.x86_64
或者 yum install -y libaio
如果不存需要下載離線包: http://mirror.centos.org/centos/6/os/x86_64/Packages/ 安裝libaio庫: rpm -ivh libaio-0.3.107-10.el6.x86_64.rpm(若在有網情況下可執行yum install libaio)
安裝server,rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm [root@hadoop01 mysql-5.7]# rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm warning: mysql-community-server-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:mysql-community-server-5.7.26-1.e################################# [100%]
如果提示沒有 perl 則, 安裝 yum install -y perl
初始化數據庫 // 指定datadir, 執行后會生成~/.mysql_secret密碼文件(5.7以后不在使用) [root@hadoop01 mysql-5.7]# mysql_install_db --datadir=/var/lib/mysql // 初始化,執行生會在/var/log/mysqld.log生成隨機密碼 [root@hadoop01 mysql-5.7]# mysqld --initialize
更改mysql數據庫目錄的所屬用戶及其所屬組(沒用創建mysql用戶) [root@hadoop01 mysql-5.7]# chown mysql:mysql /var/lib/mysql -R
啟動mysql [root@hadoop01 mysql-5.7]# systemctl start mysqld.service
授權 [root@hadoop01 mysql-5.7]# chmod -R 777 mysql
查看效果 [root@hadoop01 mysql-5.7]# systemctl status mysqld.service 修改MySQL的登錄設置 [root@hadoop01 mysql-5.7]# vim /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables
重新啟動mysqld [root@hadoop01 mysql-5.7]# service mysqld restart Redirecting to /bin/systemctl restart mysqld.service
查看初始密碼
命令:cat /root/.mysql_secret
復制下來初始的密碼,做登錄使用
命令:bin/mysql -uroot -p
密碼就是剛才復制的密碼,粘貼進去回車即可。
登錄並修改MySQL的root密碼 [root@hadoop01 mysql-5.7]# 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.26 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. # 修改root用戶的密碼 mysql> update mysql.user set authentication_string=password('你要修改的密碼') where user='root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 # 刷新該表 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) # 退出MySQL mysql> quit; Bye 將MySQL的登錄設置修改回來
重新啟動mysqld [root@hadoop01 mysql-5.7]# service mysqld restart Redirecting to /bin/systemctl restart mysqld.service 登錄到mysql,更改root用戶的密碼 命令可以查看初始密碼 grep 'temporary password' /var/log/mysqld.log [root@hadoop01 mysql-5.7]# 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.26 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. # 修改root用戶的密碼 mysql> set password=password('你要修改的密碼'); Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 # 刷新該表 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) # 退出MySQL mysql> quit; Bye 遠程登陸授權 [root@hadoop01 mysql-5.7]# 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.26 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. # root用戶遠程登錄 mysql> grant all privileges on *.* to 'root'@'%' identified by '你的密碼' with grant option; Query OK, 0 rows affected, 1 warning (0.00 sec) # 刷新表 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) # 退出MySQL mysql> quit; Bye 設置mysql開機啟動 // 檢查是否已經是開機啟動 [root@hadoop01 mysql-5.7]# systemctl list-unit-files | grep mysqld // 開機啟動 [root@hadoop01 mysql-5.7]# systemctl enable mysqld.service 默認配置文件路徑: 配置文件:/etc/my.cnf 日志文件:/var/log/mysqld.log 服務啟動腳本:/usr/lib/systemd/system/mysqld.service socket文件:/var/run/mysqld/mysqld.pid 配置默認編碼為utf8 修改/etc/my.cnf配置文件,在[mysqld]下添加編碼配置,如下所示: [mysqld] character_set_server=utf8 init_connect='SET NAMES utf8'