編碼的世界 / 優質文選 / 歷史

Linux下搭建mysql5.7.18


2021年8月23日
-   

一. 簡介
          下載路徑:https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
           系統環境: centOS 7,  mysql 5.7.18
二. 安裝步驟

2.1 卸載舊的mysql


2.1.1 刪除mysql的安裝文件
         查詢mysql的安裝文件:  
[root@ali228 ~]# find / -name mysql
/usr/local/env/mysql
/usr/local/env/mysql/mysql
/usr/local/env/mysql/mysql/bin/mysql
/usr/local/env/mysql/mysql/include/mysql
/usr/local/env/mysql/mysql/data/mysql
[root@ali228 ~]#

         卸載:
[root@ali228 ~]# rm -rf /usr/local/env/mysql/mysql
[root@ali228 ~]#

2.1.2 刪除mysql的配置文件
         刪除/etc/my.cnf文件
[root@ali228 ~]# rm -rf /etc/my.cnf

         刪除/etc/init.d/下跟mysql有關的全部文件,一般包括mysql文件或mysqld文件。
[root@ali228 mysql]# rm -rf /etc/init.d/mysql
[root@ali228 mysql]# rm -rf /etc/init.d/mysqlId

2.1.3 刪除mysql用戶和用戶組
[root@ali228 ~]# userdel mysql
userdel: user 'mysql' does not exist

至此,卸載OK!

2.2 安裝MySQL


2.2.1 下載安裝包mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
         安裝包路徑: https://dev.mysql.com/downloads/mysql/5.7.html#downloads
         上傳mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz到/usr/local/env/mysql目錄下並解壓
[root@ali228 ~]# cd /usr/local/env/mysql/
[root@ali228 mysql]# tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz

           重命名為mysql

2.2.2 添加用戶組mysql和用戶mysql,並將其添加到mysql用戶組中
[root@ali228 mysql]# groupadd mysql
[root@ali228 mysql]# useradd -r -g mysql mysql

 注:  useradd -r參數表示mysql用戶是系統用戶,不可用於登錄系統           useradd -g參數表示把mysql用戶添加到mysql用戶組中。
2.2.3 檢查是否安裝了 libaio
[root@baidu64 ~]# rpm -qa | grep libaio

如果沒有安裝,使用如下命令安裝
[root@baidu64 ~]# yum search libaio

 2.2.4 配置my.cnf文件
[root@baidu64 ~]# touch /etc/my.cnf

注意: 使用默認的644權限和用戶,不要做修改;
將如下內容拷貝到裏面:
[mysql]
# 設置mysql客戶端默認字符集
default-character-set=utf8
socket=/tmp/mysql.sock
[mysqld]
#skip-name-resolve
#設置3306端口
port=3306
socket=/tmp/mysql.sock
# 設置mysql的安裝目錄
basedir=/usr/local/env/mysql/mysql
# 設置mysql數據庫的數據的存放目錄
datadir=/usr/local/env/mysql/mysql/data
# 允許最大連接數
max_connections=200
# 服務端使用的字符集默認為8比特編碼的latin1字符雿
character-set-server=utf8
# 創建新表時將使用的默認存儲引擿
default-storage-engine=INNODB
#lower_case_table_name=1
max_allowed_packet=16M

2.2.5 創建data文件夾
[root@baidu64 mysql]# pwd
/usr/local/env/mysql/mysql
[root@baidu64 mysql]# mkdir data

2.2.6 將mysql目錄的所屬用戶和組改為mysql
[root@baidu64 mysql]# chown -R mysql:mysql ./

2.2.7 初始化mysqld 生成初始化密碼
[root@baidu64 mysql]# ./bin/mysqld initialize user=mysql basedir=/usr/local/env/mysql/mysql datadir=/usr/local/env/mysql/mysql/data

此時,控制台打印如下:
2018-10-24T04:25:13.673365Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-24T04:25:15.808961Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-24T04:25:16.105505Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-24T04:25:16.184776Z 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: cec94f21-d744-11e8-a0b5-fa163ed8e403.
2018-10-24T04:25:16.188372Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-10-24T04:25:16.189074Z 1 [Note] A temporary password is generated for root@localhost: i;lknXwO;5,s
[root@baidu64 mysql]#

結尾: i;lknXwO;5,s 就是初始化的密碼。
3 常規配置

3.1 設置開機啟動


3.1.1 複制mysql.server腳本到資源目錄,並賦予執行權限:
[root@baidu64 mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@baidu64 mysql]# chmod +x /etc/rc.d/init.d/mysqld

3.1.2 將 mysqld 服務加入到系統服務並檢測是否生效: 
[root@baidu64 mysql]# chkconfig add mysqld
[root@baidu64 mysql]# chkconfig list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

得到如下結果說明生效。
注: 查看啟動項:chkconfig list | grep -i mysql        刪除啟動項:chkconfig del mysql
啟動mysqld:
[root@baidu64 mysql]# service mysqld start
Starting MySQL.Logging to '/usr/local/env/mysql/mysql/data/baidu64.err'.
[ OK ]

 

3.2 配置環境變量


3.2.1 打開/etc/profile配置文件,添加如下內容:
#mysql環境變量
PATH=$PATH:/usr/local/env/mysql/mysql/bin
export PATH

3.2.2 執行命令,使其生效:
[root@baidu64 mysql]# source /etc/profile

3.2.3 校驗是否成功:
[root@baidu64 mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/env/jdk/jdk8/jdk1.8.0_171/bin:/root/bin:/home/env/jdk/jdk8/jdk1.8.0_171/bin:/usr/local/env/mysql/mysql/bin

 
3.3  初次登錄 修改訪問密碼
3.3.1 無密碼登錄
[root@baidu64 mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.7.18
Copyright (c) 2000, 2017, 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>

注意: 此時密碼使用上面自動生成的初始化密碼:  i;lknXwO;5,s 即可
3.3.2 修改登錄密碼:
mysql> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set authentication_string=PASSWORD('123456') where User='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql>

3.4 允許遠程訪問


mysql> grant all privileges on *.* to root@"%" identified by "password" 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>

3.5 使用navicate遠程連接報錯


如果使用navicate進行遠程連接訪問,報如下錯誤:

解決方法如下:
3.5.1 關閉mysql服務
[root@baidu64 mysql]# service mysql stop
Redirecting to /bin/systemctl stop mysql.service

3.5.2 修改/etc/my.cnf修改為無密碼登錄
在my.cnf配置文件添加如下內容:
# mysql無密碼登錄
skip-grant-tables

3.5.3 重啟mysql服務
[root@baidu64 mysql]# service mysql restart
Redirecting to /bin/systemctl restart mysql.service

3.5.4 無密碼登錄mysql
注意: 此時登錄不帶p參數:
[root@baidu64 mysql]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>

3.5.5 再次修改mysql密碼:
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update mysql.user set authentication_string=password('123456') where user='root' ;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 1 Warnings: 1
mysql>

3.5.6 然後將my.cnf無密碼登錄配置去掉(就是上面剛加的那句話)3.5.7 退出mysql,並重啟:
mysql> quit
Bye
[root@baidu64 mysql]# service mysql restart
Redirecting to /bin/systemctl restart mysql.service
[root@baidu64 mysql]#

3.5.8 再次使用navicat連接:

如此  OK!
 
 

熱門文章