安裝Mysql8.0.16
- 下載并安裝cmake
如果安裝過不需要安裝
cd /http/package/
sudo wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
sudo tar -zxvf cmake-3.14.5.tar.gz
sudo ./bootstrap
sudo gmake
sudo gmake install
查看版本
cmake -version
- 下載并安裝ncurses
cd /http/package/
sudo wget ftp://ftp.invisible-island.net/ncurses/ncurses-6.1.tar.gz
sudo tar -zxvf ncurses-6.1.tar.gz
cd ncurses-6.1
sudo ./configure --prefix=/http/package/ncurses
sudo make
sudo make install
安裝libaio庫
sudo yum -y install libaio*
安裝libaio和libaio-devel這兩個(gè)庫為Mysql添加用戶及組
為了保證操作系統(tǒng)的安全淌哟,這里為mysql工作進(jìn)程創(chuàng)建專用的用戶
sudo groupadd mysql
sudo useradd -r -g mysql mysql
- 下載并安裝
下載解壓
cd /http/package/
sudo wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz
sudo tar -xvf mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz
sudo mv mysql-8.0.16-linux-glibc2.12-x86_64 /http/server/mysql
這里使用的是mysql官方提供的解壓縮版的安裝方式,不需要進(jìn)行make操作。將mysql文件夾移動(dòng)到安裝路徑后宰缤,需要修改mysql安裝文件夾的權(quán)限犁柜,執(zhí)行以下命令
sudo chown -R mysql.mysql /http/server/mysql/
后續(xù)如果在該目錄及其子目錄下創(chuàng)建了任何文件和文件夾柏肪,均需要再次執(zhí)行該命令顶滩,以確保mysqld進(jìn)程對(duì)該文件夾具有完全的讀寫權(quán)限
- 設(shè)置mysql配置文件
mysql的配置文件是放在/etc/
目錄下的骑疆,名字是my.cnf
敛滋,具體配置如下
[client]
port = 3306
socket = /http/server/mysql/tmp/mysql.sock
[mysqld]
basedir=/http/server/mysql
datadir=/http/server/mysql/data
socket=/http/server/mysql/tmp/mysql.sock
#skip-external-locking
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in [<u>http://fedoraproject.org/wiki/Systemd</u>](http://fedoraproject.org/wiki/Systemd)
log-error=/http/logs/mysql/error.log
pid-file=/http/logs/mysql/mysql.pid
[mysqld_safe]
log-error=/http/logs/mariadb/error.log
pid-file=/http/logs/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
這里需要注意许布,配置文件最后一行引入了新的配置文件,下面列出my.cnf.d目錄下名為mysql-clients.cnf的配置文件內(nèi)容
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
[mysql]
[mysql_upgrade]
[mysqladmin]
[mysqlbinlog]
[mysqlcheck]
[mysqldump]
[mysqlimport]
[mysqlshow]
[mysqlslap]
在my.cnf配置文件中绎晃,需要注意幾個(gè)配置蜜唾,其中basedir指的是mysql的安裝目錄杂曲,datadir指的是mysql數(shù)據(jù)文件的存儲(chǔ)目錄,其他幾項(xiàng)配置項(xiàng)袁余,感興趣可以自行查看官方解釋擎勘。還需要特別說明的一個(gè)問題是,所有在配置文件中出現(xiàn)的路徑泌霍、文件均需要具有可讀寫的權(quán)限,前面為MySQL創(chuàng)建了用戶及組朱转,所以上述配置文件中涉及到的路徑和文件蟹地,均需要將其所有者及組指定為mysql用戶藤为,這樣MySQL服務(wù)才能正常啟動(dòng)。
- 創(chuàng)建必要目錄和文件
創(chuàng)建目錄
sudo mkdir -p /http/server/mysql/data/ /http/server/mysql/tmp/ /http/logs/mysql/ /http/logs/mariadb/
創(chuàng)建文件
sudo touch /http/server/mysql/tmp/mysql.sock /http/logs/mysql/error.log /http/logs/mysql/mysql.pid /http/logs/mariadb/error.log /http/logs/mariadb/mariadb.pid
更改所有者
sudo chown -R mysql.mysql /http/server/mysql/data/ /http/server/mysql/tmp/ /http/logs/mysql/ /http/logs/mariadb/
- 初始化MySQL
因本文采用的是MySQL 8.0.16版本缅疟,所以不能使用mysql_install_db命令,而是需要使用mysqld –initialize命令對(duì)數(shù)據(jù)庫進(jìn)行初始化存淫,具體命令如下
sudo /http/server/mysql/bin/mysqld --initialize --user=mysql --basedir=/http/server/mysql/ --datadir=/http/server/mysql/data/
error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
原因:
yum 安裝的libnuma.so.1
,但安裝時(shí)默認(rèn)安裝的是32的桅咆,但db2需要的是64位的
解決辦法:
如果已經(jīng)安裝了libnuma.so.1
,先sudo yum remove libnuma.so.1
sudo yum -y install numactl.x86_64
若前面配置文件中的各配置項(xiàng)均正確岩饼,各路徑、文件均存在并可讀寫籍茧,該命令將執(zhí)行成功
使用安全模式啟動(dòng)MySQL
sudo /http/server/mysql/bin/mysqld_safe --user=mysql &
設(shè)置MySQL服務(wù)開機(jī)自啟
創(chuàng)建配置文件
sudo vim /lib/systemd/system/mysql.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/http/server/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#Restart=on-failure
#RestartPreventExitStatus=1
#PrivateTmp=false
systemctl start mysql
啟動(dòng)mysql
systemctl enable mysql
設(shè)置mysql開機(jī)啟動(dòng)
- 重置密碼
sudo vim /etc/my.cnf
在[mysqld]
里面增加
skip-grant-tables
重啟mysql
systemctl restart mysql
使用用戶無密碼登錄
/http/server/mysql/bin/mysql -uroot -p
(直接點(diǎn)擊回車,密碼為空)
選擇數(shù)據(jù)庫
use mysql;
將root密碼置空
update user set authentication_string = '' where user = 'root';
刷新權(quán)限
flush privileges;
退出
exit
刪除第1部增加的配置信息
skip-grant-tables
重啟mysql
systemctl restart mysql
更改新的密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
由于MySQL 8默認(rèn)使用了新的密碼驗(yàn)證插件:caching_sha2_password,如不支持請(qǐng)使用下面方式更改密碼
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';