MySQL-CentOS7通过YUM安装MySQL5.7.29
文章目錄
- 生猛干貨
- 官方文檔
- Step1.確認操作系統的版本
- Step2. 檢查并卸載MariaDB
- Step3. 下載RPM
- Step4. 安裝rpm包 并確認yum源
- Step5. 安裝 MySQL
- Step6. 啟動 MySQL
- Step7. 登錄MySQL
- Step8. 開啟遠程訪問
- 常見錯誤
- [ERROR] --initialize specified but the data directory has files in it. Aborting.
- 解決辦法
- Initialization of mysqld failed: 0
- 解決辦法
- MySQL啟停
- 防火墻的相關設置
- 搞定MySQL
生猛干貨
帶你搞定MySQL實戰,輕松對應海量業務處理及高并發需求,從容應對大場面試
官方文檔
https://dev.mysql.com/doc/
如果英文不好的話,可以參考 searchdoc 翻譯的中文版本
http://www.searchdoc.cn/rdbms/mysql/dev.mysql.com/doc/refman/5.7/en/index.com.coder114.cn.html
Step1.確認操作系統的版本
[root@artisan ~]# cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core) [root@artisan ~]#CentOS7 及以上版本 默認安裝了 MariaDB ,MariaDB是MySQL源代碼的一個分支, 該分支完全兼容MySQL 。
CentOS 7為什么放棄了MySQL,而改使用MariaDB?。簡言之,MySQL被Oracle收購后,為了避免后續的法律糾紛,好多操作系統廠商都集成了開源免費的MariaDB了。
Step2. 檢查并卸載MariaDB
yum list installed | grep mariadb ---- 查詢已安裝的mariadbyum -y remove mariadb* ----- 移除已安裝的mariadb [root@artisan ~]# yum list installed | grep mariadb mariadb-libs.x86_64 1:5.5.35-3.el7 @anaconda mariadb-libs.x86_64 1:5.5.64-1.el7 installed [root@artisan ~]# [root@artisan ~]# yum -y remove mariadb* ...... ...... ...... Complete! [root@artisan ~]# [root@artisan ~]#重新查看,確保寫在完成
[root@artisan ~]# yum list installed | grep mariadb [root@artisan ~]#Step3. 下載RPM
訪問: https://dev.mysql.com/downloads/repo/yum/
[root@artisan ~]# cd /usr/local/ [root@artisan local]# mkdir mysql [root@artisan local]# cd mysql [root@artisan mysql]# [root@artisan mysql]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm如果想要確認下載的文件是否完整,可通過md5sum生成MD5值并確保同官方網站上的MD5值相同,以確保文件無損壞。
[root@artisan mysql]# md5sum mysql57-community-release-el7-11.noarch.rpm c070b754ce2de9f714ab4db4736c7e05 mysql57-community-release-el7-11.noarch.rpm [root@artisan mysql]#Step4. 安裝rpm包 并確認yum源
[root@artisan mysql]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm [root@artisan mysql]# yum repolist enabled | grep "mysql.*-community.*"查看 MySQL 版本
[root@artisan mysql]# yum repolist all | grep mysqlStep5. 安裝 MySQL
[root@artisan mysql]# yum install mysql-community-server一路y即可 ,下載安裝,靜候…
Step6. 啟動 MySQL
[root@artisan mysql]# systemctl start mysqld [root@artisan mysql]#查看進程
[root@artisan mysql]# ps -ef|grep mysqld |grep -v grep mysql 118107 1 0 23:46 ? 00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid [root@artisan mysql]#https://blog.csdn.net/u011886447/article/details/79796802
Step7. 登錄MySQL
[root@artisan mysql]# mysql -u root ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) [root@artisan mysql]#剛安裝的 MySQL 是沒有密碼的,所以需要設置密碼
-
① 停止 MySQL 服務:systemctl stop mysqld
-
② 以不檢查權限的方式啟動 MySQL: mysqld --user=root --skip-grant-tables &
-
③ 再次輸入 mysql -u root 或者 mysql,這次就可以進來了。
-
④ 更新密碼:
- ⑤ 刷新:flush privileges;
⑥ 退出:exit;
設置完之后,輸入 mysql -u root -p,這時輸入剛設置的密碼,就可以登進數據庫了。
[root@artisan mysql]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.29Copyright (c) 2000, 2020, 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> mysql> mysql> use mysql; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> mysql> use mysql; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> mysql> SET PASSWORD = PASSWORD('artisan'); Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)mysql>Step8. 開啟遠程訪問
[root@artisan mysql]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.29 MySQL Community Server (GPL)Copyright (c) 2000, 2020, 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> mysql> 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 -ADatabase changed mysql> update user set host = '%' where user = 'root'; Query OK, 1 row affected (0.29 sec) Rows matched: 1 Changed: 1 Warnings: 0mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec)mysql>其他方式 參考 :mysql開啟遠程訪問權限
常見錯誤
[ERROR] --initialize specified but the data directory has files in it. Aborting.
[root@artisan mysql]# systemctl start mysqld Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details. [root@artisan mysql]# systemctl status mysqld.service ● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: deactivating (final-sigterm) (Result: exit-code)Docs: man:mysqld(8)http://dev.mysql.com/doc/refman/en/using-systemd.htmlProcess: 28796 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=1/FAILURE)Process: 28773 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)CGroup: /system.slice/mysqld.service└─28799 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pidJan 27 08:55:43 artisan systemd[1]: mysqld.service holdoff time over, scheduling restart. Jan 27 08:55:43 artisan systemd[1]: Stopped MySQL Server. Jan 27 08:55:43 artisan systemd[1]: Starting MySQL Server... Jan 27 08:55:43 artisan mysqld_pre_systemd[28773]: 2020-01-27T00:55:43.664988Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit... details). Jan 27 08:55:43 artisan mysqld_pre_systemd[28773]: 2020-01-27T00:55:43.685134Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting. Jan 27 08:55:43 artisan mysqld_pre_systemd[28773]: 2020-01-27T00:55:43.685213Z 0 [ERROR] Aborting Jan 27 08:55:44 artisan systemd[1]: mysqld.service: control process exited, code=exited status=1 Hint: Some lines were ellipsized, use -l to show in full. [root@artisan mysql]# systemctl start firewalld [root@artisan mysql]# [root@artisan mysql]#找下關鍵的信息: [ERROR] --initialize specified but the data directory has files in it. Aborting.
既然說了data directory不為空,那就去/etc/my.cnf 配置文件看下mysql的配置去吧
到這個datadir指定的目錄下去瞅瞅吧
確實不為空。 那按照信息提示,那就清空了再啟唄
結果驗證,清了也沒用,依然是這個錯,那就…
解決辦法
MySQL的官網上找找吧
https://bugs.mysql.com/bug.php?id=79442
那就關閉 SELinux 吧
CentOS7關閉SELinux吧
[root@artisan mysql]# getenforce Enforcing# 臨時變更 # 設置SELinux 成為permissive模式 # setenforce 1 設置SELinux 成為enforcing模式[root@artisan mysql]# setenforce 0 [root@artisan mysql]# getenforce Permissive [root@artisan mysql]#永久改變
將SELINUX=enforcing改為SELINUX=disabled ,重啟生效
Initialization of mysqld failed: 0
查看下 mysql的日志 /var/log/mysqld.log
解決辦法
那就重新停一次吧
[root@artisan mysql]# ps aux|grep mysql mysql 117306 5.7 17.4 1122336 174664 ? Sl 23:33 0:01 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid root 117352 0.0 0.0 112716 960 pts/1 R+ 23:34 0:00 grep --color=auto mysql [root@artisan mysql]# systemctl stop mysqld [root@artisan mysql]# ps aux|grep mysql root 117379 0.0 0.0 112716 964 pts/1 S+ 23:34 0:00 grep --color=auto mysqlkill 不行,每次都會自動拉起。
然后重啟mysql
[root@artisan mysql]# systemctl start mysqld [root@artisan mysql]# [root@artisan mysql]# [root@artisan mysql]# [root@artisan mysql]# ps -ef|grep mysql mysql 117703 1 8 23:40 ? 00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid root 117739 74961 0 23:40 pts/1 00:00:00 grep --color=auto mysqlMySQL啟停
#啟動mysql [root@artisan mysql]systemctl start mysqld #停止mysqld [root@artisan mysql]systemctl stop mysqld #重啟mysqld [root@artisan mysql]systemctl restart mysqld #設置開機啟動 [root@artisan mysql]systemctl enable mysqld #查看 MySQL Server 狀態 [root@artisan mysql]systemctl status mysqld防火墻的相關設置
生產環境一般我們都是不會關閉防火墻的,所以開放特定端口即可。
# 查看防火墻狀態[root@artisan mysql]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: inactive (dead)# 打開防火墻 [root@artisan mysql]# systemctl start firewalld # 防火墻狀態[root@artisan mysql]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: active (running) since Tue 2020-01-28 02:43:15 CST; 9s agoMain PID: 127040 (firewalld)CGroup: /system.slice/firewalld.service└─127040 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopidJan 28 02:43:14 artisan systemd[1]: Starting firewalld - dynamic firewall daemon... Jan 28 02:43:15 artisan systemd[1]: Started firewalld - dynamic firewall daemon.# 開放 3306 端口 TCP UDP[root@artisan mysql]# firewall-cmd --permanent --zone=public --add-port=3306/tcp success [root@artisan mysql]# [root@artisan mysql]# firewall-cmd --permanent --zone=public --add-port=3306/udp success [root@artisan mysql]# [root@artisan mysql]# firewall-cmd --reload success [root@artisan mysql]# # CentOS 7,需要將 MySQL 服務加入防火墻,重啟防火墻[root@artisan mysql]# firewall-cmd --zone=public --permanent --add-service=mysql success [root@artisan mysql]# [root@artisan mysql]# systemctl restart firewalld [root@artisan mysql]#搞定MySQL
總結
以上是生活随笔為你收集整理的MySQL-CentOS7通过YUM安装MySQL5.7.29的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL-主从架构探索
- 下一篇: linux cmake编译源码,linu