centos mysql 筆記(內含vagrant mysql 鏡像)


《SpringCloud Nginx 高併發核心編程》 環境搭建 - 系列

組件 連接地址
windows centos 虛擬機 安裝&排坑 vagrant+java+springcloud+redis+zookeeper鏡像下載(&製做詳解))
centos mysql 安裝&排坑 centos mysql 筆記(內含vagrant mysql 鏡像)
linux kafka安裝&排坑 kafka springboot (或 springcloud ) 整合
Linux openresty 安裝 Linux openresty 安裝
【必須】Linux Redis 安裝(帶視頻) Linux Redis 安裝(帶視頻)
【必須】Linux Zookeeper 安裝(帶視頻) Linux Zookeeper 安裝, 帶視頻
Windows Redis 安裝(帶視頻) Windows Redis 安裝(帶視頻)
RabbitMQ 離線安裝(帶視頻) RabbitMQ 離線安裝(帶視頻)
ElasticSearch 安裝, 帶視頻 ElasticSearch 安裝, 帶視頻
Nacos 安裝(帶視頻) Nacos 安裝(帶視頻)
【必須】Eureka Eureka 入門,帶視頻
【必須】springcloud Config 入門,帶視頻 springcloud Config 入門,帶視頻
【必須】SpringCloud 腳手架打包與啓動 SpringCloud腳手架打包與啓動
Linux 自啓動 假死自啓動 定時自啓 Linux 自啓動 假死啓動

centos mysql 虛擬機鏡像

瘋狂創客圈網盤,爲你們準備了springcloud.box 虛擬機鏡像,裏邊預裝了java 、 redis 、zookeeper、kafka、Eureka、springcloud config、mysql 等必須的組件,省去你們準備開發環境的煩惱,何不妙哉。網盤地址請參見【博客園總入口mysql

若是真正要本身安裝,請使用下面的教程。 這也是筆者的安裝筆記,而且會持續進行問題記錄。linux

安裝mysql

在CentOS中默認安裝有MariaDB,這個是MySQL的分支,但爲了須要,仍是要在系統中安裝MySQL,並且安裝完成以後能夠直接覆蓋掉MariaDB。nginx

1 下載並安裝MySQL官方的 Yum Repository

因爲CentOS 的yum源中沒有mysql,須要到mysql的官網下載yum repo配置文件。下載命令:面試

[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

執行過程以下:redis

[root@localhost work]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
--2020-12-27 02:02:22--  http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
.....
HTTP request sent, awaiting response... 200 OK
Length: 25548 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-10.noarch.rpm’

100%[==================================================================================================>] 25,548       127KB/s   in 0.2s

2020-12-27 02:04:42 (127 KB/s) - ‘mysql57-community-release-el7-10.noarch.rpm’ saved [25548/25548]

使用上面的命令就直接下載了安裝用的Yum Repository,大概25KB的樣子,spring

而後進行repo的安裝:

rpm -ivh mysql57-community-release-el7-10.noarch.rpm

執行過程以下:sql

[root@localhost work]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm
warning: mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-10 ################################# [100%]

執行完成後會在/etc/yum.repos.d/目錄下生成兩個repo文件mysql-community.repo mysql-community-source.repo

使用yum命令便可完成安裝

必須進入到 /etc/yum.repos.d/目錄後再執行如下腳本, 而後就能夠直接yum安裝了。

[root@localhost ~]# yum install mysql-server

這步可能會花些時間,安裝完成後就會覆蓋掉以前的mariadb。

[root@localhost yum.repos.d]# yum install mysql-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 .....
Installed:
  mysql-community-libs.x86_64 0:5.7.32-1.el7 mysql-community-libs-compat.x86_64 0:5.7.32-1.el7 mysql-community-server.x86_64 0:5.7.32-1.el7

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.32-1.el7                         mysql-community-common.x86_64 0:5.7.32-1.el7

Dependency Updated:
  openssl.x86_64 1:1.0.2k-21.el7_9             openssl-libs.x86_64 1:1.0.2k-21.el7_9             postfix.x86_64 2:2.10.1-9.el7

Replaced:
  mariadb-libs.x86_64 1:5.5.44-2.el7.centos

Complete!

啓動msyql:

啓動MySQL

systemctl start mysqld

登陸:

獲取安裝時的臨時密碼(在第一次登陸時就是用這個密碼):grep 'temporary password' /var/log/mysqld.log

[root@localhost yum.repos.d]# grep 'temporary password' /var/log/mysqld.log
2020-12-27T02:34:04.063826Z 1 [Note] A temporary password is generated for root@localhost: LXwH6NSf*kVh

使用臨時密碼,登陸mysql

mysql -u root -p

而後輸入密碼(剛剛獲取的臨時密碼)LXwH6NSf*kVh

登陸成功後,修改密碼

一、首先須要設置密碼的驗證強度等級,設置 validate_password_policy 的全局參數爲 LOW 便可,

輸入設值語句 「 set global validate_password_policy=LOW; 」 進行設值,

二、當前密碼長度爲 8 ,若是不介意的話就不用修改了,按照通用的來說,設置爲 6 位的密碼,設置 validate_password_length 的全局參數爲 6 便可,

輸入設值語句 「 set global validate_password_length=6; 」 進行設值,

三、如今能夠爲 mysql 設置簡單密碼了,只要知足六位的長度便可,

輸入修改語句 「 ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; 」 能夠看到修改爲功,表示密碼策略修改爲功了!!!

執行過程以下:

[root@localhost yum.repos.d]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32

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> set global validate_password_policy=LOW
    -> ;
Query OK, 0 rows affected (0.03 sec)

mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.01 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.04 sec)

注意:必定要記得在寫sql的時候要在語句完成後加上" ; "

經常使用命令

一、啓動MySQL

systemctl start mysqld

二、關閉MySQL

systemctl stop mysqld

三、重啓MySQL

systemctl restart mysqld

四、查看MySQL運行狀態

systemctl status mysqld

五、設置開機啓動

systemctl enable mysqld

六、關閉開機啓動

systemctl disable mysqld

問題排解

mysql賬號不容許從遠程登錄

默認狀況下,mysql賬號不容許從遠程登錄,只能在localhost登陸。本文提供了二種方法設置mysql能夠經過遠程主機進行鏈接。

  • 1、改表法
    在localhost登入mysql後,更改 「mysql」 數據庫裏的 「user」 表裏的 「host」 項,將」localhost」改稱」%」
#mysql -u root -p
Enter password:
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;

​ 說明: % 表明任意的客戶端,可替換成具體IP地址。

  • 2、受權法

例如: 你想myuser使用mypassword(密碼)從任何主機鏈接到mysql服務器的話。

  mysql>GRANT ALL PRIVILEGES ON . TO ‘myuser’@’%’IDENTIFIED BY ‘mypassword’ WITH GRANT OPTION;

  若是你想容許用戶myuser從ip爲192.168.1.6的主機鏈接到mysql服務器,並使用mypassword做爲密碼

mysql>GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3'IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES

配置默認編碼爲utf8:

vi /etc/my.cnf

添加

[mysqld]

 character_set_server=utf8 
 init_connect='SET NAMES utf8'

其餘配置文件:

/etc/my.cnf 日誌文件:/var/log//var/log/mysqld.log

服務啓動腳本:/usr/lib/systemd/system/mysqld.service

socket文件:/var/run/mysqld/mysqld.pid

不區分大小寫

找到你安裝MySQL的時候的my.cnf這個文件
以下圖所示:在 mysqld下添加一行
lower_case_table_names=1 (1不區分大小寫,0區分大小寫,默認爲0)

遠程鏈接 Mysql 太慢問題

嘗試解決了一下上面的鏈接超時問題,可是發現並無什麼用,仍是會出現上面的問題。因而便懷疑是否是遠程鏈接 Mysql 太慢致使了鏈接超時?由於我在 CentOS7 服務端和 Windows 本地的 Navicat 鏈接 mysql 都沒問題。在網上查詢了下,發如今 mysql 的配置文件 /etc/my.cnf 中增長以下配置參數:

# 注意該配置是加在[mysqld]下面

[mysqld]

skip-name-resolve

而後須要重啓 mysql 服務。由於根聽說明,若是 mysql 主機查詢和解析 DNS 會致使緩慢或是有不少客戶端主機時會致使鏈接很慢。同時,請注意在增長該配置參數後,mysql的受權表中的host字段就不可以使用域名而只可以使用ip地址了,由於這是禁止了域名解析的結果。

從防火牆開放端口,或者關閉防火牆

  • 經過systemctl status firewalld查看firewalld狀態
[root@localhost yum.repos.d]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
  • 若是要關閉防火牆設置,可能經過systemctl stop firewalld這條指令來關閉該功能。
systemctl stop firewalld.service   #關閉防火牆
  • 啓動防火牆
systemctl start firewalld

再次經過systemctl status firewalld查看firewalld狀態,顯示running即已開啓了。

  • 增長防火牆放行的規則:

再次執行執行firewall-cmd --permanent --zone=public --add-port=3306/tcp,提示success,表示設置成功,

firewall-cmd --permanent --zone=public --add-port=3306/tcp
  • 這樣就能夠繼續後面的設置了。從新加載防火牆
firewall-cmd --reload

說明:開發環境,關閉防火牆便可

報錯:Specified key was too long; max key length is 767 bytes

1 ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

版本5.6 解決方案;經過root帳號,設置兩個全局變量:

set global innodb_large_prefix=on;
set global innodb_file_format=Barracuda;

報錯:MySql Host is blocked because of many connection errors;

具體異常

MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'

  • 緣由分析:

查看 tomcat 的日誌文件,發如今報錯開始部分出現了這個錯誤。通過查詢,發現這個錯誤的 緣由 是:同一個 ip 在短期內產生太多(超過 mysql 數據庫 maxconnectionerrors 的最大值)中斷的數據庫鏈接而致使的阻塞。

  • 解決方法:

進入 CentOS7 服務器:

  • 方法一:提升容許的maxconnectionerrors數量(治標不治本):
  1. 進入 Mysql 數據庫查看 maxconnectionerrors: show variables like '%max_connection_errors%';
  2. 修改 maxconnectionerrors 的數量爲 1000: set global max_connect_errors = 1000;
  3. 查看是否修改爲功:show variables like '%max_connection_errors%';
  • 方法二:使用 mysqladmin flush-hosts 命令清理一下 hosts 文件:
  1. 查找 mysqladmin 的路徑:whereis mysqladmin
  2. 執行命令,如:/usr/local/mysql5.5.35/bin/mysqladmin -uroot -pyourpwd flush-hosts

注: 方法二清理 hosts 文件,也能夠直接進入 mysql 數據庫執行命令:mysql> flush hosts;

回到◀瘋狂創客圈

瘋狂創客圈 - Java高併發研習社羣,爲你們開啓大廠之門

相關文章
相關標籤/搜索