1. 程式人生 > 資料庫 >Linux下CentOS 7安裝mysql8.0.18以及自啟動

Linux下CentOS 7安裝mysql8.0.18以及自啟動

本次安裝的mysql版本是mysql8.0.18,安裝包在這裡,有需要的小夥伴點選下載,提取碼:qbe3。如需其他版本,點選這裡前往官網下載對應版本。

一、檢查Linux是否安裝了mysql

#使用如下命令,如果有的話需要先解除安裝乾淨才能夠繼續下一步安裝
rpm -qa|grep -i mysql

#移除系統原來的mysql包,以免影響後續安裝
rpm -qa|grep mariadb
rpm -e mariadb-libs-5.5.44-2.el7.centos.x86_64
yum remove mariadb-libs-5.5.44-2.el7.centos.x86_64

二、使用安裝包傳到Linux,我用的工具是Xftp

#使用tar命令解壓安裝包
tar -xvf mysql-8.0.18-linux-glibc2.12-x86_64.tar

#解壓mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz檔案
tar -xvf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz

#解壓後的資料夾重新命名並移動到/usr/local目錄下
 mv mysql-8.0.18-linux-glibc2.12-x86_64 mysql
 mv mysql /usr/local

三、開始mysql的配置

cd /usr/local

#新增使用者組合使用者
groupadd mysql
useradd -f -g mysql mysql

#授權
chown -R mysql:mysql ./

#在/usr/local/mysql下建立data資料夾
mkdir data

#初始化資料庫
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

初始化資料庫後會得到如下資訊,其root@localhost後面的為初始密碼,需要記住後面要用

2020-01-29T08:17:34.115343Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.18) initializing of server in progress as process 3040
2020-01-29T08:17:37.355887Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 1ams)egw_38P

#修改/usr/local/mysql 當前目錄的使用者
chown -R root:root ./
chown -R mysql:mysql data

#複製my-default.cnf檔案到etc/my.cnf
cd support-files/
touch my-default.cnf
chmod 777 ./my-default.cnf 
cd ../
cp support-files/my-default.cnf /etc/my.cnf**

#配置my.cnf

內容如下:

[mysqld]
 
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server,else 10%.
# innodb_buffer_pool_size = 128M
 
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
 
# These are commonly set,remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
tmpdir = /tmp
port = 5186
#lower_case_table_names = 1
# server_id = .....
# socket = .....
#lower_case_table_names = 1
max_allowed_packet=32M
default-authentication-plugin = mysql_native_password
#lower_case_file_system = on
#lower_case_table_names = 1
log_bin_trust_function_creators = ON
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed,experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

四、開機自啟動

#拷貝mysql.server到init.d並重命名為mysql
cd support-files/
cp mysql.server /etc/init.d/mysql 
chmod +x /etc/init.d/mysql

#註冊服務
chkconfig --add mysql
export PATH=/sbin:$PATH
chkconfig
PATH="PATH":/sbin
echo $PATH

#使用命令檢視是否註冊成功
config --list mysql

#etc/ld.so.conf新增內容配置路徑
/usr/local/mysql/lib

#配置環境變數
vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib

#立即生效
source /etc/profile

#啟動mysql服務
service mysql start
mysql -uroot -p[初始化密碼]

#修改密碼
alter user 'root'@'localhost' identified by '123456';