伺服器環境:centos7 x64 需要安裝:mysql5.7+ 一、卸載CentOS7系統自帶mariadb # 查看系統自帶的Mariadb [root@CDH-141 ~]# rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x86_6 ...
伺服器環境:centos7 x64
需要安裝:mysql5.7+
一、卸載CentOS7系統自帶mariadb
# 查看系統自帶的Mariadb [root@CDH-141 ~]# rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x86_64 # 卸載系統自帶的Mariadb(rpm -e --nodeps) [root@CDH-141 ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64 # 刪除etc目錄下的my.cnf [root@CDH-141 ~]# rm /etc/my.cnf
二、檢查mysql是否存在
# 檢查mysql是否存在 [root@CDH-141 ~]# rpm -qa | grep mysql
三、查看用戶和組是否存在
1)檢查mysql組合用戶是否存在
# 檢查mysql組和用戶是否存在,如無則創建 [root@CDH-141 ~]# cat /etc/group | grep mysql [root@CDH-141 ~]# cat /etc/passwd | grep mysql
2)若不存在,則創建mysql組和用戶
[root@CDH-141 ~]# groupadd mysql # 創建mysql用戶組 [root@CDH-141 ~]# useradd -g mysql mysql # 創建一個用戶名為mysql的用戶,並加入mysql用戶組 [root@CDH-141 ~]# passwd mysql # 制定password 為111111 (用戶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
版本選擇,可以選擇以下兩種方式:
1)使用Red Hat Enterprise Linux
Select Version:5.7.35 Select Operating System:Red Hat Enterprise Linux / Oracle Linux Select OS Version:Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit) 列表中下載: Compressed TAR Archive:(mysql-5.7.35-el7-x86_64.tar.gz)
2)使用Linux - Generic
2)使用Linux - Generic Select Version:5.7.35 Select Operating System:Linux - Generic Select OS Version:Linux - Generic (glibc 2.12) (x86, 64-bit) 列表中下載: Compressed TAR Archive:(mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz)【本文中使用的是這個版本】
2)百度網盤
鏈接:https://pan.baidu.com/s/1-FnDtfsq-gFOcewVXYSGAw 提取碼:2zhz
註意:上邊三種方式找mysql離線安裝包的方式都可以。
五、上傳第四步下載的mysql TAR包
# 進入/usr/local/文件夾(此目錄作為安裝目錄,給根據自己目錄安排可自行更換目錄,後續目錄保持一致即可) [root@CDH-141 ~]# cd /usr/local/ # 上傳mysql TAR包 [root@CDH-141 local]# rz # 解壓mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz [root@CDH-141 local]# tar -zxvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz # 進入/usr/local下,修改為mysql [root@CDH-141 local]# mv mysql-5.7.35-linux-glibc2.12-x86_64 mysql [root@CDH-141 local]# ls bin etc full-path-to-mysql-VERSION-OS games include lib lib64 libexec mysql mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz sbin share src
六、更改所屬的組和用戶
# 更改所屬的組和用戶 [root@CDH-141 ~]# cd /usr/local/ [root@CDH-141 local]# chown -R mysql mysql/ [root@CDH-141 local]# chgrp -R mysql mysql/ [root@CDH-141 local]# cd mysql/ [root@CDH-141 mysql]# mkdir data [root@CDH-141 mysql]# chown -R mysql:mysql data 備註: chown -R mysql:mysql ./ chown [選項]... [所有者][:[組]] 文件... 必要參數: -c 顯示更改的部分的信息 -f 忽略錯誤信息 -h 修複符號鏈接 -R 處理指定目錄以及其子目錄下的所有文件 -v 顯示詳細的處理信息 -deference 作用於符號鏈接的指向,而不是鏈接文件本身 選擇參數: --reference=<目錄或文件> 把指定的目錄/文件作為參考,把操作的文件/目錄設置成參考文件/目錄相同擁有者和群組 --from=<當前用戶:當前群組> 只有當前用戶和群組跟指定的用戶和群組相同時才進行改變 --help 顯示幫助信息 --version 顯示版本信息
七、在/etc下創建my.cnf文件
# 進入/usr/local/mysql文件夾下 [root@CDH-141 ~]# cd /usr/local/mysql # 創建my.cnf文件 [root@CDH-141 mysql]# touch my.cnf # 編輯my.cnf [root@CDH-141 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 # 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 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION [mysql.server] user=mysql #用戶不是mysql時,更改此處 basedir=/usr/local/mysql
八、進入mysql文件夾,並安裝mysql
# 進入mysql [root@CDH-141 local]# cd /usr/local/mysql # 安裝mysql [root@CDH-141 mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 或者 cd 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
設置文件及目錄許可權:
[root@CDH-141 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld [root@CDH-141 mysql]# chown 777 my.cnf [root@CDH-141 mysql]# chmod +x /etc/init.d/mysqld [root@CDH-141 mysql]# mkdir data [root@CDH-141 mysql]# chown -R mysql:mysql data
九、啟動mysql
# 啟動mysql [root@CDH-141 mysql]# /etc/init.d/mysqld start/restart/stop/status 或者 [root@CDH-141 mysql]# service mysqld start/stop/restart/status
十、設置開機啟動
#設置開機啟動 [root@CDH-141 mysql]# chkconfig --level 35 mysqld on [root@CDH-141 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 [root@CDH-141 mysql]# chmod +x /etc/rc.d/init.d/mysqld [root@CDH-141 mysql]# chkconfig --add mysqld [root@CDH-141 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 [root@CDH-141 mysql]# service mysqld status MySQL running (26122)[ OK ] [root@CDH-141 mysql]#
十一、修改配置文件(配置環境變數)
# 進入/etc/profile文件夾 [root@CDH-141 mysql]# vim /etc/profile 修改/etc/profile,在最後添加如下內容 export PATH=$PATH:/usr/local/mysql/bin # 使文件生效 [root@CDH-141 mysql]# source /etc/profile
十二、獲得mysql初始密碼
1)獲得mysql初始密碼
[root@CDH-141 mysql]# cat /root/.mysql_secret # Password set for user 'root@localhost' at 2019-03-08 17:40:42 poc3u0mO_luv
2)修改密碼
[root@CDH-141 mysql]# mysql -uroot -p Enter password: #此處填寫上邊獲取到的初始密碼 ‘poc3u0mO_luv’ mysql> set PASSWORD = PASSWORD('123456'); 或者 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!'; 或者(本人一般使用下麵的修改密碼的方式) mysql> set password for 'root'@'localhost'=password('MyNewPass4!'); mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit 備註: 修改密碼時報錯: Can't connect to local MySQL server through socket '/tmp/mysql.sock' 上述提示可能在啟動mysql時遇到,即在/tmp/mysql.sock位置找不到所需要的mysql.sock文件,主要是由於my.cnf文件里對mysql.sock的位置設定導致。 mysql.sock預設的是在/var/lib/mysql, 如果發現確實是在該目錄下,可以在[mysqld]下麵加入mysql.sock的path vi /etc/my.cnf(my.cnf也可能在其他路徑下) [mysqld] port = 3306 socket = /var/lib/mysql/mysql.sock 但是要保證使用mysql的用戶具有對該目錄的寫許可權,否則這樣的改動由於許可權限制仍然會報錯。 所以為了避免許可權問題也可以使用軟鏈接為/var/lib/mysql/mysql.sock創建一個到/tmp/mysql.sock的聯接 ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock 由於/tmp/文件夾預設對other有w許可權,這樣就可以避免許可權問題。 此外,如果發現mysql.sock不在預設的/var/lib/mysql位置,一種解決方法是使用find命令搜索mysql.sock的位置,然後按前面兩種解決方案挑一種做即可。
3)驗證新密碼是否登錄成功:
[root@CDH-141 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) 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> use mysql # 修改root用戶可以遠程登錄: mysql> update user set host='%' where user='root'; 或者 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root用戶密碼' WITH GRANT OPTION; mysql> select host,user from user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | root | | localhost | mysql.session | | localhost | mysql.sys | +-----------+---------------+ 3 rows in set (0.00 sec)
十四、重啟mysql生效
[root@CDH-141 mysql]# service mysqld restart
備註: 由於安裝在/usr/local下麵的mysql,因此可以在熱河文件夾啟動mysql 若安裝在別的文件夾,請執行以下命令: # 為了在任何目錄下可以登錄mysql ln -s /你的mysql路徑/mysql /usr/local/mysql
十五、安裝過程(僅供參考)
[root@localhost ~]# rpm -qa|grep mariadb mariadb-libs-5.5.68-1.el7.x86_64 [root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 [root@localhost ~]# rm /etc/my.cnf rm: cannot remove ‘/etc/my.cnf’: No such file or directory [root@localhost ~]# rpm -qa | grep mysql [root@localhost ~]# cat /etc/group | grep mysql [root@localhost ~]# cat /etc/passwd | grep mysql [root@localhost ~]# groupadd mysql [root@localhost ~]# useradd -g mysql mysql [root@localhost ~]# passwd mysql Changing password for user mysql. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: Sorry, passwords do not match. New password: Retype new password: passwd: all authentication tokens updated successfully. [root@localhost ~]# cd /home/ [root@localhost home]# ll total 4 drwxr-xr-x. 3 root root 18 Apr 28 20:35 install drwx------. 3 mysql mysql 78 Aug 7 07:02 mysql drwx------. 15 panhusun panhusun 4096 Aug 7 06:52 panhusun drwxr-xr-x. 2 root root 6 Apr 21 01:12 server drwxr-xr-x. 4 root root 214 May 11 02:43 tar [root@localhost home]# cd tar/ [root@localhost tar]# ll total 559012 -rwxr-xr-x. 1 root root 11156 Apr 20 03:54 get_helm.sh -rw-r--r--. 1 root root 62468096 Apr 20 23:55 k3s -rw-r--r--. 1 root root 503815168 Apr 20 04:28 k3s-airgap-images-amd64.tar -rw-r--r--. 1 root root 15152 Oct 8 2021 k3s-selinux-0.4-1.el7.noarch.rpm drwxr-xr-x. 11 root root 4096 May 11 02:49 libressl-3.5.2 -rw-r--r--. 1 root root 4039407 May 11 02:43 libressl-3.5.2.tar.gz drwxr-xr-x. 14 501 games 4096 May 11 01:29 zlib-1.2.12 -rw-r--r--. 1 root root 2060528 May 11 01:58 zlib-1.2.12Upgrade.tar.gz [root@localhost tar]# pwd /home/tar [root@localhost tar]# ^C [root@localhost tar]# ll total 1209952 -rwxr-xr-x. 1 root root 11156 Apr 20 03:54 get_helm.sh -rw-r--r--. 1 root root 62468096 Apr 20 23:55 k3s -rw-r--r--. 1 root root 503815168 Apr 20 04:28 k3s-airgap-images-amd64.tar -rw-r--r--. 1 root root 15152 Oct 8 2021 k3s-selinux-0.4-1.el7.noarch.rpm drwxr-xr-x. 11 root root 4096 May 11 02:49 libressl-3.5.2 -rw-r--r--. 1 root root 4039407 May 11 02:43 libressl-3.5.2.tar.gz -rw-r--r--. 1 root root 666559924 Aug 7 07:04 mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz drwxr-xr-x. 14 501 games 4096 May 11 01:29 zlib-1.2.12 -rw-r--r--. 1 root root 2060528 May 11 01:58 zlib-1.2.12Upgrade.tar.gz [root@localhost tar]# cp mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz /usr/local/ [root@localhost tar]# cd /usr/local/ [root@localhost local]# ll total 650940 drwxr-xr-x. 2 root root 122 Apr 21 00:03 bin drwxr-xr-x. 2 root root 6 Apr 10 2018 etc drwxr-xr-x. 2 root root 6 Apr 10 2018 games drwxr-xr-x. 2 root root 35 May 11 02:00 include drwxr-xr-x. 2 root root 6 Apr 10 2018 lib drwxr-xr-x. 2 root root 6 Apr 10 2018 lib64 drwxr-xr-x. 2 root root 6 Apr 10 2018 libexec -rw-r--r--. 1 root root 666559924 Aug 7 07:04 mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz drwxr-xr-x. 2 root root 6 Apr 10 2018 sbin drwxr-xr-x. 5 root root 49 Feb 25 19:38 share drwxr-xr-x. 2 root root 6 Apr 10 2018 src [root@localhost local]# tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz mysql-5.7.36-linux-glibc2.12-x86_64/bin/myisam_ftdump mysql-5.7.36-linux-glibc2.12-x86_64/bin/myisamchk mysql-5.7.36-linux-glibc2.12-x86_64/bin/myisamlog mysql-5.7.36-linux-glibc2.12-x86_64/bin/myisampack mysql-5.7.36-linux-glibc2.12-x86_64/bin/mysql mysql-5.7.36-linux-glibc2.12-x86_64/bin/mysql_client_test_embedded mysql-5.7.36-linux-glibc2.12-x86_64/bin/mysql_config_editor mysql-5.7.36-linux-glibc2.12-x86_64/bin/mysql_embedded mysql-5.7.36-linux-glibc2.12-x86_64/bin/mysql_install_db mysql-5.7.36-linux-glibc2.12-x86_64/bin/mysql_plugin mysql-5.7.36-linux-glibc2.12-x86_64/bin/mysql_secure_installation mysql-5.7.36-linux-glibc2.12-x86_64/bin/mysql_ssl_rsa_setup mysql-5.7.36-linux-glibc2.12-x86_64/bin/mysql_tzinfo_to_sql 。。。。。。 [root@localhost local]# mv mysql-5.7.36-linux-glibc2.12-x86_64 mysql [root@localhost local]# ll total 650940 drwxr-xr-x. 2 root root 122 Apr 21 00:03 bin drwxr-xr-x. 2 root root 6 Apr 10 2018 etc drwxr-xr-x. 2 root root 6 Apr 10 2018 games drwxr-xr-x. 2 root root 35 May 11 02:00 include drwxr-xr-x. 2 root root 6 Apr 10 2018 lib drwxr-xr-x. 2 root root 6 Apr 10 2018 lib64 drwxr-xr-x. 2 root root 6 Apr 10 2018 libexec drwxr-xr-x. 9 root root 129 Aug 7 07:05 mysql -rw-r--r--. 1 root root 666559924 Aug 7 07:04 mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz drwxr-xr-x. 2 root root 6 Apr 10 2018 sbin drwxr-xr-x. 5 root root 49 Feb 25 19:38 share drwxr-xr-x. 2 root root 6 Apr 10 2018 src [root@localhost local]# rm -f mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz [root@localhost local]# ll total 0 drwxr-xr-x. 2 root root 122 Apr 21 00:03 bin drwxr-xr-x. 2 root root 6 Apr 10 2018 etc drwxr-xr-x. 2 root root 6 Apr 10 2018 games drwxr-xr-x. 2 root root 35 May 11 02:00 include drwxr-xr-x. 2 root root 6 Apr 10 2018 lib drwxr-xr-x. 2 root root 6 Apr 10 2018 lib64 drwxr-xr-x. 2 root root 6 Apr 10 2018 libexec drwxr-xr-x. 9 root root 129 Aug 7 07:05 mysql drwxr-xr-x. 2 root root 6 Apr 10 2018 sbin drwxr-xr-x. 5 root root 49 Feb 25 19:38 share drwxr-xr-x. 2 root root 6 Apr 10 2018 src [root@localhost local]# chown -R mysql mysql/ [root@localhost local]# chgrp -R mysql mysql/ [root@localhost local]# cd mysql/ [root@localhost mysql]# mkdir data [root@localhost mysql]# chown -R mysql:mysql data [root@localhost mysql]# cd /etc/ [root@localhost etc]# ll total 1420 drwxr-xr-x. 3 root root 101 Feb 25 19:43 abrt -rw-r--r--. 1 root root 16 Feb 25 19:51 adjtime -rw-r--r--. 1 root root 1529 Mar 31 2020 aliases -rw-r--r--. 1 root root 12288 Feb 25 19:52 aliases.db drwxr-xr-x. 3 root root 65 Feb 25 19:46 alsa drwxr-xr-x. 2 root root 4096 Feb 25 19:51 alternatives -rw-------. 1 root root 541 Aug 8 2019 anacrontab -rw-r--r--. 1 root root 55 Aug 8 2019 asound.conf -rw-r--r--. 1 root root 1 Oct 30 2018 at.deny drwxr-x---. 3 root root 43 Feb 25 19:43 audisp drwxr-x---. 3 root root 83 Feb 25 19:52 audit -rw-r--r--. 1 root root 15137 Sep 30 2020 autofs.conf -rw-------. 1 root root 232 Sep 30 2020 autofs_ldap_auth.conf -rw-r--r--. 1 root root 795 Sep 30 2020 auto.master drwxr-xr-x. 2 root root 6 Sep 30 2020 auto.master.d -rw-r--r--. 1 root root 524 Sep 30 2020 auto.misc -rwxr-xr-x. 1 root root 1260 Sep 30 2020 auto.net -rwxr-xr-x. 1 root root 687 Sep 30 2020 auto.smb drwxr-xr-x. 4 root root 71 Feb 25 19:46 avahi 。。。。。。 [root@localhost etc]# touch my.cnf [root@localhost etc]# ll total 1420 drwxr-xr-x. 3 root root 101 Feb 25 19:43 abrt -rw-r--r--. 1 root root 16 Feb 25 19:51 adjtime -rw-r--r--. 1 root root 1529 Mar 31 2020 aliases -rw-r--r--. 1 root root 12288 Feb 25 19:52 aliases.db drwxr-xr-x. 3 root root 65 Feb 25 19:46 alsa drwxr-xr-x. 2 root root 4096 Feb 25 19:51 alternatives -rw-------. 1 root root 541 Aug 8 2019 anacrontab -rw-r--r--. 1 root root 55 Aug 8 2019 asound.conf -rw-r--r--. 1 root root 1 Oct 30 2018 at.deny drwxr-x---. 3 root root 43 Feb 25 19:43 audisp drwxr-x---. 3 root root 83 Feb 25 19:52 audit 。。。。。。 [root@localhost etc]# pwd /etc [root@localhost etc]# cd /usr/local/mysql/ [root@localhost mysql]# ll total 272 drwxr-xr-x. 2 mysql mysql 4096 Aug 7 07:05 bin drwxr-xr-x. 2 mysql mysql 6 Aug 7 07:06 data drwxr-xr-x. 2 mysql mysql 55 Aug 7 07:05 docs drwxr-xr-x. 3 mysql mysql 4096 Aug 7 07:05 include drwxr-xr-x. 5 mysql mysql 230 Aug 7 07:05 lib -rw-r--r--. 1 mysql mysql 259199 Sep 6 2021 LICENSE drwxr-xr-x. 4 mysql mysql 30 Aug 7 07:05 man -rw-r--r--. 1 mysql mysql 566 Sep 6 2021 README drwxr-xr-x. 28 mysql mysql 4096 Aug 7 07:05 share drwxr-xr-x. 2 mysql mysql