建立mysql使用者、使用者組
groupadd mysql
useradd -g mysql mysql
passwd mysql
解除安裝系統自帶的mariadb
#查詢
rpm -qa|grep mariadb
返回 mariadb-libs-5.5.60-1.el7_5.x86_64
#解除安裝
rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64
返回
error: Failed dependencies:
libmysqlclient.so.18()(64bit) is needed by (installed) postfix-2:2.10.1-7.el7.x86_64
libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) postfix-2:2.10.1-7.el7.x86_64
#強行解除安裝
rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
下載mysql rpm
下載mysql rpm
選擇
Red Hat Enterprise Linux / Oracle Linux
Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)
RPM Bundle
取得下載地址
https://cdn.mysql.com//Downlo…
wget "https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.13-1.el7.x86_64.rpm-bundle.tar"
tar -xvf mysql-8.0.13-1.el7.x86_64.rpm-bundle.tar
安裝
安裝依賴
yum install -y libaio.x86_64 libaio-devel.x86_64
yum install -y openssl-devel.x86_64 openssl.x86_64
yum install -y perl.x86_64 perl-devel.x86_64
yum install -y perl-JSON.noarch
yum install -y autoconf
必要安裝,注意順序,有依賴關係
rpm -ivh mysql-community-common-8.0.13-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.13-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.13-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.13-1.el7.x86_64.rpm
非必要安裝,注意順序,有依賴關係
rpm -ivh mysql-community-libs-compat-8.0.13-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-compat-8.0.13-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-8.0.13-1.el7.x86_64.rpm
rpm -ivh mysql-community-test-8.0.13-1.el7.x86_64.rpm
初始化資料庫
mysqld --initialize --console
修改目錄許可權
此步驟必須在初始化資料庫之後!!!
安裝完預設是mysql 使用者、使用者組的許可權,如果沒有,則更改
chown -R mysql:mysql /var/lib/mysql/
用root使用者啟動
systemctl start mysqld
若啟動失敗,檢視日誌
tail -n 100 /var/log/mysqld.log
檢視初始化時的密碼
啟動成功後可以檢視初始化密碼隨機生成的
cat /var/log/mysqld.log | grep password
登入MySQL修改mysql使用者密碼
mysql -u root -p
#修改密碼
alter user `root`@`localhost` identified by `yourpassword`;
#遠端設定
use mysql;
update user set host=`%` where user=`root`;
#授權使用者名稱的許可權,賦予任何主機訪問資料的許可權
grant all privileges ON *.* to `root`@`%` with grant option;
flush privileges;
新增mysql使用者,建立資料庫,授權
create user `flame`@`%` identified by `password`;
create database flame;
grant all privileges on flame.* to `flame`@`%`;
flush privileges;
centos7防火牆開放埠
# 開放3306埠
firewall-cmd --zone=public --add-port=3306/tcp --permanent
#過載
firewall-cmd --reload
#檢視狀態
firewall-cmd --list-all
阿里雲埠安全組設定
安全組說明
新增3306埠的新增【入方向】規則
生產環境
mysql_secure_installation
會做以下操作
- 為root使用者設定密碼
- 刪除匿名賬號
- 取消root使用者遠端登入
- 刪除test庫和對test庫的訪問許可權
- 重新整理授權表使修改生效
相關管理命令
#啟動mysql
systemctl start mysqld.service
#結束
systemctl stop mysqld.service
#重啟
systemctl restart mysqld.service
#開機自啟
systemctl enable mysqld.service