(一)、準(zhǔn)備
1、Linux環(huán)境
視頻教程:https://www.bilibili.com/video/BV15m4y1d7ZP
2、檢查內(nèi)存
5.6及以上版本的MySQL要求Linux系統(tǒng)虛擬內(nèi)存不能小于1G,否則MySQL可能無(wú)法運(yùn)行。
3、卸載mariadb
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
[root@localhost ~]#
(二)、安裝
1、下載
下載地址:https://mirrors.aliyun.com/mysql/MySQL-8.0,這里下載mysql-8.0.28-el7-x86_64.tar.gz
2、上傳
將mysql-5.7.30-el7-x86_64.tar.gz壓縮文件上傳至/opt目錄;
3、解壓
將MySQL壓縮文件解壓至/usr/local目錄
[root@localhost ~]# tar -zxvf /opt/mysql-8.0.28-el7-x86_64.tar.gz -C /usr/local
4、重命名
將MySQL根目錄重命名為mysql
[root@localhost ~]# mv /usr/local/mysql-8.0.28-el7-x86_64 /usr/local/mysql
注意:必須重命名為mysql,否則無(wú)法啟動(dòng)
5、刪除
刪除壓縮文件
[root@localhost ~]# rm -f /opt/mysql-8.0.28-el7-x86_64.tar.gz
6、創(chuàng)建目錄
/usr/local/mysql根目錄下創(chuàng)建data文件夾
[root@localhost ~]# mkdir /usr/local/mysql/data
7、環(huán)境變量
a、編輯/etc/profile文件,內(nèi)容如下:
export PATH=/usr/local/mysql/bin:$PATH
b、重載/etc/profile文件:source /etc/profile
c、查看PATH值:echo $PATH
8、修改配置
a、查找mysql配置路徑
[root@localhost ~]# mysql --help | grep 'my.cnf'
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
[root@localhost ~]#
b、執(zhí)行vi /etc/my.cnf
c、點(diǎn)擊I鍵,復(fù)制并粘貼如下配置:
[mysql]
# 設(shè)置mysql客戶端默認(rèn)字符集
default-character-set=utf8
[mysqld]
#設(shè)置端口
port=3306
socket=/tmp/mysql.sock
#設(shè)置mysql根目錄
basedir=/usr/local/mysql
#設(shè)置數(shù)據(jù)庫(kù)的數(shù)據(jù)存放目錄
datadir=/usr/local/mysql/data
#設(shè)置最大連接數(shù)
max_connections=200
#設(shè)置mysql服務(wù)端字符集,默認(rèn)為latin1
character-set-server=UTF8MB4
#設(shè)置默認(rèn)存儲(chǔ)引擎
default-storage-engine=INNODB
#設(shè)置密碼永不過(guò)期
default_password_lifetime=0
#設(shè)置 server接受的數(shù)據(jù)包大小
max_allowed_packet=16M
9、用戶與用戶組
a、添加 mysql 組
[root@localhost ~]# groupadd mysql
b、添加 mysql 用戶
[root@localhost ~]# useradd -r -g mysql mysql
c、變更用戶和用戶組
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql
10、初始化
[root@localhost ~]# mysqld --initialize --user=mysql
2022-11-17T03:34:13.745049Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 20052
2022-11-17T03:34:13.868756Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-11-17T03:34:15.109952Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-11-17T03:34:16.778334Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: pH6T0ltJ6y,N
[root@localhost ~]#
說(shuō)明:pH6T0ltJ6y,N 為臨時(shí)密碼
11、其它
# 安裝SSL
[root@localhost ~]# mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
# 添加權(quán)限
[root@localhost ~]# chmod -R a+r /usr/local/mysql/data/server-key.pem
(三)、配置
1、開(kāi)機(jī)啟動(dòng)
a、復(fù)制啟動(dòng)腳本到資源目錄
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
b、mysqld文件添加執(zhí)行權(quán)限
[root@localhost ~]# chmod +x /etc/rc.d/init.d/mysqld
c、mysqld服務(wù)添加至系統(tǒng)服務(wù)
[root@localhost ~]# chkconfig --add mysqld
d、查詢mysqld服務(wù)
[root@localhost ~]# chkconfig --list mysqld
注:該輸出結(jié)果只顯示 SysV 服務(wù),并不包含
原生 systemd 服務(wù)。SysV 配置數(shù)據(jù)
可能被原生 systemd 配置覆蓋。
要列出 systemd 服務(wù),請(qǐng)執(zhí)行 'systemctl list-unit-files'。
查看在具體 target 啟用的服務(wù)請(qǐng)執(zhí)行
'systemctl list-dependencies [target]'。
mysqld 0:關(guān) 1:關(guān) 2:開(kāi) 3:開(kāi) 4:開(kāi) 5:開(kāi) 6:關(guān)
[root@localhost ~]#
e、啟動(dòng) mysqld服務(wù)
[root@localhost ~]# service mysqld start
2、開(kāi)放端口
a、添加端口
[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
b、重新加載
[root@localhost ~]# firewall-cmd --reload
3、修改密碼
初次登錄MySQL數(shù)據(jù)庫(kù)需要重置密碼才能繼續(xù)后面的數(shù)據(jù)庫(kù)操作,步驟如下:
[root@localhost ~]# mysql -uroot -p
Enter password: 輸入臨時(shí)密碼
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
[root@localhost ~]#
4、允許遠(yuǎn)程連接
MySQL數(shù)據(jù)庫(kù)默認(rèn)不允許遠(yuǎn)程連接,可通過(guò)如下步驟允許遠(yuǎn)程連接:
[root@localhost ~]# mysql -uroot -p
Enter password: 輸入密碼
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> 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> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@localhost ~]#
(四)、問(wèn)題
1、問(wèn)題一
問(wèn)題:使用mysql -u root -p命令登陸MySQL數(shù)據(jù)庫(kù)時(shí)提示如下錯(cuò)誤:
SQLyog遠(yuǎn)程連接MySQL,提示如下錯(cuò)誤:
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-444987.html
方案:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-444987.html
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> alter user 'root'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
[root@localhost ~]#
到了這里,關(guān)于如何在Linux系統(tǒng)中安裝MySQL數(shù)據(jù)庫(kù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!