0、背景
有一些知識很簡單,很低頻,但是偶爾還是需要使用,故而需要記下來。
開始的時候碰見mysql.sock文件找不到了,不知道什麼時候被破壞了,修復了好久,問題一個接着一個,需要急着用,很浪費時間,所以直接選擇重裝。簡單暴力有效。
1、安裝
yum安裝mysql
# 下載mysql源
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
# 添加mysql源
yum localinstall mysql57-community-release-el7-8.noarch.rpm
# 安裝mysql
yum install mysql-community-server
檢查mysql源是否安裝成功
# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 141
mysql-tools-community/x86_64 MySQL Tools Community 105
mysql57-community/x86_64 MySQL 5.7 Community Server 404
2、啓動
然後就是啓動服務
systemctl start mysqld
到這裏算是安裝、啓動完成了
驗證下,有下面這個進程,差不多就說明ok了。
# ps -ef | grep mysql
mysql 31427 1 9 10:19 ? 00:16:14 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
3、初始密碼
安裝完mysql,有一個初始密碼,在/var/log/mysqld.log文件中,找到下面這個位置,例如:
[Note] A temporary password is generated for root@localhost: VVwAk;E6y/6a
這裏,VVwAk;E6y/6a 就是我的初始密碼了,每個人的不一樣。
4、修改初始密碼
初始密碼太複雜,一般都需要修改下。先用初始密碼登入:
# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.29 MySQL Community Server (GPL)
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.
mysql>
這裏插播一個錯誤
一般修改密碼用這個命令就可以了。
# mysqladmin -u root -p password "abc123"
Enter password:
// 這裏abc123是新密碼,Enter password:地方輸入原密碼
但是,新密碼強度太弱,不允許設置簡單密碼,報錯信息如下
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
mysqladmin: unable to change password; error: 'Your password does not satisfy the current policy requirements'
所以,需要降低下密碼強度的配置
5、降低初始密碼強度配置
# mysql -u root -pXXXXXXX
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
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.
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_number_count=3;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_special_char_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=3;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
上面最查看時有個錯誤,意思是需要重設密碼,繼續如下:(設置新密碼爲123456)
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 3 |
| validate_password_mixed_case_count | 0 |
| validate_password_number_count | 3 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 0 |
+--------------------------------------+-------+
7 rows in set (0.00 sec)
mysql> exit
OK,到這裏密碼重新設置完成
6、遠程登入配置
mysql允許遠程連接的命令就如下2條
mysql> grant all on *.* to root@'%' identified by '123456' with grant option;
mysql> flush privileges;
執行效果如下
# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.29 MySQL Community Server (GPL)
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.
mysql> grant all on *.* to root@'%' identified by '123456' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
好了,到這裏就可以愉快的使用MySQL了。