1. MySQL軟件下載
下載地址:
http://mysql.mirror.kangaroot.net/Downloads/MySQL-5.7/
http://mirrors.163.com/mysql/Downloads/MySQL-5.7/
wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-community-client-5.7.26-1.el7.x86_64.rpm wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-community-common-5.7.26-1.el7.x86_64.rpm wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-community-devel-5.7.26-1.el7.x86_64.rpm wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-community-libs-5.7.26-1.el7.x86_64.rpm wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-community-server-5.7.30-1.el7.x86_64.rpm
2. MySQL安裝
yum localinstall *.rpm
3. MySQL服務啟動
語法:
systemctl start | stop | restart | status mysqld
# 啟動MySQL服務,並將服務加入開機啟動
systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld
4. 登錄並配置MySQL
說明:mysql在安裝完成,啟動mysqld服務后,MySQL會自動生成一個隨機的root用戶密碼,可以到日志里面查看。
[root@cn-prom ~]# vim /var/log/mysqld.log 2020-09-16T01:48:30.469772Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-09-16T01:48:30.760416Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-09-16T01:48:30.809246Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-09-16T01:48:30.868591Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b90cb3b5-f7be-11ea-8843-00505687264b. 2020-09-16T01:48:30.869372Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-09-16T01:48:31.540430Z 0 [Warning] CA certificate ca.pem is self signed. 2020-09-16T01:48:31.716721Z 1 [Note] A temporary password is generated for root@localhost: l5s;PyK%(odX 2020-09-16T01:48:34.315475Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-09-16T01:48:34.317738Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.30) starting as process 14208 ...
可以看到類似如下信息:
2020-03-03T06:24:40.573789Z 1 [Note] A temporary password is generated for root@localhost: l5s;PyK%(odX
說明:用該臨時密碼登錄mysql后,必須要先修改密碼
[root@mydb-server001 opt]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 5.7.26 Copyright (c) 2000, 2019, 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> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> set password = PASSWORD('cbMiu3@db.com'); ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
說明:如果不符合MySQL密碼策略要求,會出現上述錯誤。
MySQL密碼復雜度配置是通過參數validate_password_policy來控制的,你可以去修改,但是建議你不要修改(如果要修改,參考下文描述)。
validate_password_policy 有以下取值:
mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | validate_password_check_user_name | OFF | | validate_password_dictionary_file | | | validate_password_length | 8 | | validate_password_mixed_case_count | 1 | | validate_password_number_count | 1 | | validate_password_policy | MEDIUM | | validate_password_special_char_count | 1 | +--------------------------------------+--------+ 7 rows in set (0.00 sec)
Policy Tests Performed
0 or LOW Length
1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file
默認是1,即MEDIUM,所以設置MySQL的密碼必須不小於8位,且必須含有數字,小寫或大寫字母,特殊字符。
5. 修改mysql密碼
mysql> set password = PASSWORD('NMm#t87TO2JL&Zq2'); mysql> set password = PASSWORD('cbMiu936@mydb.com'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql>
6. 設置mysql的中文編碼支持
# 修改/etc/my.cnf
vim /etc/my.cnf
在[mysqld]中添加參數,使得mariadb服務端支持中文
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
character_set_server = utf8
collation-server = utf8_bin
# 重啟mariadb服務,讀取my.cnf新配置
systemctl restart mysqld
# 登錄數據庫,查看字符編碼
mysql -uroot -p
輸入 \s 查看編碼
7. mysql常用命令
desc #查看表結構 create database #數據庫名 create table #表名 # 查看如何創建db的 show create database #庫名 # 查看如何創建table結構的 show create table 表名; # 修改mysql的密碼 set password = PASSWORD('mysqlrootuser686'); # 創建mysql的普通用戶,默認權限非常低 create user mcb@'%' identified by 'changbin.miao'; # 查詢mysql數據庫中的用戶信息 use mysql; select host,user,password from user; --模糊查詢匹配 show status like 'Thread_%'; SHOW VARIABLES LIKE 'validate_password%'; 如果我們的show status語句中不包含統計范圍關鍵字,則默認統計范圍為SESSION,也就是只統計當前連接的狀態信息。 如果我們需要查詢自當前MySQL啟動后所有連接執行的SELECT語句總數,我們可以執行如下語句: show global status like 'com_select'; --查看MySQL本次啟動后的運行時間(單位:秒) show status like 'uptime'; --查看select語句的執行數 show [global] status like 'com_select'; --查看insert語句的執行數 show [global] status like 'com_insert'; --查看update語句的執行數 show [global] status like 'com_update'; --查看delete語句的執行數 show [global] status like 'com_delete'; --查看試圖連接到MySQL(不管是否連接成功)的連接數 show status like 'connections'; --查看線程緩存內的線程的數量。 show status like 'threads_cached'; --查看當前打開的連接的數量。 show status like 'threads_connected'; --查看當前打開的連接的數量。 show status like 'threads_connected'; --查看創建用來處理連接的線程數。如果Threads_created較大,你可能要增加thread_cache_size值。 show status like 'threads_created'; --查看激活的(非睡眠狀態)線程數。 show status like 'threads_running'; --查看立即獲得的表的鎖的次數。 show status like 'table_locks_immediate'; --查看不能立即獲得的表的鎖的次數。如果該值較高,並且有性能問題,你應首先優化查詢,然后拆分表或使用復制。 show status like 'table_locks_waited'; --查看創建時間超過slow_launch_time秒的線程數。 show status like 'slow_launch_threads'; --查看查詢時間超過long_query_time秒的查詢的個數。 show status like 'slow_queries';
8. 給用戶添加權限
# 對所有庫和所有表授權所有權限
grant all privileges on *.* to 賬戶@主機名
# 給mcb用戶授予所有權限
grant all privileges on *.* to mcb@'%';
# 刷新授權表
flush privileges;
9. 授予遠程登錄權限
# 給apollo用戶授予所有權限
grant all privileges on *.* to mcb@'%';
# 給與root權限授予遠程登錄的命令
# centos這是密碼隨意設置
grant all privileges on *.* to root@'%' identified by 'centos';
# 此時可以在windows登錄linux的數據庫
# 連接服務器的mysql
mysql -umcb -p -h 服務器的地址