目錄
1.下載軟件包:
wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
2.使用yum在線安裝
rpm -ivh mysql80-community-release-el7-1.noarch.rpm
yum install mysql-server
安裝過程中會需要提示下載依賴,按y確認下載即可:
3.初始化MySQL:
mysqld --initialize
4.啓動MySQL:
# RedHat6 下啓動命令
service mysqld start
# RedHat7 下啓動命令
systemctl start mysqld.service
# 設置開機啓動
systemctl enable mysqld
systemctl daemon-reload
5.查看 MySQL 運行狀態:
# RedHat6 下查看mysql狀態
service mysqld status
# RedHat7 下查看mysql狀態
systemctl status mysqld.service
6.驗證:
mysqladmin --version
# 執行上述命令後顯示如下信息:
# mysqladmin Ver 8.0.18 for Linux on x86_64 (MySQL Community Server - GPL)
7.修改初始密碼
# MySQL 5.7 調整密碼驗證規則:
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
# MySQL 8.0 調整密碼驗證規則:
mysql> set global validate_password.policy=0;
mysql> set global validate_password.length=1;
# MySQL5.7 和 MySQL8.0 密碼驗證插件種變量名不一樣。(_password_policy 和 _password.policy )
# 用命令查看 validate_password 密碼驗證插件是否安裝。
mysql> SHOW VARIABLES LIKE ‘validate_password%’;
方式一:
1)剛剛啓動成功之後, 用命令查看默認密碼並且登錄
查看:cat /var/log/mysqld.log | grep password
登陸:mysql -u root -p # 然後輸入密碼
2)修改密碼
set global validate_password.policy=0;
set global validate_password.length=1;
ALTER USER "root"@"localhost" IDENTIFIED BY "1234"; ## 新密碼爲1234
3) exit 退出
mysql -u root -p ## 然後輸入密碼即可登錄
方式二:
1)停止mysql
systemctl stop mysqld.service
2)編輯配置文件
vi /etc/my.cnf
# 尾部加上:
skip-grant-tables
2)重啓mysql服務,修改密碼(密碼置空)
[root@localhost ~]# systemctl start mysqld.service
[root@localhost ~]# mysql -u root -p
Enter password: ##直接回車
mysql> use mysql; ## 進入mysql表
mysql> update user set authentication_string='' where user='root'; ## 將密碼設置爲空(必須在mysql庫設置)
mysql> exit
[root@localhost ~]# vi /etc/my.cnf ## 尾部去掉 skip-grant-tables
[root@localhost ~]# systemctl restart mysqld ## 重啓mysql
[root@localhost ~]# mysql -u root -p
Enter password: ##直接回車
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密碼';
mysql> flush privileges; ##刷新權限
mysql> exit ## mysql -u root -p 然後輸入新密碼即可登錄
8.授權遠程訪問
-- 創建所有用戶都可以訪問的用戶規則,在mysql8.0中 授權語句已經更換,使用之前的sql語句授權會報錯;
create user 'root'@'%' identified WITH mysql_native_password by 'Root@123';
-- 對這個用戶規則進行授權
grant all privileges on *.* to 'root'@'%';
flush privileges; -- 刷新權限
9.mysql常用配置
# 配置默認編碼爲utf8
# 修改/etc/my.cnf配置文件,在[mysqld]下添加編碼配置,如下所示:
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
# mysql -u root -p ##登陸
use mysql;
show databases;
show tables;
-- 查看用戶是否允許遠程登錄 localhost 僅本地 % 可以遠程登錄
select host,user, authentication_string, plugin from user; \G; -- \G表示格式化顯示
-- 修改root用戶可遠程登錄.
update user set host= '%' where user = 'root';
-- 修改密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的密碼';
ALTER USER "root"@"%" IDENTIFIED BY "你的密碼";
-- 修改爲舊的密碼認證方式.
use mysql;
alter USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的密碼';
flush privileges; #刷新權限
-- 修改加密規則 mysql5.7.6之後就修改了加密規則,所以需要更改加密方式。
update user set plugin='mysql_native_password' where user ='root';
-- 創建所有用戶都可以訪問的用戶規則,在mysql8.0中 授權語句已經更換,使用之前的sql語句授權會報錯;
create user 'root'@'%' identified WITH mysql_native_password by 'Root@123';
-- 對這個用戶規則進行授權
grant all privileges on *.* to 'root'@'%';
flush privileges; #刷新權限
10.重要目錄(默認安裝目錄)
/etc/my.cnf # 配置文件
/var/lib/mysql #數據庫目錄
/usr/share/mysql # 配置文件目錄
/usr/bin #相關命令目錄
/var/log/mysqld.log # log-error # 日誌文件
/var/run/mysqld/mysqld.pid # pid-file
/etc/init.d/mysql #啓動腳本
————————————————
11.重要命令
# 查看運行進程
ps -ef | grep mysqld
# 查看端口
netstat -tulpn |grep mysqld
## 服務操作:
# 啓動服務
systemctl start mysqld
# 重啓服務
systemctl restart mysqld
# 加入開機啓動
systemctl enable mysqld;
# 停止運行服務
systemctl stop mysqld ;
# 加入開機禁用
systemctl disable mysqld;
# 查看狀態
systemctl status mysqld;
# 查看httpd的開機啓動狀態列表
systemctl list-unit-files;