Linux安裝MySQL

前期的Linux環(huán)境檢測(cè)

1、SELinux和系統(tǒng)防火墻

# 需要把 SELinux 設(shè)置為disable,并且重啟系統(tǒng)
vi /etc/sysconfig/selinux
SELINUX=disable
# 關(guān)閉防火墻
chkconfig --list|grep iptables
chkconfig iptables off

2、將系統(tǒng)的IO調(diào)度模式設(shè)置為deadline模式

vi /sys/block/sda/queue/scheduler
noop anticipatory deadline[cfg]
# 修改IO的調(diào)度器需要在/etc/grub.conf中配置才能永久生效
vi /etc/grub.conf
# 最后一行添加
elevator=deadline

3、swap分區(qū)設(shè)置

swappiness值的大小對(duì)如何使用swap分區(qū)有著很大的影響。它有0和100兩個(gè)極限值,0表示最大限度的使用物理內(nèi)存,這種行為有可能導(dǎo)致系統(tǒng)內(nèi)存溢出,100則是積極的使用swap分區(qū),并且把內(nèi)存上面的數(shù)據(jù)及時(shí)的搬到swap分區(qū)里。

一般不建議分配swap,或者分配4GB的空間

# 查看系統(tǒng)的swappiness文件
cat /proc/sys/vm/swappiness
sysctl -a|grep swap

4、文件系統(tǒng)的選擇

建議選擇xfc文件系統(tǒng),相比與ext4,更加方便管理,支持動(dòng)態(tài)擴(kuò)容,刪除文件也很方便

5、操作系統(tǒng)的限制

ulimit -a

重要的兩個(gè)參數(shù)是 open filesmax user processes

open files是當(dāng)前服務(wù)器支持的最大句柄數(shù),也就是單個(gè)進(jìn)程最多可以訪問(wèn)多少個(gè)文件句柄(連接過(guò)多會(huì)導(dǎo)致打不開(kāi)表或者訪問(wèn)不到表的現(xiàn)象)。

max user processes 在單個(gè)服務(wù)器上運(yùn)行多個(gè)MySQL實(shí)例時(shí),可能會(huì)出現(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、關(guān)閉numa

關(guān)閉numa有利于更好的分配內(nèi)存,不需要采取swap的方式來(lái)獲取內(nèi)存。關(guān)閉方式可以在BIOS、操作系統(tǒng)和數(shù)據(jù)庫(kù)啟動(dòng)過(guò)程中關(guān)閉

numa --interleave=all /usr/local/mysql/bin/mysqld_safe -defaults-file=/etc/my.cnf &

正式的MySQL安裝

Linux上安裝軟件的常見(jiàn)方式

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安裝是會(huì)覆蓋已經(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開(kāi)始,MariaDB成為Yum源中默認(rèn)的數(shù)據(jù)庫(kù)安裝包。也就是說(shuō)在CentOS 7及以上的系統(tǒng)中使用yum安裝MySQL默認(rèn)安裝的會(huì)是MariaDB(MySQL的一個(gè)分支)。如果想安裝官方MySQL版本,需要使用MySQL提供的Yum源。

查看系統(tǒng)版本

cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

選擇對(duì)應(yīng)的版本進(jìn)行下載, https://dev.mysql.com/downloads/repo/yum/

wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm

安裝下載下來(lái)的源

rpm -Uvh mysql80-community-release-el7-3.noarch.rpm

檢查是否安裝成功

執(zhí)行成功后會(huì)在/etc/yum.repos.d/目錄下生成兩個(gè)repo文件mysql-community.repomysql-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

該命令會(huì)安裝MySQL服務(wù)器 (mysql-community-server) 及其所需的依賴、相關(guān)組件,包括mysql-community-client、mysql-community-common、mysql-community-libs等

step4:?jiǎn)?dòng)MySQL

# 啟動(dòng) 狀態(tài) 停止 重啟mysql服務(wù)
systemctl start/status/stop/restart mysqld.service
# 查看初始密碼,初始用戶是root@localhost,密碼存放在日志文件中
grep 'temporary password' /var/logs/mysqld.log
# 修改默認(rèn)密碼
mysql -uroot -p
# 輸入密碼登錄之后,進(jìn)入修改密碼,如果密碼過(guò)于簡(jiǎn)單,可以修改密碼策略
# 修改密碼默認(rèn)策略 validate_password_policy
validate_password_policy=0 #只校驗(yàn)密碼長(zhǎng)度,只要長(zhǎng)度跟 validate_password_length一樣即可,默認(rèn)長(zhǎng)度是8位
validate_password_policy=1 #這個(gè)時(shí)候首先要滿足的是validate_password_policy=0時(shí)的驗(yàn)證要求。然后現(xiàn)去驗(yàn)證密碼中的數(shù)字、大小寫、特殊字符個(gè)數(shù)。這些又分別由validate_password_number_count,validate_password_mixed_case_count,validate_password_special_char_count 這幾個(gè)參數(shù)來(lái)控制。
validate_password_policy=2 # 這個(gè)時(shí)候必須先滿足0,1的要求,然后它還追加了一個(gè),對(duì)于密碼中任意連續(xù)4個(gè)(或4個(gè)讓上)字符不得是字典中的單詞(validate_password_dictionary_file)
# 關(guān)閉密碼校驗(yàn)
vi /etc/my.cnf
# 添加以下配置,并重啟mysql服務(wù)
validate_password=OFF
# 重置密碼
alter user root@localhost identified by 'password';

允許root用戶遠(yuǎn)程訪問(wèn)

# 進(jìn)入MySQL進(jìn)行授權(quán)
grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;

設(shè)置編碼

show variables like 'character%';
vi /etc/my.cnf
[mysqld]
character_set_server=utf8
init-connect='SET NAMES utf8'

設(shè)置開(kāi)機(jī)自啟動(dòng)

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個(gè)。也可以單獨(dú)的下載以下的RPM包進(jìn)行安裝

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包是有依賴關(guān)系的,所以需要按照一定順序進(jìn)行安裝,安裝期間如果提示缺少哪些依賴也要先安裝相應(yīng)的包

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

還有一種簡(jiǎn)單的方式,可以自動(dòng)處理各個(gè)包之間的依賴關(guān)系并自動(dòng)下載缺少的依賴:

yum install mysql-community-{server,client,common,libs}-*

注意:上面的yum install命令需要在tar解壓之后的各個(gè)rpm包所在目錄內(nèi)執(zhí)行,否則就變成yum方式安裝了,需要配置MySQL的yum源并且速度很慢,還要當(dāng)前機(jī)器支持外網(wǎng)訪問(wèn)

3、使用壓縮包進(jìn)行安裝

下載對(duì)應(yīng)的 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依賴庫(kù)

MySQL依賴libaio庫(kù),如果沒(méi)有先安裝一下

yum -y install libaio

step2:創(chuàng)建mysql用戶

不需要登錄的一個(gè)系統(tǒng)賬號(hào),啟動(dòng)MySQL服務(wù)時(shí)會(huì)使用該賬號(hào)

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)以下錯(cuò)誤

error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

是因?yàn)閘ibnuma沒(méi)有安裝(或者默認(rèn)安裝的是32位),我們這里需要64位的

yum -y install numactl.x86_64

初始化完成后會(huì)有一行日志顯示出臨時(shí)密碼。

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
# 啟動(dòng)mysql并設(shè)置開(kāi)啟自啟動(dòng)
systemctl enable --now mysqld
# 查看mysql狀態(tài)
systemctl status mysqld
# 運(yùn)行mysql_secure_installation腳本,設(shè)置密碼
mysql_secure_installation

設(shè)置密碼的步驟

[root@docker packet]# mysql_secure_installation
Securing the MySQL server deployment.
# 是否進(jìn)入密碼配置
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
# 設(shè)置密碼強(qiáng)度 0 1 2,我設(shè)置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 
# 確認(rèn)是否繼續(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遠(yuǎn)程登陸
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
# 是否移除test數(shù)據(jù)庫(kù)
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.
# 是否重新載入權(quán)限表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done! 

配置遠(yuǎn)程登陸

本機(jī)登錄MySQL,將root用戶的host字段設(shè)為'%',意為接受root所有IP地址的登錄請(qǐng)求。

mysql -uroot -p
# input password
use mysql;
update user set host='%' where user='root';
flush privileges;

安裝后可能出現(xiàn)的問(wèn)題

使用root用戶啟動(dòng)mysql失敗

# 問(wèn)題
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.

問(wèn)題的根源都是使用root用戶安裝的mysql,在使用root用戶啟動(dòng)時(shí)就會(huì)報(bào)錯(cuò)

解決方法1:

mysqld --user=root start

解決方法2:

# 增加跳過(guò)權(quán)限表參數(shù)
mysqld --skip-grant-tables --skip-networking --user=root &

單個(gè)mysql配置文件啟動(dòng)多個(gè)mysql實(shí)例:

https://dev.mysql.com/doc/refman/8.0/en/using-systemd.html

MySQL使用select into outfile導(dǎo)出報(bào)錯(cuò)

select * from t2 into outfile 'root/mysql/blk/temp/t2.sql';
# 錯(cuò)誤信息如下
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

錯(cuò)誤的原因是: mysql默認(rèn)配置了導(dǎo)入和導(dǎo)出的文件路線,即 secure-file-priv參數(shù),導(dǎo)入或者導(dǎo)出的文件路徑與默認(rèn)的路徑?jīng)_突時(shí)就會(huì)報(bào)錯(cuò)

修改的方案也有兩種:

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,不允許導(dǎo)入和導(dǎo)出
  • /path,指定固定的路徑導(dǎo)入和導(dǎo)出
  • '', 不對(duì)mysql的導(dǎo)入和導(dǎo)出做限制
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容