CentOS7.4安裝配置mysql5.7 TAR免安裝版

一、CentOS7.4系統自帶mariadb
# 查看系統自帶的Mariadb
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
# 卸載系統自帶的Mariadb
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# 刪除etc目錄下的my.cnf
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rm /etc/my.cnf
二、檢查mysql是否存在
# 檢查mysql是否存在
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rpm -qa | grep mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# 
三、查看用戶和組是否存在

1)檢查mysql組合用戶是否存在

# 檢查mysql組和用戶是否存在,如無則創建
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cat /etc/group | grep mysql 
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cat /etc/passwd | grep mysql 
# 查詢全部用戶(只是做記錄,沒必要執行)
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F ":" '{print $1 "|" $3 "1" $4}' | more 
root|010
sync|510
mysql|99711001

2)若不存在,則創建mysql組和用戶

# 創建mysql用戶組
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# groupadd mysql
# 創建一個用戶名爲mysql的用戶,並加入mysql用戶組
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# useradd -g mysql mysql
# 制定password 爲111111
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# passwd mysql
Changing password for user mysql.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
四、下載mysql的tar包
https://dev.mysql.com/downloads/mysql/5.7.html#downloads


五、上傳第四步下載的mysql TAR包到
# 進入/usr/local/src文件夾
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cd /usr/local/src/
# 上傳mysql TAR包
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# rz
# 解壓tar文件
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# tar xvf mysql-5.7.22-el7-x86_64.tar
# 解壓mysql-5.7.22-el7-x86_64.tar.gz
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# tar -zxvf mysql-5.7.22-el7-x86_64.tar.gz
# 解壓後的文件移動到/usr/local文件夾
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# mv mysql-5.7.22-el7-x86_64 /usr/local
# 進入/usr/local下,修改爲mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# cd /usr/local
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# mv mysql-5.7.22-el7-x86_64 mysql
六、更改所屬的組和用戶
# 更改所屬的組和用戶
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# chown -R mysql mysql/
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# chgrp -R mysql mysql/
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# cd mysql/

[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# mkdir data

[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chown -R mysql:mysql data
七、在/etc下創建my.cnf文件
# 進入/etc文件夾下
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# cd /etc
# 創建my.cnf文件
[root@iZ2ze3hm3gyjyjz628l7rgZ etc]# touch my.cnf 
# 編輯my.cnf
[root@iZ2ze3hm3gyjyjz628l7rgZ etc]# vim my.cnf 

1)my.cnf添加如下內容:

[mysql]
# 設置mysql客戶端默認字符集
default-character-set=utf8 

[mysqld]
# 設置3306端口
port = 3306 
# 設置mysql的安裝目錄
basedir=/usr/local/mysql
# 設置mysql數據庫的數據的存放目錄
datadir=/usr/local/mysql/data
# 允許最大連接數
max_connections=200
# 服務端使用的字符集默認爲8比特編碼的latin1字符集
character-set-server=utf8
# 創建新表時將使用的默認存儲引擎
default-storage-engine=INNODB 
lower_case_table_names=1
max_allowed_packet=16M

2)查看my.cnf內容

# 查看my.cnf文件
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# cat /etc/my.cnf
八、進入mysql文件夾,並安裝mysql
# 進入mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ etc]# cd /usr/local/mysql/ 
# 安裝mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2018-07-04 15:46:02 [WARNING] 5mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-07-04 15:46:05 [WARNING] The bootstrap log isn't empty:
2018-07-04 15:46:05 [WARNING] 2018-07-04T15:46:02.728710Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2018-07-01T15:46:02.729161Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2018-07-04 T15:46:02.729167Z 0 [Warning] Changed limits: table_open_cache: 407 (requested 2000)
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chown 777 /etc/my.cnf 
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chmod +x /etc/init.d/mysqld
九、啓動mysql
# 啓動mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# /etc/init.d/mysqld restart
MySQL manager or server PID file could not be found!       [FAILED]

解決:

# 1、查看進程
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# ps aux|grep mysql
root     10031  0.0  0.1 113264  1616 pts/0    S    14:36   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/iZ2ze3hm3gyjyjz628l7rgZ.pid
mysql    10220  0.0 19.1 1140876 195072 pts/0  Sl   14:36   0:02 /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=iZ2ze3hm3gyjyjz628l7rgZ.err --pid-file=/usr/local/mysql/data/iZ2ze3hm3gyjyjz628l7rgZ.pid --port=3306
root     10421  0.0  0.0 112660   968 pts/0    R+   15:51   0:00 grep --color=auto mysql
# 2、殺死進程
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# kill -9 10031
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# kill -9 10220
# 3、重啓mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. 
Starting MySQL. 
十、設置開機啓動
#設置開機啓動
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --level 35 mysqld on
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --list mysqld

[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --add mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --list mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# service mysqld status
 SUCCESS! MySQL running (4475)
十一、修改配置文件
# 進入/etc/profile文件夾
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# vim /etc/profile

1)修改/etc/profile,在最後添加如下內容

# 修改/etc/profile文件
#set mysql environment
export PATH=$PATH:/usr/local/mysql/bin
# 使文件生效
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# source /etc/profile
十二、獲得mysql初始密碼
# 1、獲得mysql初始密碼
[root@iZ2ze3hm3gyjyjz628l7rgZ bin]# cat /root/.mysql_secret  
# Password set for user 'root@localhost' at 2017-04-17 17:40:02 
_pB*3VZl5T<6
# 2、修改密碼
[root@iZ2ze3hm3gyjyjz628l7rgZ bin]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.7.22 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> set PASSWORD = PASSWORD('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 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 host='%' where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
+-----------+---------------+
3 rows in set (0.00 sec)
十四、重啓mysql生效
# 重啓mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ bin]# /etc/init.d/mysqld restart 
Shutting down MySQL.. 
Starting MySQL. 

備註:

    由於安裝在/usr/local下面的mysql,因此可以在熱河文件夾啓動mysql

    若安裝在別的文件夾,請執行以下命令:

# 爲了在任何目錄下可以登錄mysql
ln -s /你的mysql路徑/mysql  /usr/local/mysql
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章