伺服器環境:
centos7 x64
需要安裝mysql5.7+
一、解除安裝CentOS7系統自帶mariadb

# 檢視系統自帶的Mariadb
[[email protected] ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
# 解除安裝系統自帶的Mariadb
[[email protected] ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# 删除etc目錄下的my.cnf
[[email protected] ~]# rm /etc/my.cnf

二、檢查mysql是否存在
# 檢查mysql是否存在
[[email protected] ~]# rpm -qa | grep mysql
[[email protected] ~]#
三、檢視使用者群組是否存在
1)檢查mysql組合使用者是否存在
# 檢查mysql組和使用者是否存在,如無則建立
[[email protected] ~]# cat /etc/group | grep mysql
[[email protected] ~]# cat /etc/passwd | grep mysql
# 查詢全部使用者(隻是做記錄,沒必要執行)

[[email protected] ~]# cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F ":" '{print $1 "|" $3 "1" $4}' | more
root|010
sync|510
flume|9921989
hdfs|9911988
zookeeper|9891986
llama|9881985
httpfs|9871984
mapred|9861983
sqoop|9851982
yarn|9841981
kms|9831980
hive|9821979
oozie|9801977
hbase|9781975
impala|9761973
hue|9741971
wlaqzc2018|100111001
[[email protected] mysql]#

2)若不存在,則建立mysql組和使用者

# 建立mysql使用者組
[[email protected] ~]# groupadd mysql
# 建立一個使用者名為mysql的使用者,并加入mysql使用者組
[[email protected] ~]# useradd -g mysql mysql
# 制定password 為111111
[[email protected] ~]# 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檔案
版本選擇,可以選擇一下兩種方式:
1)使用Red Hat Enterprise LinuxSelect Version:5.7.25Select Operating System:Red Hat Enterprise Linux / Oracle LinuxSelect OS Version:Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)清單中下載下傳:Compressed TAR Archive:(mysql-5.7.25-el7-x86_64.tar.gz)2)使用Linux - GenericSelect Version:5.7.25Select Operating System:Linux - GenericSelect OS Version:Linux - Generic (glibc 2.12) (x86, 64-bit)清單中下載下傳:Compressed TAR Archive:(mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz)【本文中使用的是這個版本】注意:上邊兩種方式找mysql離線安裝包的方式都可以。
五、上傳第四步下載下傳的mysql TAR包

# 進入/usr/local/檔案夾
[[email protected] ~]# cd /usr/local/
# 上傳mysql TAR包
[[email protected] local]# rz
# 解壓mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
[[email protected] local]# ls
bin full-path-to-mysql-VERSION-OS include lib64 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz share
etc games lib libexec sbin src
[[email protected] local]# tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
mysql-5.7.25-lin
...
mysql-5.7.25-linux-glibc2.12-x86_64/share/install_rewriter.sql
mysql-5.7.25-linux-glibc2.12-x86_64/share/uninstall_rewriter.sql
mysql-5.7.25-linux-glibc2.12-x86_64/support-files/magic
mysql-5.7.25-linux-glibc2.12-x86_64/support-files/mysql.server
mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_BIN
mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_SRC
[[email protected] local]# ls
bin full-path-to-mysql-VERSION-OS include lib64 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz share
etc games lib libexec mysql-5.7.25-linux-glibc2.12-x86_64 sbin src
# 進入/usr/local下,修改為mysql
[[email protected] local]# mv mysql-5.7.25-linux-glibc2.12-x86_64 mysql
[[email protected] local]# ls
bin etc full-path-to-mysql-VERSION-OS games include lib lib64 libexec mysql mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz sbin share src

六、更改所屬的組和使用者

# 更改所屬的組和使用者
[[email protected] ~]# cd /usr/local/
[[email protected] local]# chown -R mysql mysql/
[[email protected] local]# chgrp -R mysql mysql/
[[email protected] local]# cd mysql/
[[email protected] mysql]# mkdir data
[[email protected] mysql]# chown -R mysql:mysql data

七、在/etc下建立my.cnf檔案

# 進入/usr/local/mysql檔案夾下
[[email protected] ~]# cd /usr/local/mysql
# 建立my.cnf檔案
[[email protected] mysql]# touch my.cnf #或者cd ''>my.conf
# 編輯my.cnf
[[email protected] mysql]# vi my.conf
[mysql]
socket=/var/lib/mysql/mysql.sock
# set mysql client default chararter
default-character-set=utf8
[mysqld]
socket=/var/lib/mysql/mysql.sock
# set mysql server port
port = 3323 #預設是3306,這裡發現3306已經被占用,是以防止這種情況發生,可以避免使用3306mysql預設端口
# set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=200
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
explicit_defaults_for_timestamp=true
[mysql.server]
user=mysql
basedir=/usr/local/mysql
[[email protected] mysql]#

八、進入mysql檔案夾,并安裝mysql

# 進入mysql
[[email protected] local]# cd /usr/local/mysql
# 安裝mysql
[[email protected] mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2019-03-08 18:11:07 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2019-03-08 18:11:24 [WARNING] The bootstrap log isn't empty:
2019-03-08 18:11:24 [WARNING] 2019-03-08T10:11:07.208602Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead

設定檔案及目錄權限:

[[email protected] mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# chown 777 my.cnf
[[email protected] mysql]# ls
bin COPYING data docs include lib man my.cnf README share support-files
[[email protected] mysql]# ls -l
total 60
drwxr-xr-x 2 root root 4096 Mar 8 15:56 bin
-rw-r--r-- 1 7161 31415 17987 Dec 21 18:39 COPYING
drwxr-x--- 5 mysql mysql 4096 Mar 8 16:21 data
drwxr-xr-x 2 root root 4096 Mar 8 15:56 docs
drwxr-xr-x 3 root root 4096 Mar 8 15:56 include
drwxr-xr-x 5 root root 4096 Mar 8 15:56 lib
drwxr-xr-x 4 root root 4096 Mar 8 15:56 man
-rw-r--r-- 1 777 root 516 Mar 8 16:19 my.cnf
-rw-r--r-- 1 7161 31415 2478 Dec 21 18:39 README
drwxr-xr-x 28 root root 4096 Mar 8 15:56 share
drwxr-xr-x 2 root root 4096 Mar 8 15:56 support-files
[[email protected] mysql]# chmod +x /etc/init.d/mysqld
[ro[email protected] mysql]#
[[email protected] mysql]# mkdir data
[[email protected] mysql]#
[[email protected] mysql]# chown -R mysql:mysql data
[[email protected] mysql]#

九、啟動mysql

# 啟動mysql
[[email protected] mysql]# /etc/init.d/mysqld restart
MySQL server PID file could not be found![FAILED]
Starting MySQL.Logging to '/usr/local/mysql/data/CDH-141.err'.
..The server quit without updating PID file (/usr/local/mysql/data/CDH-141.pid).[FAILED]
[[email protected] mysql]#

出現錯誤,解決方案如下:

#找到是否已經有程序占用
[[email protected] mysql]# ps aux|grep mysql
root 32483 0.0 0.0 113252 1620 pts/0 S 18:04 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/CDH-141.pid
mysql 32684 0.1 0.1 1119892 178224 pts/0 Sl 18:04 0: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=CDH-141.err --pid-file=/usr/local/mysql/data/CDH-141.pid --port=3323
root 35137 0.0 0.0 112648 944 pts/0 S+ 18:12 0:00 grep --color=auto mysql
#關閉程序
[[email protected] mysql]# kill -9 32684
[[email protected] mysql]# /usr/local/mysql/bin/mysqld_safe: line 198: 32684 Killed nohup /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=CDH-141.err --pid-file=/usr/local/mysql/data/CDH-141.pid --port=3323 < /dev/null > /dev/null 2>&1
#确認是否還占用
[[email protected] mysql]# ps aux|grep mysql
root 35501 0.0 0.0 112644 948 pts/0 S+ 18:13 0:00 grep --color=auto mysql
[[email protected] mysql]# /etc/init.d/mysqld restart
MySQL server PID file could not be found![FAILED]
Starting MySQL..[ OK ]
[[email protected] mysql]#
# 重新開機mysql
[[email protected] mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..[ OK ]
Starting MySQL..[ OK ]
[[email protected] mysql]#

十、設定開機啟動

#設定開機啟動
[[email protected] mysql]# chkconfig --level 35 mysqld on
[[email protected] 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
[[email protected] mysql]# chmod +x /etc/rc.d/init.d/mysqld
[[email protected] mysql]# chkconfig --add mysqld
[[email protected] 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
[[email protected] mysql]# service mysqld status
MySQL running (26122)[ OK ]
[[email protected] mysql]#

十一、修改配置檔案

# 進入/etc/profile檔案夾
[[email protected] mysql]# vim /etc/profile
修改/etc/profile,在最後添加如下内容
# 修改/etc/profile檔案
#set mysql environment
export PATH=$PATH:/usr/local/mysql/bin
# 使檔案生效
[[email protected] mysql]# source /etc/profile

十二、獲得mysql初始密碼
1)獲得mysql初始密碼
[[email protected] mysql]# cat /root/.mysql_secret
# Password set for user '[email protected]' at 2019-03-08 17:40:42
poc3u0mO_luv
[[email protected] mysql]#
2)修改密碼

[[email protected] mysql]# mysql -uroot -p
Enter password: #此處填寫上邊擷取到的初始密碼‘poc3u0mO_luv’
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25
Copyright (c) 2000, 2015, 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('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye

3)驗證新密碼是否登入成功:

[[email protected] mysql]# mysql -uroot -p
Enter password: #此處輸入新密碼‘123456’
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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> show tables;
ERROR 1046 (3D000): No database selected
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
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 user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 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生效
# 重新開機mysql
[[email protected] mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..[ OK ]
Starting MySQL..[ OK ]
[[email protected] mysql]#
備注:
由于安裝在/usr/local下面的mysql,是以可以在熱河檔案夾啟動mysql
若安裝在别的檔案夾,請執行以下指令:
# 為了在任何目錄下可以登入mysql
ln -s /你的mysql路徑/mysql /usr/local/mysql
申明原貼:https://www.cnblogs.com/yy3b2007com/p/10497787.html