前言
MySQL是一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL AB 公司開發(fā),屬于 Oracle 旗下產(chǎn)品。MySQL是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)之一,在 WEB 應(yīng)用方面,MySQL是最好的 RDBMS (Relational Database Management System,關(guān)系數(shù)據(jù)庫管理系統(tǒng)) 應(yīng)用軟件之一。本文章收錄在MySQL性能優(yōu)化+原理+實戰(zhàn)專欄,點擊此處查看開篇介紹。
在系統(tǒng)的學(xué)習(xí)mysql數(shù)據(jù)庫的時候,有興趣的小伙伴們可以了解一下數(shù)據(jù)庫系統(tǒng)概念
一、生產(chǎn)中mysqlq啟動方式
在上一章節(jié)的時候,我們對mysql.server和mysqld_multi.server腳本做了修改,如果能修改到腳本初始化的狀態(tài)可以自己修改,如果不可以就重新安裝mysql。后面我們在修改配置文件的時候可以使用cp命令進行備份。
1.1 mysql的啟動原理
我們可以看看msyql腳本調(diào)用得關(guān)系如下:
service-->mysqld.safe-->mysqld-->啟動mysql
systemctl-->mysqld-->啟動mysql
mysqld.safe-->mysqld-->啟動mysql
mysqld-->啟動mysql
1、mysql.server會調(diào)用mysqld_safe,mysql_safe會調(diào)用mysqld
[root@mysql2 ~]# ll /usr/local/mysql/support-files/mysql.server
-rwxr-xr-x. 1 7161 31415 10599 4月 15 09:10 /usr/local/mysql/support-files/mysql.server
/usr/local/mysql/support-files/mysql.server
[root@mysql2 ~]# cat /usr/local/mysql/support-files/mysql.server
2、mysqld_safe
[root@mysql2 ~]# ll /usr/local/mysql/bin/mysqld_safe
-rwxr-xr-x. 1 7161 31415 27862 6月 8 2022 /usr/local/mysql/bin/mysqld_safe
mysqld_safe的啟動會同時啟動mysqld,也會監(jiān)控mysqld
3、mysqld
[root@mysql2 ~]# which mysqld
/usr/local/mysql/bin/mysqld
[root@mysql2 ~]# ll /usr/local/mysql/bin/mysqld
-rwxr-xr-x. 1 7161 31415 255653197 6月 8 2022 /usr/local/mysql/bin/mysqld
4、mysqld_multi:用來啟動多實例的
[root@mysql2 install]# which mysqld_multi
/usr/local/mysql/bin/mysqld_multi
1.2 參數(shù)文件的優(yōu)先級
my.cnf可以看做是mysql的參數(shù)文件,和oracle一樣可以存在多個地方。不同的是不指定參數(shù)文件的情況下,oracle啟動的時候只會讀取優(yōu)先順序第一的參數(shù)文件,而mysql則有點不同,會按照參數(shù)文件的等級來陸續(xù)讀取。
上一章節(jié)的時候我們啟動數(shù)據(jù)庫需要指定參數(shù)文件,如果我們的參數(shù)文件在指定的位置,就不需要指定參數(shù),我們可以使用ysqld --verbose --help | grep my.cnf命令查詢mysql默認(rèn)參數(shù)位置
[root@mysql2 ~]# mysqld --verbose --help | grep my.cnf
mysqld: Can't change dir to '/opt/mysql/mysql-5.7.39-linux-glibc2.12-x86_64/data/' (Errcode: 2 - No such file or directory)
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default
優(yōu)先級的高低順序從右到左,讀取順序是從左到右。前面兩個可以視為全局參數(shù) , 后面兩個可以視為用戶選項參數(shù)。
也可以這么理解 ,4個參數(shù)同時存在的話,系統(tǒng)會按照優(yōu)先等級來讀取參數(shù)文件
- 先讀取等級較低的/etc/my.cnf
- 接著讀取/etc/mysql/my.cnf
- 接著讀取–>/usr/local/mysql/etc/my.cnf
- 最后讀取~/.my.cnf參數(shù)文件
最后只能應(yīng)用~/.my.cnf參數(shù)文件里的所有參數(shù)。
如果這幾個目錄里都沒有參數(shù)文件,在安裝,啟動,停止的時候需要我們手動指定參數(shù)文件。如下:
[root@mysql2 ~]#nohup mysqld --defaults-file=/data/mysql/my3306/my3306.cnf &
1.3 以server的方式啟動mysql(實際啟動mysql方式)
1、首先我們先對server進行備份,在將server文件移到 /etc/init.d目錄下面,成為一個常規(guī)的服務(wù)文件
[root@mysql2 ~]# cp /usr/local/mysql/support-files/mysql.server /usr/local/mysql/support-files/mysql.server.bak
[root@mysql2 ~]# cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
cp:是否覆蓋"/etc/init.d/mysqld"? y
2、接著將mysql參數(shù)文件移到 /etc 目錄下,作為mysql的全局參數(shù)文件
[root@mysql2 ~]# cp /data/mysql/my3306/my3306.cnf /etc/my.cnf
cp:是否覆蓋"/etc/init.d/mysqld"? y
3、通過服務(wù)器啟動mysql
[root@mysql2 ~]# /etc/init.d/mysqld start
Starting MySQL SUCCESS!
[root@mysql2 ~]# 2023-04-15T02:40:43.366648Z mysqld_safe A mysqld process already exists
[root@mysql2 ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 13462 mysql 32u IPv6 1611543 0t0 TCP *:mysql (LISTEN)
[root@mysql2 ~]# ps -ef | grep mysql
4、添加到mysqld到service
[root@mysql2 ~]# chkconfig --add /etc/init.d/mysqld
[root@mysql2 ~]# chkconfig --list mysqld
注:該輸出結(jié)果只顯示 SysV 服務(wù),并不包含
原生 systemd 服務(wù)。SysV 配置數(shù)據(jù)
可能被原生 systemd 配置覆蓋。
要列出 systemd 服務(wù),請執(zhí)行 'systemctl list-unit-files'。
查看在具體 target 啟用的服務(wù)請執(zhí)行
'systemctl list-dependencies [target]'。
mysqld 0:關(guān) 1:關(guān) 2:開 3:開 4:開 5:開 6:關(guān)
1.4 mysqld_safe方式啟動
啟動命令:nohup mysqld_safe --defaults-file=/data/mysql/my3306/my3306.cnf
如果參數(shù)文件在默認(rèn)位置,就可以不用指定
[root@mysql2 ~]# nohup mysqld_safe &
[1] 15524
[root@mysql2 ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 15953 mysql 31u IPv6 1647497 0t0 TCP *:mysql (LISTEN)
[root@mysql2 ~]# ps -ef | grep mysql
1.5 mysqld方式啟動
啟動命令:nohup mysql --defaults-file=/data/mysql/my3307/my3307.cnf
如果參數(shù)文件在默認(rèn)位置,就可以不用指定
[root@mysql2 ~]# nohup mysqld &
[1] 16985
[roott@mysql2 ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 16985 mysql 31u IPv6 1611742 0t0 TCP *:mysql (LISTEN)
[root@mysql2 ~]# ps -ef | grep mysql
1.6 systemctl方式啟動(CentOS 7)
[root@mysql2 ~]# cat <<EOF>> /etc/systemd/system/my3306.service
[Unit]
Description=MySQL Server by
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld ##--defaults-file=/data/mysql/my3306/my3306.cnf
LimitNOFILE = 5000
EOF
[root@mysql2 ~]# more /etc/systemd/system/my3306.service
[root@mysql2 ~]# systemctl start my3306.service
[root@mysql2 ~]# systemctl status my3306.service
● my3306.service - MySQL Server by
Loaded: loaded (/etc/systemd/system/my3306.service; disabled; vendor preset: disabled)
Active: active (running) since 六 2023-04-15 11:11:46 CST; 7s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 17671 (mysqld)
Tasks: 27
CGroup: /system.slice/my3306.service
└─17671 /usr/local/mysql/bin/mysqld
4月 15 11:11:46 mysql2.x88.com systemd[1]: Started MySQL Server by.
[root@mysql2 ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 17671 mysql 31u IPv6 1629942 0t0 TCP *:mysql (LISTEN)
常用命令 start status stop enable disanble 等來管理數(shù)據(jù)庫的啟停
二、關(guān)庫
2.1 相關(guān)參數(shù)innodb_fast_shutdown (數(shù)據(jù)庫關(guān)閉參數(shù))
[root@mysql2 my3306]# mysql -uroot -p --socket=/data/mysql/my3306/mysql.sock
mysql> select @@innodb_fast_shutdown ;
+------------------------+
| @@innodb_fast_shutdown |
+------------------------+
| 1 |
+------------------------+
1 row in set (0.00 sec)
在關(guān)閉時,參數(shù)innodb_fast_shutdown影響著表的存儲引擎為InnoDB的行為。該參數(shù)可取值為0、1、2,默認(rèn)值為1。
- 0表示在MySQL數(shù)據(jù)庫關(guān)閉時,InnoDB需要完成所有的full purge(回收頁)和merge insert buffer(合并插入緩沖區(qū)),并且將所有的臟頁刷新回磁盤。這需要一些時間,有時甚至需要幾個小時來完成。如果在進行InnoDB升級時,必須將這個參數(shù)調(diào)為0,然后再關(guān)閉數(shù)據(jù)庫。
- 1是參數(shù)innodb_fast_shutdown的默認(rèn)值,表示不需要完成上述的full purge和merge insert buffer操作,但是在緩沖池中的一些數(shù)據(jù)臟頁還是會刷新回磁盤。生產(chǎn)環(huán)境推薦的參數(shù)
- 2表示不完成full purge和merge insert buffer操作,也不將緩沖池中的數(shù)據(jù)臟頁寫回磁盤,而是將日志都寫入日志文件。這樣不會有任何事務(wù)的丟失,但是下次mysql數(shù)據(jù)庫啟動時,會進行恢復(fù)操作(recovery)。如果寫入日志文件出現(xiàn)異常,可能導(dǎo)致數(shù)據(jù)啟動不了。
2.2 相關(guān)參數(shù)innodb_force_recovery(恢復(fù)參數(shù))
mysql> select @@innodb_force_recovery ;
+-------------------------+
| @@innodb_force_recovery |
+-------------------------+
| 0 |
+-------------------------+
1 row in set (0.00 sec)
當(dāng)正常關(guān)閉mysql數(shù)據(jù)庫時,下次的啟動應(yīng)該是非?!罢!?。但是如果沒有正常地關(guān)閉數(shù)據(jù)庫,如用kill命令關(guān)閉數(shù)據(jù)庫,在mysql數(shù)據(jù)庫運行中重啟了服務(wù)器,或者在關(guān)閉數(shù)據(jù)庫時,將參數(shù)innodb_fast_shutdown設(shè)為2,下次mysql數(shù)據(jù)庫啟動時都會對InnoDB存儲引擎的表進行恢復(fù)操作.
參數(shù)innodb_force_recovery影響了整個InnoDB存儲引擎恢復(fù)的狀況。該參數(shù)值默認(rèn)為0,代表當(dāng)發(fā)生需要恢復(fù)時,進行所有的恢復(fù)操作,當(dāng)不能進行有效恢復(fù)時,如數(shù)據(jù)頁發(fā)生了corruption(壞塊),mysqL數(shù)據(jù)庫可能發(fā)生宕機(crash),并把錯誤寫入錯誤日志中去。
但是,在某些情況下,可能并不需要進行完整的恢復(fù)操作,比如在對一個表進行alter table操作時發(fā)生意外了,數(shù)據(jù)庫重啟時會對InnoDB表進行回滾操作,對于一個大表來說這需要很長時間,可能是幾個小時。這時用戶可以自行進行恢復(fù),例如把表刪除,從備份中重新導(dǎo)入數(shù)據(jù)到表,可能這些操作的速度要遠(yuǎn)遠(yuǎn)快于回滾操作。
參數(shù)innodb_force_recovery還可以設(shè)置為6個非零值:1到6。大的數(shù)字表示包含了前面所有小數(shù)字表示的影響。具體情況如下:
- 1(SRV_FORCE_IGNORE_CORRUPT):忽略檢查到的corrupt頁。
- 2(SRV_FORCE_NO_BACKGROUND):阻止Master Thread線程的運行,如MasterThread線程需要進行full purge(完整清理)操作,而這會導(dǎo)致crash。
- 3(SRV_FORCE_NO_TRX_UNDO):不進行事務(wù)的回滾操作。
- 4(SRV_FORCE_NO_IBUF_MERGE):不進行插入緩沖的合并操作。
- 5(SRV_FORCE_NO_UNDO_LOG_SCAN):不查看撤銷日志(Undo Log),InnoDB存儲引擎會將未提交的事務(wù)視為已提交。
- 6(SRV_FORCE_NO_LOG_REDO):不進行前滾的操作。
建議:生產(chǎn)環(huán)境只能設(shè)置為0。即遇到故障,系統(tǒng)就要自動進行所有恢復(fù)操作。如果啟動不了,就調(diào)整該參數(shù)進行數(shù)據(jù)庫的調(diào)整。
2.2 關(guān)閉mysql多種方式
關(guān)閉腳本 | 方法說明 |
---|---|
/etc/init.d/mysqld stop | 使用stop參數(shù)調(diào)用時關(guān)閉本地服務(wù)器 |
mysqladmin -uroot -p123456 shutdown | 作為客戶機連接到服務(wù)器并且關(guān)閉服務(wù)器 |
mysql> shutdown; | 作為客戶機連接到服務(wù)器并且關(guān)閉服務(wù)器(有管理員(root)的權(quán)限) |
kill -9 pid | 殺掉mysqld進程,不建議使用 |
三、常見的mysql啟動失敗案例
3.1 目錄權(quán)限
目錄權(quán)限問題導(dǎo)致MySQL啟動失敗
[ERROR] Could not open file '/data/mysql/my3306/logs/error.log' for error logging: Permission denied
原因:數(shù)據(jù)目錄的用戶和用戶組不是我們設(shè)置的用戶
解決:
[root@mysql2 ~]# chown -R mysql:mysql /data/*
[root@mysql2 ~]# chmod -R 750 /data/mysql/
3.2 參數(shù)問題
在修改配置文件參數(shù)時,可能因為參數(shù)拼寫錯誤或者參數(shù)因版本不再支持,在錯誤日志會記錄內(nèi)容,在mysql啟動關(guān)閉及在運行時的錯誤
[ERROR] unknown variable 'basedi=/usr/local/mysql'
解決:在配置文件將錯誤的參數(shù)修改
3.3 配置文件
在啟動時,mysqld 如果默認(rèn)位置沒有my.cnf,則要通過–defaults-file指定正確的配置文件
[root@mysql2 ~]# mysqld --verbose --help |grep defaults
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file,
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=#
--explicit-defaults-for-timestamp
Dump only the hottest N% of each buffer pool, defaults to
explicit-defaults-for-timestamp FALSE
[root@mysql2 ~]# nohup mysqld_safe --defaults-file=/data/mysql/my3306/my3306.cnf
3.4 端口占用
端口被占用導(dǎo)致MySQL啟動失敗
[ERROR] Do you already have another mysqld server running on port: 3306 ?
在多實例的環(huán)境下,因為某幾個實例的端口是一樣的,導(dǎo)致啟動時造成端口的沖突,而無法啟動,我們需要修改端口
3.5 誤刪二進制文件
二進制日志文件保的記錄已提交的dml、ddl、dcl,主要用來主從復(fù)制、增量備份,而我們手工刪除二進制文件導(dǎo)致mysql啟動失敗
[ERROR] Failed to open log (file '/data/mysql/my3306/logs/mysql-bin.000012', errno 2)
解決:vim mysql-bin.index 將對應(yīng)的二進制日志文件記錄刪除即可
3.6 undo表空間異常
修改共享表空間參數(shù)大小導(dǎo)致MySQL啟動失敗,而共享表空間:ibdata1里保存元數(shù)據(jù)信息、insert buffer、double write、undo、回滾指針
參數(shù):innodb_data_file_path=ibdata1:12M:autoextend
[ERROR] InnoDB: The Auto-extending innodb_system data file './ibdata1' is of a different size 768 pages (rounded down to MB) than specified in the .cnf file: initial 8192 pages, max 0 (relevant if non-zero) pages!
解決:在初始化數(shù)據(jù)庫之前設(shè)置innodb_data_file_path的大小。重新修改配置文件中innodb_data_file_path的大小,必須是小于默認(rèn)的大?。?2M)或者等于默認(rèn)的大小
3.7 binlog緩沖異常
報錯
mysql> call insert_emp(1,4000000);
ERROR 1534 (HY000): Writing one row to the row-based binary log failed
+---------------------+
| @@binlog_cache_size |
+---------------------+
| 2097152 |
+---------------------+
1 row in set (0.00 sec)
因為內(nèi)存不足導(dǎo)致的binlog cache size不夠不能寫入binlog,導(dǎo)致語句無法執(zhí)行 在配置文件中調(diào)整binlog_cache_size和max_binlog_cache_size參數(shù)的值,改大一點
binlog_cache_size = 128M(binlog緩存根據(jù)實際情況調(diào)大)
max_binlog_cache_size = 512M (binlog日志根據(jù)實際情況調(diào)大)
四、mysql啟動失敗的排查方法
首選我們需要明確當(dāng)前的數(shù)據(jù)庫版本,啟動方式和配置文件等,查看錯誤日志文件
mysql> select @@log_error;
+-----------------------------------+
| @@log_error |
+-----------------------------------+
| /data/mysql/my3306/logs/error.log |
+-----------------------------------+
1 row in set (0.01 sec)
mysql> show variables like '%log_error%';
+---------------------+-----------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------+
| binlog_error_action | ABORT_SERVER |
| log_error | /data/mysql/my3306/logs/error.log |
| log_error_verbosity | 3 |
+---------------------+-----------------------------------+
3 rows in set (0.00 sec)
五、連接mysql的方式
5.1 本地連接
[root@mysql2 ~]# mysql -uroot -p123456 --socket=/data/mysql/my3306/mysql.sock
如果不想指定套接字文件,我們可以使用軟連接到默認(rèn)的指定位置
[root@mysql2 ~]# ln -sf /data/mysql/my3306/mysql.sock /tmp/mysql.sock
[root@mysql2 ~]# 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 5
Server version: 5.7.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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>
5.2 遠(yuǎn)程連接
- -u 用戶名
- -p 密碼
- -P 端口
- -h 遠(yuǎn)程地址
[root@mysql1 ~]# mysql -uroot -pCjlr2023! -P3306 -h10.30.0.60
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 8
Server version: 5.7.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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>
六、mysql數(shù)據(jù)庫用戶安全策略
6.1 數(shù)據(jù)庫的初始化
方式一: –initialize-insecure 默認(rèn)管理員root,密碼為空
如果mysql參數(shù)文件在默認(rèn)位置,可以忽略
[root@mysql1 ~]# mysqld --defaults-file=/data/mysql/my3306/my3306.cnf --initialize-insecure --user=mysql
mysql登陸
-S 參數(shù) = --socket=/data/mysql/my3306/mysql.sock,如果套接字文件在默認(rèn)位置,可以忽略
[root@mysql1 ~]# mysql -uroot -S /data/mysql/my3306/mysql.sock
方式二(推薦使用): –initialize 默認(rèn)管理員root,生成密碼;保存在error.log
如果mysql參數(shù)文件在默認(rèn)位置,可以忽略
[root@mysql1 ~]# mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data
6.2 修改密碼
方法一:登陸數(shù)據(jù)庫,修改密碼
[root@mysql1 ~]# mysql -uroot -pCjlr2023! -P3306 -h10.30.0.60
mysql> alter user root@'%' identified by '123456';
mysql> flush privileges;
方法二:mysqladmin修改密碼
[root@mysql2 ~]# mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
方法三:set password修改密碼
? 使用默認(rèn)加密
mysql> set password for 'test'@'%' ='abcdefg';
Query OK, 0 rows affected (0.01 sec)
? 使用PASSWORD()函數(shù)加密(mysql使用該方式)
mysql> set password for 'test'@'%' =password('123456');
Query OK, 0 rows affected (0.01 sec)
6.3 刪除無用用戶
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user from user;
+---------------+
| user |
+---------------+
| root |
| mysql.session |
| mysql.sys |
| root |
+---------------+
4 rows in set (0.00 sec)
mysql> delete from user where user !='root';
Query OK, 2 rows affected (0.01 sec)
mysql> select user from user;
+------+
| user |
+------+
| root |
| root |
+------+
2 rows in set (0.00 sec)
mysql> grant all privileges on *.* to 'multis'@'%' identified by '123456' with grant option;
mysql> grant all privileges on *.* to 'multis'@'localhost' identified by '123456' with grant option;
mysql> flush privileges;
6.4 mysql_secure_installation(推薦使用該方式修改密碼)
用于安裝后,權(quán)限安全的初始化
[root@mysql2 ~]# mysql_secure_installation -uroot -p --socket=/data/mysql/my3306/mysql.sock
#輸入日志里密碼
Enter password:
Securing the MySQL server deployment.
The existing password for the user account root has expired. Please set a new password.
#設(shè)置新密碼
New password:
#確認(rèn)新密碼
Re-enter new password:
#是否設(shè)置驗證密碼插件
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
... skipping.
# 是否刪除匿名賬戶
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
... skipping.
# 是否禁止遠(yuǎn)程登錄root用戶
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
... skipping.
# 是否刪除test 數(shù)據(jù)庫
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
... skipping.
# 是否重新加載特權(quán)表
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
... skipping.
All done!
6.5 找回丟失的用戶密碼
方式一: 管理員root密碼丟失,無法登錄數(shù)據(jù)庫,我們關(guān)閉數(shù)據(jù)后可以在參數(shù)文件中添加skip-grant-tables參數(shù)
[root@mysql2 ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/my3306/data
socket = /data/mysql/my3306/mysql.sock
server_id = 1
port = 3306
log_error=/data/mysql/my3306/logs/error.log
log_bin=/data/mysql/my3306/logs/mysql-bin
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
max_connections=1024
wait_timeout=60
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
innodb_buffer_pool_size=128M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=2
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/my3306/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
skip-grant-tables
[root@mysql2 ~]# mysqld &
[root@mysql2 ~]# mysql
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> alter user root@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
密碼修改完成,關(guān)閉數(shù)據(jù)庫。注釋到skip-grant-tables參數(shù)
mysql> shutdown;
Query OK, 0 rows affected (0.00 sec)
啟動數(shù)據(jù)庫
[root@mysql2 ~]# mysqld &
方式二 :文章來源:http://www.zghlxwxcb.cn/news/detail-414311.html
[root@mysql2 ~]# mysqld --skip-grant-tables &
[root@mysql2 ~]# mysql
mysql> flush privileges;
mysql> alter user root@'localhost' identified by '123456';
mysql> flush privileges;
mysql> shutdown;
mysql> exit;
[root@mysql2 ~]# nohup mysqld &
[root@mysql2 ~]# mysqld -uroot -p
文章來源地址http://www.zghlxwxcb.cn/news/detail-414311.html
到了這里,關(guān)于【mysql性能調(diào)優(yōu) ? 二】mysql的啟動關(guān)閉原理和實戰(zhàn),及常見的錯誤排查的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!