00 前言
因爲後臺服務需要用到mysql數據庫,但是因爲之前安裝過,密碼又忘掉了,嘗試了幾種方法,發現都不能跳過密碼登錄,所以乾脆把mysql卸載重裝了。
環境:CentOS 7。
安裝包:mysql 5.7。
01 卸載數據庫
# 卸載mysql服務
yum remove -y mysql mysql-server mysql-libs mysql-server;
# 刪除所有mysql相關數據
find / -name 'mysql*' | xargs rm -rf;
刪除其餘rpm包
rpm -qa|grep mysql
# 查詢出來的rpm使用yum remove掉或者使用rpm -e命令刪除
查出內容如下
[root@hostone lib]# rpm -qa|grep mysql
mysql57-community-release-el7-8.noarch
mysql-community-common-5.7.29-1.el7.x86_64
繼續刪除
rpm -e mysql57-community-release-el7-8.noarch
rpm -e mysql-community-common-5.7.29-1.el7.x86_64
02 安裝
下載安裝包
# 創建mysql安裝包存放文件夾
mkdir -p /usr/local/src/mysql
# 進入目錄
cd /usr/local/src/mysql
# 下載5.7 安裝包
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
# 配置安裝依賴
yum localinstall -y mysql57-community-release-el7-8.noarch.rpm
# 安裝數據庫
yum install -y mysql-community-server
# 數據庫啓動
systemctl start mysqld
03 登錄
第一次啓動,從mysqld.log中查看初始化密碼
# 查看初始化密碼
grep 'temporary password' /var/log/mysqld.log
可以看到,查詢密碼如下
[root@hostone mysql]# grep 'temporary password' /var/log/mysqld.log
2020-02-18T21:46:40.622767Z 1 [Note] A temporary password is generated for root@localhost: p8)<7:geYY;-
使用【mysql -u root -p】命令登錄,密碼爲p8)<7:geYY;-
[root@hostone mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
登錄成功,修改root密碼
mysql> SET PASSWORD = PASSWORD('Db@369012');
Query OK, 0 rows affected, 1 warning (0.00 sec)
可以看到,我的新密碼爲Db@369012。當然,我隨後會把這個密碼改掉。
04 創建數據庫、用戶及數據表
數據庫終於搞定了,接着就要創建我們需要的數據表了。
# 查看數據庫
show databases;
# 創建一個新的數據庫live
create database live;
# 選擇進入剛剛創建的live數據庫
use live;
# 查看mysql庫下有哪些數據表,可以看到是空的
show tables;
# 創建數據表
create table user( id varchar(20),
random_name varchar(50)
);
添加一個非root用戶
# use mysql,創建用戶live,並賦權
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON TUTORIALS.* TO 'live'@'%' IDENTIFIED BY 'Live@123';
# 給用戶賦權指定數據庫操作權限
grant all privileges on live.* to 'live'@'%';
# 刷新權限
FLUSH PRIVILEGES;
報錯遠程機器無法訪問,"Host '183.160.28.242' is not allowed to connect to this MySQL server"。
修改訪問權限
# 進入mysql DB
use mysql
# 修改host爲%
update user set host = '%';
# 再次查看
select host, user from user;
+------+---------------+
| host | user |
+------+---------------+
| % | live |
| % | mysql.session |
| % | mysql.sys |
| % | root |
+------+---------------+
4 rows in set (0.00 sec)
# 更改host後,發現客戶端還是無法連接,因爲沒有刷新權限
FLUSH PRIVILEGES;
刷新權限後可以了。
本文分享自微信公衆號 - 架構師之殤(ysistrue)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。