對最近mysql的常用運維命令進行整理
查看使用的哪個配置文件啟動的mysql
1. ps aux|grep mysql|grep 'my.cnf'
如果啟動的命令中選擇了配置文件,則可以查詢出來,也可能查詢不到。
2. mysql --help|grep 'my.cnf'
輸出:
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /opt/app/mysql/etc/my.cnf ~/.my.cnf
啟動時選擇配置文件的默認順序從前到后。
3. locate my.cnf命令可以列出所有的my.cnf文件,或者使用find命令也可以
賬戶的創建修改和設置權限
1.查看當前已有用戶
連接mysql的通用方式,可以省略其中一部分參數 :
mysql -uroot -h 127.0.0.1 -P 3306 -p
注意:大寫P是端口 小寫p是密碼
2. create user創建用戶和修改密碼和刪除
2.1 創建用戶:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
例子:
CREATE USER 'vinter'@'localhost' IDENTIFIED BY '123456';
注意
用戶名和host必須加引號否則不能運行
create user vinter;
此種寫法賬戶無密碼,可以在任意客戶端任意ip下登錄
2.2 修改密碼:
2.2.1 最容易記住的,就是直接update user表,update成功后要使用 flush privileges;更新
例如
update user set password = password('111111') where user = 'vinter';
flush privileges;
2.2.2 使用mysqladmin語法:mysqladmin -u用戶名 -p舊密碼 password 新密碼
mysqladmin -u vinter -p 123 password 456;
2.2.3 使用set password 語句修改
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
如果是當前登陸用戶用SET PASSWORD = PASSWORD("newpassword");
set password forr 'vinter'@'%'=password('haojia');
2.3 刪除用戶:
DROP USER username@localhost;
3.使用grant語句設置權限
1. 賦予權限:
格式語句:
grant 權限1,權限2,...權限n on 數據庫名稱.表名稱 to 用戶名@用戶地址 identified by '連接密碼' [with grant option];
權限取值如下:
select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等
數據庫名和表名可以使用通配符來表示所有,比如 dbname. 和.
例子:
grant all privileges on testdb.* to '‘'vinte'r@'127.0.0.1' identified by '111111';
grant insert,delete,select,update on testdb.* to vinter@127.0.0.1 identified by '111111';
identified by 可以不寫,如果寫會更改密碼為新密碼。
注:如果帶了 with grant option則被授予權限的人,可以把此權限再轉授權(傳遞)給其他用戶。
2. 撤銷權限
語法:REVOKE 權限列表 ON db.table FROM 'username'@'localhost';
REVOKE ALL PRIVILEGES ON *.* FROM 'vinter'@'%';
revoke update on *.* from 'vinter'@'%' ;
設置遠程登錄
我們先說一下實現遠程登錄mysql的條件
首先mysql必須設置相關的權限,允許該賬戶遠程登錄,第二需要防火牆設置了3306端口的遠程訪問。
1. mysql設置遠程訪問
可以使用grant語句,可以直接更新user表中相關項。
例子中用戶為vinter,ip為192.168.1.111
//固定ip:
grant all privileges on *.* to 'vinter'@'192.168.1.111' identified by '123' with grant option;
insert into user (host,user,password) values('192.168.1.111','vinter',password('123'));
//不限制ip
grant all privileges on *.* to 'vinter'@'%' identified by '123' with grant option;
insert into user (host,user,password) values('%','vinter',password('123'));
2. 防火牆設置(centos7)
常用的如下幾個其他的見文后附錄:
- 添加一個端口
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,沒有此參數重啟后失效)
- 重新載入
firewall-cmd --reload
- 查看
firewall-cmd --zone=public --query-port=80/tcp
- 查看所有打開的端口
firewall-cmd --zone=public --list-ports
附錄 firewalld基本操作
1、firewalld的基本使用
啟動: systemctl start firewalld
關閉: systemctl stop firewalld
查看狀態: systemctl status firewalld
開機禁用 : systemctl disable firewalld
開機啟用 : systemctl enable firewalld
2.systemctl是CentOS7的服務管理工具中主要的工具,它融合之前service和chkconfig的功能於一體。
啟動一個服務:systemctl start firewalld.service
關閉一個服務:systemctl stop firewalld.service
重啟一個服務:systemctl restart firewalld.service
顯示一個服務的狀態:systemctl status firewalld.service
在開機時啟用一個服務:systemctl enable firewalld.service
在開機時禁用一個服務:systemctl disable firewalld.service
查看服務是否開機啟動:systemctl is-enabled firewalld.service
查看已啟動的服務列表:systemctl list-unit-files|grep enabled
查看啟動失敗的服務列表:systemctl --failed
3.配置firewalld-cmd
查看版本: firewall-cmd --version
查看幫助: firewall-cmd --help
顯示狀態: firewall-cmd --state
查看所有打開的端口: firewall-cmd --zone=public --list-ports
更新防火牆規則: firewall-cmd --reload
查看區域信息: firewall-cmd --get-active-zones
查看指定接口所屬區域: firewall-cmd --get-zone-of-interface=eth0
拒絕所有包:firewall-cmd --panic-on
取消拒絕狀態: firewall-cmd --panic-off
查看是否拒絕: firewall-cmd --query-panic
添加端口
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,沒有此參數重啟后失效)
重新載入
firewall-cmd --reload
查看
firewall-cmd --zone=public --query-port=80/tcp
刪除
firewall-cmd --zone=public --remove-port=80/tcp --permanent