前期的Linux環(huán)境檢測
1横辆、SELinux和系統(tǒng)防火墻
# 需要把 SELinux 設置為disable懂酱,并且重啟系統(tǒng)
vi /etc/sysconfig/selinux
SELINUX=disable
# 關閉防火墻
chkconfig --list|grep iptables
chkconfig iptables off
2竹习、將系統(tǒng)的IO調度模式設置為deadline模式
vi /sys/block/sda/queue/scheduler
noop anticipatory deadline[cfg]
# 修改IO的調度器需要在/etc/grub.conf中配置才能永久生效
vi /etc/grub.conf
# 最后一行添加
elevator=deadline
3、swap分區(qū)設置
swappiness值的大小對如何使用swap分區(qū)有著很大的影響列牺。它有0和100兩個極限值整陌,0表示最大限度的使用物理內存,這種行為有可能導致系統(tǒng)內存溢出昔园,100則是積極的使用swap分區(qū)蔓榄,并且把內存上面的數(shù)據(jù)及時的搬到swap分區(qū)里。
一般不建議分配swap默刚,或者分配4GB的空間
# 查看系統(tǒng)的swappiness文件
cat /proc/sys/vm/swappiness
sysctl -a|grep swap
4甥郑、文件系統(tǒng)的選擇
建議選擇xfc文件系統(tǒng),相比與ext4荤西,更加方便管理澜搅,支持動態(tài)擴容,刪除文件也很方便
5邪锌、操作系統(tǒng)的限制
ulimit -a
重要的兩個參數(shù)是 open files
和 max user processes
open files是當前服務器支持的最大句柄數(shù)勉躺,也就是單個進程最多可以訪問多少個文件句柄(連接過多會導致打不開表或者訪問不到表的現(xiàn)象)。
max user processes 在單個服務器上運行多個MySQL實例時觅丰,可能會出現(xiàn)創(chuàng)建不了新連接的情況
為了防止以上兩種情況的出現(xiàn)饵溅,可以修改系統(tǒng)的軟硬限制 /etc/security/limits.conf
vi /etc/security/limits.conf
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
6、關閉numa
關閉numa有利于更好的分配內存妇萄,不需要采取swap的方式來獲取內存蜕企。關閉方式可以在BIOS咬荷、操作系統(tǒng)和數(shù)據(jù)庫啟動過程中關閉
numa --interleave=all /usr/local/mysql/bin/mysqld_safe -defaults-file=/etc/my.cnf &
正式的MySQL安裝
Linux上安裝軟件的常見方式
1、源碼安裝
2轻掩、壓縮包安裝(.tar.gz)
3幸乒、編譯好的安裝包安裝(rpm、dpkg等)
4唇牧、在線安裝(yum)
刪除已安裝的MySQL
檢查mariadb
rpm -qa|grep mariadb
mariadb-server-5.5.60-1.el7_5.x86_64
mariadb-5.5.60-1.el7_5.x86_64
mariadb-libs-5.5.60-1.el7_5.x86_64
如果存在mariadb則刪除(yum安裝是會覆蓋已經(jīng)存在mariadb)
rpm -e --nodeps mariadb-server
rpm -e --nodeps mariadb
rpm -e --nodeps mariadb-libs
檢查mysql
rpm -qa|grep mysql # 存在就刪除
1罕扎、Yum安裝Mysql
step1:添加MySQL Yum Repository
從CentOS 7開始,MariaDB成為Yum源中默認的數(shù)據(jù)庫安裝包丐重。也就是說在CentOS 7及以上的系統(tǒng)中使用yum安裝MySQL默認安裝的會是MariaDB(MySQL的一個分支)腔召。如果想安裝官方MySQL版本,需要使用MySQL提供的Yum源扮惦。
查看系統(tǒng)版本
cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
選擇對應的版本進行下載宴咧, https://dev.mysql.com/downloads/repo/yum/
wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
安裝下載下來的源
rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
檢查是否安裝成功
執(zhí)行成功后會在/etc/yum.repos.d/
目錄下生成兩個repo文件mysql-community.repo
及 mysql-community-source.repo
step2:選擇Mysql版本
yum repolist all | grep mysql
切換版本
1、直接切換版本
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community
2径缅、直接修改 /etc/yum.repos.d/mysql-community.repo enable=0
表示禁用
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
修改后再次查看激活的mysql版本,發(fā)現(xiàn)5.7版本已經(jīng)被激活
yum repolist all | grep mysql
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 404
mysql57-community-source MySQL 5.7 Community Server - disabled
!mysql80-community/x86_64 MySQL 8.0 Community Server disabled
mysql80-community-source MySQL 8.0 Community Server - disabled
step3:安裝MySQL
yum -y install mysql-community-server
該命令會安裝MySQL服務器 (mysql-community-server) 及其所需的依賴烙肺、相關組件纳猪,包括mysql-community-client、mysql-community-common桃笙、mysql-community-libs等
step4:啟動MySQL
# 啟動 狀態(tài) 停止 重啟mysql服務
systemctl start/status/stop/restart mysqld.service
# 查看初始密碼氏堤,初始用戶是root@localhost,密碼存放在日志文件中
grep 'temporary password' /var/logs/mysqld.log
# 修改默認密碼
mysql -uroot -p
# 輸入密碼登錄之后,進入修改密碼搏明,如果密碼過于簡單鼠锈,可以修改密碼策略
# 修改密碼默認策略 validate_password_policy
validate_password_policy=0 #只校驗密碼長度,只要長度跟 validate_password_length一樣即可星著,默認長度是8位
validate_password_policy=1 #這個時候首先要滿足的是validate_password_policy=0時的驗證要求购笆。然后現(xiàn)去驗證密碼中的數(shù)字、大小寫虚循、特殊字符個數(shù)同欠。這些又分別由validate_password_number_count,validate_password_mixed_case_count横缔,validate_password_special_char_count 這幾個參數(shù)來控制铺遂。
validate_password_policy=2 # 這個時候必須先滿足0,1的要求茎刚,然后它還追加了一個襟锐,對于密碼中任意連續(xù)4個(或4個讓上)字符不得是字典中的單詞(validate_password_dictionary_file)
# 關閉密碼校驗
vi /etc/my.cnf
# 添加以下配置,并重啟mysql服務
validate_password=OFF
# 重置密碼
alter user root@localhost identified by 'password';
允許root用戶遠程訪問
# 進入MySQL進行授權
grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;
設置編碼
show variables like 'character%';
vi /etc/my.cnf
[mysqld]
character_set_server=utf8
init-connect='SET NAMES utf8'
設置開機自啟動
systemctl enable mysqld
systemctl daemon-reload
2膛锭、使用RPM包安裝MySQL
RPM包下載地址: https://downloads.mysql.com/archives/community/ 選擇合適的版本
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
解壓
tar -zxvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
解壓后粮坞,需要安裝的RMP包主要以下4個蚊荣。也可以單獨的下載以下的RPM包進行安裝
mysql-community-libs-5.7.28-1.el7.x86_64.rpm
mysql-community-common-5.7.28-1.el7.x86_64.rpm
mysql-community-client-5.7.28-1.el7.x86_64.rpm
mysql-community-server-5.7.28-1.el7.x86_64.rpm
各rpm包是有依賴關系的,所以需要按照一定順序進行安裝捞蚂,安裝期間如果提示缺少哪些依賴也要先安裝相應的包
rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
還有一種簡單的方式妇押,可以自動處理各個包之間的依賴關系并自動下載缺少的依賴:
yum install mysql-community-{server,client,common,libs}-*
注意:上面的yum install
命令需要在tar解壓之后的各個rpm包所在目錄內執(zhí)行,否則就變成yum方式安裝了姓迅,需要配置MySQL的yum源并且速度很慢敲霍,還要當前機器支持外網(wǎng)訪問
3、使用壓縮包進行安裝
下載對應的 tar 包: https://dev.mysql.com/downloads/mysql/
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
step1:安裝mysql依賴庫
MySQL依賴libaio庫丁存,如果沒有先安裝一下
yum -y install libaio
step2:創(chuàng)建mysql用戶
不需要登錄的一個系統(tǒng)賬號肩杈,啟動MySQL服務時會使用該賬號
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
step3:解壓配置
cd /usr/local/mysql
tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
# 初始化
bin/mysqld --initialize --user=mysql
如果初始化出現(xiàn)以下錯誤
error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
是因為libnuma沒有安裝(或者默認安裝的是32位),我們這里需要64位的
yum -y install numactl.x86_64
初始化完成后會有一行日志顯示出臨時密碼解寝。
CentOS安裝MySQL8
與上面使用yum安裝MySQL一樣扩然,我們只需激活MySQL8的版本即可
cat /etc/yum.repos.d/mysql-community.repo
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
安裝MySQL
# 直接執(zhí)行
dnf install @mysql
# 啟動mysql并設置開啟自啟動
systemctl enable --now mysqld
# 查看mysql狀態(tài)
systemctl status mysqld
# 運行mysql_secure_installation腳本,設置密碼
mysql_secure_installation
設置密碼的步驟
[root@docker packet]# mysql_secure_installation
Securing the MySQL server deployment.
# 是否進入密碼配置
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
# 設置密碼強度 0 1 2,我設置0
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.
# 輸入兩次新密碼
New password:
Re-enter new password:
Estimated strength of the password: 50
# 確認是否繼續(xù)使用提供的密碼
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
# 是否移除匿名用戶
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
# 是否不允許root遠程登陸
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
# 是否移除test數(shù)據(jù)庫
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
# 是否重新載入權限表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
配置遠程登陸
本機登錄MySQL聋伦,將root用戶的host字段設為'%'夫偶,意為接受root所有IP地址的登錄請求。
mysql -uroot -p
# input password
use mysql;
update user set host='%' where user='root';
flush privileges;
安裝后可能出現(xiàn)的問題
使用root用戶啟動mysql失敗
# 問題
mysqld
2020-07-27T14:26:54.337726Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-07-27T14:26:54.353607Z 0 [Note] mysqld (mysqld 5.7.29-log) starting as process 7339 ...
2020-07-27T14:26:54.356757Z 0 [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
2020-07-27T14:26:54.356784Z 0 [ERROR] Aborting
2020-07-27T14:26:54.356805Z 0 [Note] Binlog end
2020-07-27T14:26:54.356905Z 0 [Note] mysqld: Shutdown complete
systemctl start mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
問題的根源都是使用root用戶安裝的mysql觉增,在使用root用戶啟動時就會報錯
解決方法1:
mysqld --user=root start
解決方法2:
# 增加跳過權限表參數(shù)
mysqld --skip-grant-tables --skip-networking --user=root &
單個mysql配置文件啟動多個mysql實例:
https://dev.mysql.com/doc/refman/8.0/en/using-systemd.html
MySQL使用select into outfile導出報錯
select * from t2 into outfile 'root/mysql/blk/temp/t2.sql';
# 錯誤信息如下
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
錯誤的原因是: mysql默認配置了導入和導出的文件路線兵拢,即 secure-file-priv參數(shù),導入或者導出的文件路徑與默認的路徑?jīng)_突時就會報錯
修改的方案也有兩種:
1逾礁、修改文件的路徑
2说铃、修改配置文件
mysql> show variables like '%secure%'\G;
*************************** 1. row ***************************
Variable_name: require_secure_transport
Value: OFF
*************************** 2. row ***************************
Variable_name: secure_auth
Value: ON
*************************** 3. row ***************************
Variable_name: secure_file_priv
Value: /var/lib/mysql-files/
3 rows in set (0.00 sec)
secure_file_priv 參數(shù)有三種取值
- null,不允許導入和導出
- /path嘹履,指定固定的路徑導入和導出
- ''腻扇, 不對mysql的導入和導出做限制