做網(wǎng)站域名服務(wù)器網(wǎng)站建設(shè)公司簡(jiǎn)介
1、下載mysql安裝包
下載哪個(gè)版本,首先需要確定一下系統(tǒng)的glibc版本,使用如下命令:
rpm -qa | grep glibc
????????
2、檢查是否安裝過(guò)mysql
ps:因?yàn)橐郧坝脃um安裝過(guò),所以先用yum卸載。如果不是此方式或者沒(méi)安裝過(guò)則跳過(guò)?
[root@localhost ~]# yum remove mysql
已加載插件:fastestmirror
參數(shù) mysql 沒(méi)有匹配
不刪除任何軟件包
?查看是否有mysql依賴(lài)
[root@localhost ~]# rpm -qa | grep mysql
如果有則卸載
//普通刪除模式
rpm -e xxx(mysql_libs)
//強(qiáng)力刪除模式,如果上述命令刪除時(shí),提示有依賴(lài)其他文件,則可以用該命令對(duì)其進(jìn)行強(qiáng)力刪除
rpm -e --nodeps xxx(mysql_libs)
3、檢查是否有mariadb
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
如果有則卸載
[root@localhost ~]# rpm -e --nodeps mariadb-libs
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
錯(cuò)誤:未安裝軟件包 mariadb-libs-5.5.68-1.el7.x86_64
4、安裝mysql依賴(lài)包
[root@localhost home]# yum install libaio
5、解壓
進(jìn)入/opt目錄下將mysql文件解壓
[root@localhost home]# cd /opt
[root@localhost opt]# tar -xvf mysql-8.0.36-linux-glibc2.17-x86_64.tar
mysql-test-8.0.36-linux-glibc2.17-x86_64.tar.xz
mysql-8.0.36-linux-glibc2.17-x86_64.tar.xz
mysql-router-8.0.36-linux-glibc2.17-x86_64.tar.xz[root@localhost opt]# tar -Jxvf mysql-8.0.36-linux-glibc2.17-x86_64.tar.xz
[root@localhost opt]# mv mysql-8.0.36-linux-glibc2.17-x86_64 mysql
按照習(xí)慣,我們將文件移動(dòng)到/usr/local目錄下
[root@localhost opt]# mv /opt/mysql/ /usr/local/
我們切換到usr/local/目錄下查看mysql是否存在
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ll
總用量 0
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
drwxr-xr-x. 9 root root 129 4月 2 21:20 mysql
drwxr-xr-x. 11 root root 151 8月 28 2023 nginx
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 8月 29 2023 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
創(chuàng)建數(shù)據(jù)庫(kù)文件存放的文件夾。這個(gè)文件夾將來(lái)存放每個(gè)數(shù)據(jù)庫(kù)的庫(kù)文件
[root@localhost local]# cd mysql
[root@localhost mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@localhost mysql]# mkdir mysqldb
7、mysql安裝目錄賦予權(quán)限
[root@localhost mysql]# chmod -R 777 /usr/local/mysql/
8、創(chuàng)建mysql組和用戶(hù)
創(chuàng)建組
[root@localhost mysql]# groupadd mysql
創(chuàng)建用戶(hù)(-s /bin/false參數(shù)指定mysql用戶(hù)僅擁有所有權(quán),而沒(méi)有登錄權(quán)限)
[root@localhost mysql]# useradd -r -g mysql -s /bin/false mysql
將用戶(hù)添加到組中
[root@localhost mysql]# chown -R mysql:mysql ./
9、修改mysql配置文件
[root@localhost mysql]# vi /etc/my.cnf
將里面的命令都刪除掉,然后添加以下命令,保存并退出(如果有一定經(jīng)驗(yàn),可以在里面添加一些其他的配置)
[mysqld]
# 設(shè)置3306端口
port=3306
# 設(shè)置mysql的安裝目錄
basedir=/usr/local/mysql
# 設(shè)置mysql數(shù)據(jù)庫(kù)的數(shù)據(jù)的存放目錄
datadir=/usr/local/mysql/mysqldb
# 允許最大連接數(shù)
max_connections=10000
# 允許連接失敗的次數(shù)。這是為了防止有人從該主機(jī)試圖攻擊數(shù)據(jù)庫(kù)系統(tǒng)
max_connect_errors=10
# 服務(wù)端使用的字符集默認(rèn)為UTF8
character-set-server=utf8
# 創(chuàng)建新表時(shí)將使用的默認(rèn)存儲(chǔ)引擎
default-storage-engine=INNODB
# 默認(rèn)使用“mysql_native_password”插件認(rèn)證
default_authentication_plugin=mysql_native_password
[mysql]
# 設(shè)置mysql客戶(hù)端默認(rèn)字符集
default-character-set=utf8
[client]
# 設(shè)置mysql客戶(hù)端連接服務(wù)端時(shí)默認(rèn)使用的端口
port=3306
default-character-set=utf8
10、安裝mysql
進(jìn)入mysql 安裝目錄下:
[root@localhost mysql]# cd /usr/local/mysql/bin/
安裝mysql,并記住初始化隨機(jī)密碼
[root@localhost bin]# ./mysqld --initialize --console
2024-04-02T13:25:55.133890Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
2024-04-02T13:25:55.133913Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.36) initializing of server in progress as process 2186
2024-04-02T13:25:55.139191Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2024-04-02T13:25:55.154304Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-04-02T13:25:55.706150Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-04-02T13:25:57.058187Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: <;cdUJXy!91b
2024-04-02T13:25:57.159383Z 6 [Warning] [MY-013360] [Server] Plugin mysql_native_password reported: ''mysql_native_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
11、啟動(dòng)mysql服務(wù)
進(jìn)入mysql.server服務(wù)目錄下并啟動(dòng)服務(wù)
[root@localhost bin]# cd /usr/local/mysql/support-files
[root@localhost support-files]# ./mysql.server start
Starting MySQL.Logging to '/usr/local/mysql/mysqldb/localhost.localdomain.err'.ERROR! The server quit without updating PID file (/usr/local/mysql/mysqldb/localhost.localdomain.pid).
?如果第一次啟動(dòng),當(dāng)初始化執(zhí)行會(huì)有報(bào)錯(cuò)
此時(shí)不要擔(dān)心,重新給mysql安裝目錄賦予一下權(quán)限后,再次執(zhí)行。
[root@localhost support-files]# chmod -R 777 /usr/local/mysql
[root@localhost support-files]# ./mysql.server start
Starting MySQL. SUCCESS!
12、將mysql添加到系統(tǒng)進(jìn)程中
[root@localhost support-files]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
此時(shí)我們就可以使用服務(wù)進(jìn)程操作mysql了
13、設(shè)置mysql自啟動(dòng)
[root@localhost support-files]# chmod +x /etc/init.d/mysqld
[root@localhost support-files]# systemctl enable mysqld
mysqld.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mysqld on
此時(shí)mysql自啟動(dòng)就已經(jīng)設(shè)置好了
14、修改root用戶(hù)登錄密碼
登錄mysql
[root@localhost support-files]# cd /usr/local/mysql/bin/
[root@localhost bin]# ./mysql -u root -p
執(zhí)行后,輸入我們初始化時(shí)記錄下的隨機(jī)密碼,就會(huì)進(jìn)入mysql。
修改密碼:
mysql> alter user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
15、設(shè)置允許遠(yuǎn)程登錄
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 -ADatabase changed
mysql> update user set user.Host='%'where user.User='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)mysql> quit
Bye
16、重啟服務(wù)且測(cè)試
centos6與centos7的服務(wù)命令都支持
[root@localhost bin]# systemctl restart mysql
[root@localhost bin]# service mysql restart
Redirecting to /bin/systemctl restart mysql.service
查看mysql是否啟動(dòng)
systemctl status mysql
查看防火墻開(kāi)放端口
[root@localhost bin]# firewall-cmd --list-all
在防火墻中將3306端口開(kāi)放
[root@localhost bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
//--permanent為永久生效,沒(méi)有此參數(shù) 服務(wù)器重啟后配置失效
[root@localhost bin]# firewall-cmd --reload
success
在Navicat上測(cè)試連接
重啟linux后測(cè)試自啟動(dòng)(可選)
reboot
?測(cè)試mysql服務(wù)是否自啟動(dòng)
測(cè)試遠(yuǎn)程訪問(wèn)