溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

淺析CentOS6.8安裝MySQL8.0.18的教程(RPM方式)

發(fā)布時間:2020-10-05 00:01:07 來源:腳本之家 閱讀:551 作者:冰 河 欄目:MySQL數(shù)據(jù)庫

今天,記錄下在CentOS 6.8服務器上如何安裝MySQL 8.0.18,廢話不多說了,直接進入主題。

一、卸載CentOS 6.8自帶的MySQL

首先,卸載CentOS 6.8服務器上自帶的MySQL,在命令行中輸入如下命令查看CentOS 6.8服務器自帶的MySQL。

[root@binghe151 src]# rpm -qa | grep -i mysql    
mysql-libs-5.1.73-7.el6.x86_64

可以看到,CentOS 6.8服務器中默認安裝了mysql-libs-5.1.73-7.el6.x86_64,接下來將mysql-libs-5.1.73-7.el6.x86_64卸載,如下所示。

[root@binghe151 src]# rpm -e mysql-libs-5.1.73-7.el6.x86_64 --nodeps 
[root@binghe151 src]# 

再次查看CentOS 6.8服務器自帶的MySQL,如下所示。

[root@binghe151 src]# rpm -qa | grep -i mysql
[root@binghe151 src]# 

說明CentOS 6.8服務器自帶的MySQL卸載成功。

二、安裝MySQL依賴環(huán)境

正式安裝MySQL之前,首先需要安裝MySQL的依賴的基礎環(huán)境,如下所示。

yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype* make gcc-c++ cmake bison-devel ncurses-devel bison perl perl-devel perl perl-devel net-tools* numactl* 

三、添加mysql用戶

在命令行執(zhí)行如下命令添加mysql用戶。

groupadd mysql
useradd -g mysql mysql

四、下載并安裝MySQL的RPM安裝包

首先,在CentOS 6.8服務器的命令行執(zhí)行如下命令下載MySQL 8.0.18的RPM安裝包。

wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.18-1.el6.x86_64.rpm-bundle.tar

接下來,查看下載的MySQL 8.0.18的RPM安裝包,如下所示

[root@binghe151 src]# ll
total 744876
-rw-r--r--. 1 root root 762746880 Nov 18 14:32 mysql-8.0.18-1.el6.x86_64.rpm-bundle.tar

接下來,將下載的安裝包解壓,如下所示。

[root@binghe151 src]# tar xvf mysql-8.0.18-1.el6.x86_64.rpm-bundle.tar 
mysql-community-client-8.0.18-1.el6.x86_64.rpm
mysql-community-test-8.0.18-1.el6.x86_64.rpm
mysql-community-server-8.0.18-1.el6.x86_64.rpm
mysql-community-libs-compat-8.0.18-1.el6.x86_64.rpm
mysql-community-common-8.0.18-1.el6.x86_64.rpm
mysql-community-devel-8.0.18-1.el6.x86_64.rpm
mysql-community-libs-8.0.18-1.el6.x86_64.rpm

接下來,按照順序依次安裝

mysql-community-common-8.0.18-1.el6.x86_64.rpm
mysql-community-libs-8.0.18-1.el6.x86_64.rpm
mysql-community-client-8.0.18-1.el6.x86_64.rpm
mysql-community-server-8.0.18-1.el6.x86_64.rpm

安裝時,需要嚴格按照上述順序進行安裝。

首先,安裝mysql-community-common-8.0.18-1.el6.x86_64.rpm,如下所示。

[root@binghe151 src]# rpm -ivh mysql-community-common-8.0.18-1.el6.x86_64.rpm
warning: mysql-community-common-8.0.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...    ########################################### [100%]
 1:mysql-community-common ########################################### [100%]

接下來,安裝mysql-community-libs-8.0.18-1.el6.x86_64.rpm,如下所示。

[root@binghe151 src]# rpm -ivh mysql-community-libs-8.0.18-1.el6.x86_64.rpm
warning: mysql-community-libs-8.0.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...    ########################################### [100%]
 1:mysql-community-libs ########################################### [100%]

接下來,安裝mysql-community-client-8.0.18-1.el6.x86_64.rpm,如下所示。

[root@binghe151 src]# rpm -ivh mysql-community-client-8.0.18-1.el6.x86_64.rpm
warning: mysql-community-client-8.0.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...    ########################################### [100%]
 1:mysql-community-client ########################################### [100%]

最后,安裝mysql-community-server-8.0.18-1.el6.x86_64.rpm,如下所示。

[root@binghe151 src]# rpm -ivh mysql-community-server-8.0.18-1.el6.x86_64.rpm
warning: mysql-community-server-8.0.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...    ########################################### [100%]
 1:mysql-community-server ########################################### [100%]

如果大家在安裝mysql-community-server-8.0.18-1.el6.x86_64.rpm的時候報錯,如下所示。

[root@binghe151 src]# rpm -ivh mysql-community-server-8.0.18-1.el6.x86_64.rpm
warning: mysql-community-server-8.0.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
  libnuma.so.1()(64bit) is needed by mysql-community-server-8.0.18-1.el6.x86_64
  libnuma.so.1(libnuma_1.1)(64bit) is needed by mysql-community-server-8.0.18-1.el6.x86_64
  libnuma.so.1(libnuma_1.2)(64bit) is needed by mysql-community-server-8.0.18-1.el6.x86_64

可以看到,缺少libnuma庫,執(zhí)行如下命令安裝libnuma。

yum install numactl* -y

執(zhí)行命令后,再次安裝mysql-community-server-8.0.18-1.el6.x86_64.rpm即可。

五、初始化MySQL數(shù)據(jù)

首先,來說一下MySQL官方對運行MySQL的限制。

在MySQL 8.x中,初始化數(shù)據(jù)需要確保數(shù)據(jù)庫目錄與文件的所有者為mysql登錄賬戶。如果以root身份運行mysqld服務,就需要確認一下,執(zhí)行如下所示的包含有--user選項的命令:

shell> bin/mysqld --initialize --user=mysql
shell> bin/mysqld --initialize-insecure --user=mysql

如果是以mysql的賬戶登錄并執(zhí)行程序的情況下,可以將--user選項從命令中去掉,如下所示。

shell> bin/mysqld --initialize
shell> bin/mysqld --initialize-insecure

通過以上MySQL官方的對運行MySQL的限制,可以得出。

如果以root用戶身份登錄CentOS 6.8服務器,可以執(zhí)行如下命令。

mysqld --initialize --user=mysql
mysqld --initialize-insecure --user=mysql

如果以mysql用戶身份登錄CentOS 6.8服務器,可以執(zhí)行如下命令。

mysqld --initialize
mysqld --initialize-insecure

因為我這里使用的是root賬戶登錄CentOS 6.8服務器,所以,初始化數(shù)據(jù)庫時,我執(zhí)行的是如下命令。

mkdir -p /usr/local/mysql
chown -R mysql.mysql /usr/local/mysql
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

注意:這里,我在執(zhí)行mysqld --initialize命令時,指定了MySQL的安裝目錄。 在MySQL的安裝過程中,建議指定MySQL的安裝目錄。當/usr/local/mysql目錄下存在data目錄時,會初始化失敗,必須確保/usr/local/mysql下沒有data目錄。

六、啟動MySQL

在CentOS 6.8服務器命令行輸入如下命令啟動MySQL服務。

[root@binghe151 src]# service mysqld start
Starting mysqld: 
[ OK ]

啟動MySQL時,會為MySQL的root賬戶生成臨時登錄密碼,這個密碼可以在/var/log/mysqld.log 文件中查看,使用vim編輯器打開/var/log/mysqld.log 文件,如下所示。

vim /var/log/mysqld.log 
2019-11-18T08:16:08.162464Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 2111
2019-11-18T08:16:12.451542Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: -8cagKkdK#5G
2019-11-18T08:17:13.269505Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 2378
2019-11-18T08:17:16.568836Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-18T08:17:16.642494Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server - GPL.
2019-11-18T08:17:16.652000Z 7 [Warning] [MY-013360] [Server] Plugin sha256_password reported: ''sha256_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instea
d'
2019-11-18T08:17:16.801986Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

找到如下一行代碼。

2019-11-18T08:16:12.451542Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: -8cagKkdK#5G

可以看到,啟動MySQL時生成的臨時登錄密碼為-8cagKkdK#5G。

七、登錄MySQL

使用臨時密碼登錄MySQL,如下所示。

[root@binghe151 src]# mysql -uroot -p-8cagKkdK#5G
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18
Copyright (c) 2000, 2019, 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命令行執(zhí)行SQL語句報錯,要求重置密碼,如下所示。

mysql> SHOW DATABASES;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> 

接下來,修改MySQL的root賬戶密碼,如下所示。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.03 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

接下來,在MySQL命令行輸入exit命令退出MySQL。使用修改后的root密碼重新登錄MySQL,如下所示。

mysql> exit
Bye
[root@binghe151 src]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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的root賬戶密碼修改成功。

接下來,再次在MySQL命令行執(zhí)行SQL語句,如下所示。

mysql> SHOW DATABASES;
+--------------------+
| Database   |
+--------------------+
| information_schema |
| mysql    |
| performance_schema |
| sys    |
+--------------------+
4 rows in set (0.36 sec)
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 -A
Database changed
mysql>

可以看到,修改MySQL的root賬戶密碼后,可以在MySQL命令行正確執(zhí)行SQL語句。

八、使用rpm安裝MySQL的默認目錄

1、數(shù)據(jù)庫目錄

/var/lib/mysql/

2、配置文件

/usr/share/mysql(mysql.server命令及配置文件)

3、相關命令

/usr/bin(mysqladmin mysqldump等命令)

4、啟動腳本

/etc/rc.d/init.d/(啟動腳本文件mysql的目錄)

九、配置my.cnf文件

MySQL 8.0默認沒有my.cnf,可以手動在/etc目錄下創(chuàng)建my.cnf文件,如果是以如下指定MySQL目錄的命令初始化數(shù)據(jù)。

mkdir -p /usr/local/mysql
chown -R mysql.mysql /usr/local/mysql
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
則可以將my.cnf文件配置成如下所示。
[client]
port = 3306
#根據(jù)實際情況調(diào)整mysql.sock配置
socket = /tmp/mysql.sock
[mysqld]
#Mysql服務的唯一編號 每個mysql服務Id需唯一
server-id = 1
#服務端口號 默認3306
port = 3306
#mysql安裝根目錄
basedir = /usr/local/mysql
#mysql數(shù)據(jù)文件所在位置
datadir = /usr/local/mysql/data
#pid
pid-file = /usr/local/mysql/mysql.pid
#設置socke文件所在目錄
socket = /tmp/mysql.sock
#設置臨時目錄
tmpdir = /tmp
# 用戶
user = mysql
# 允許訪問的IP網(wǎng)段
bind-address = 0.0.0.0
# 跳過密碼登錄
#skip-grant-tables
#主要用于MyISAM存儲引擎,如果多臺服務器連接一個數(shù)據(jù)庫則建議注釋下面內(nèi)容
skip-external-locking
#只能用IP地址檢查客戶端的登錄,不用主機名
skip_name_resolve = 1
#事務隔離級別,默認為可重復讀,mysql默認可重復讀級別(此級別下可能參數(shù)很多間隙鎖,影響性能)
transaction_isolation = READ-COMMITTED
#數(shù)據(jù)庫默認字符集,主流字符集支持一些特殊表情符號(特殊表情符占用4個字節(jié))
character-set-server = utf8mb4
#數(shù)據(jù)庫字符集對應一些排序等規(guī)則,注意要和character-set-server對應
collation-server = utf8mb4_general_ci
#設置client連接mysql時的字符集,防止亂碼
init_connect='SET NAMES utf8mb4'
#是否對sql語句大小寫敏感,1表示不敏感
lower_case_table_names = 1
#最大連接數(shù)
max_connections = 400
#最大錯誤連接數(shù)
max_connect_errors = 1000
#TIMESTAMP如果沒有顯示聲明NOT NULL,允許NULL值
explicit_defaults_for_timestamp = true
#SQL數(shù)據(jù)包發(fā)送的大小,如果有BLOB對象建議修改成1G
max_allowed_packet = 128M
#MySQL連接閑置超過一定時間后(單位:秒)將會被強行關閉
#MySQL默認的wait_timeout 值為8個小時, interactive_timeout參數(shù)需要同時配置才能生效
interactive_timeout = 1800
wait_timeout = 1800
#內(nèi)部內(nèi)存臨時表的最大值 ,設置成128M。
#比如大數(shù)據(jù)量的group by ,order by時可能用到臨時表,
#超過了這個值將寫入磁盤,系統(tǒng)IO壓力增大
tmp_table_size = 134217728
max_heap_table_size = 134217728
#禁用mysql的緩存查詢結(jié)果集功能
#后期根據(jù)業(yè)務情況測試決定是否開啟
#大部分情況下關閉下面兩項
query_cache_size = 0
query_cache_type = 0
#數(shù)據(jù)庫錯誤日志文件
log_error = error.log
#慢查詢sql日志設置
slow_query_log = 1
slow_query_log_file = slow.log
#檢查未使用到索引的sql
log_queries_not_using_indexes = 1
#針對log_queries_not_using_indexes開啟后,記錄慢sql的頻次、每分鐘記錄的條數(shù)
log_throttle_queries_not_using_indexes = 5
#作為從庫時生效,從庫復制中如何有慢sql也將被記錄
log_slow_slave_statements = 1
#慢查詢執(zhí)行的秒數(shù),必須達到此值可被記錄
long_query_time = 8
#檢索的行數(shù)必須達到此值才可被記為慢查詢
min_examined_row_limit = 100
#mysql binlog日志文件保存的過期時間,過期后自動刪除
expire_logs_days = 5

總結(jié)

以上所述是小編給大家介紹的CentOS6.8安裝MySQL8.0.18的教程(RPM方式),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI