本文介紹的是在linux centos7下安裝mysql8.0.15
一、獲取mysql
可以官網下載
或者使用我 提供好的
https://pan.baidu.com/s/1LVUau71wSmaE84QTuEDSFQ
提取碼:7mr9
也可以使用wget命令下載,不過下載的太慢了
這是一個xz包,先用xz命令解壓成tar包,再用tar命令解壓成文件夾,可能會有點慢,因為文件有點大,估計在1.8GB左右
[root@VM-0-15-centos ~]# xz -d mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz [root@VM-0-15-centos ~]# tar xf mysql-8.0.15-linux-glibc2.12-x86_64.tar [root@VM-0-15-centos ~]# cd mysql-8.0.15-linux-glibc2.12-x86_64
解壓工作完成,進入mysql文件夾,可以看見mysql的文件目錄
二、安裝
我們把解壓好的mysql文件夾移動到/usr/local/mysql,這個目錄可以自己定義
安裝完成
三、配置mysql
修改配置文件my.cnf
我這里提供了一份比較詳細的配置文件
https://pan.baidu.com/s/1matQzLm5FvPmJzi8LMV_Ng
提取碼:6t23
可以使用ftp把 /etc下的my.cnf文件替換掉
替換完之后,我們需要創建幾個目錄,具體的路徑在my.cnf里面,這里跟着我直接創建目錄就行了
#切記,創建目錄的時候,一定要在mysql文件的根目錄創建
[root@VM-0-15-centos mysql]# mkdir data sql_log undo [root@VM-0-15-centos mysql]#
創建一個mysql賬號用來啟動mysql服務,一般不用root賬號,這樣不安全
[root@VM-0-15-centos mysql]# adduser mysql
把mysql目錄更創建的文件的屬、組改變成mysql的
[root@VM-0-15-centos mysql]# chown mysql:mysql -R data/ sql_log/ undo/
四、初始化mysql
添加mysql全局環境變量
#在最后加上export PATH=$PATH:/usr/local/mysql/bin #/usr/local/mysql是你的mysql安裝目錄 #:wq保存退出 [root@VM-0-15-centos mysql]# vi /etc/profile #重新加載環境變量 [root@VM-0-15-centos mysql]# source /etc/profile
初始化mysql,在5.7以后的版本需要用mysqld命令
[root@VM-0-15-centos mysql]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
--user 指定mysql用戶
--basedir 指定安裝目錄
--datadir 指定日志目錄
初始化后可以進入data目錄,會發現自動生成了系統的數據庫
進行下一步,把啟動腳本放到啟動目錄
我們先進入到mysql根目錄下的support-files目錄
[root@VM-0-15-centos mysql]# cd support-files [root@VM-0-15-centos support-files]# cp mysql.server /etc/init.d/mysql
啟動mysql
#啟動 [root@VM-0-15-centos support-files]# /etc/init.d/mysql start #停止 [root@VM-0-15-centos support-files]# /etc/init.d/mysql stop #重啟 [root@VM-0-15-centos support-files]# /etc/init.d/mysql restart #查看mysql進程 [root@VM-0-15-centos support-files]# ps -ef|grep mysql
看見這樣的提示就說明啟動成功了
Starting MySQL... SUCCESS!
五、登錄mysql
獲取默認密碼
在mysql根目錄下的sql_log目錄下有mysql的日志文件mysql_error.log
執行如下命令
[root@VM-0-15-centos sql_log]# grep password mysql-error.log
會得到類似的字符串
2021-01-07T02:31:11.723844Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: .utdq=g)E6!Z
root@localhost: 后面的就是密碼。賬號就是root
[root@VM-0-15-centos sql_log]# mysql -uroot -p
輸入默認密碼。登錄成功
可能會有人提示 mysql的密碼已經過期,這樣的話你可以進行如下操作,進行免密碼登錄