安装
sudo apt-get update #更新源
sudo apt-get install mysql-server #安装
sudo service mysql status #查看MySQL状态
sudo service mysql stop #停止服务
sudo service mysql start #开启服务
sudo service mysql restart #重启服务
sudo cat /etc/mysql/debian.cnf #查看默认的账户和密码
#debian-sys-maint账号是在安装MySQL自动产生的,可以通过命令查看到它。
MySQL
MySQL配置文件的介绍:
配置文件路径为: /etc/mysql/mysql.conf.d/mysqld.cnf
port表示端口号,默认为3306
bind-address表示服务器绑定的ip,默认为127.0.0.1,需要远程登陆,必须注释
datadir表示数据库保存路径,默认为/var/lib/mysql
log_error表示错误日志,默认为/var/log/mysql/error.log
MySQL用户管理
mysql -u用户名 -p密码 #登陆MySQL
create user '用户名'@'%' identified by '你的密码' #创建用户
create user '用户名'@'%' identified WITH mysql_native_password by '你的密码' #创建用户,WITH mysql_native_password是mysql5的加密方式,默认是8的加密
alter user '用户名'@'localhost' IDENTIFIED by '你的密码'; #修改用户密码
#5.X 版本的 mysql_native_password ,
#8.X加密方式:caching_sha2_password
GRANT ALL ON *.* TO '用户名'@'%'; #给用户授权所有表格所有权限
GRANT ALL ON *.* TO '用户名'@'%' WITH GRANT OPTION; #
#with gran option表示该用户可给其它用户赋予权限,但不可能超过该用户已有的权限
# all privileges 可换成select,update,insert,delete,drop,create等操作如:
grant select,insert,update,delete on *.* to '用户名'@'localhost';
# 第一个*表示通配数据库,可指定新建用户只可操作的数据库
# 第二个*表示通配表,可指定新建用户只可操作的数据库下的某个表
show grants for '用户名'@'localhost'; #查看用户授权信息
revoke all privileges on *.* from '用户名'@'localhost'; #撤销权限
drop user '用户名'@'%'; #删除用户
flush privileges; #刷新
- 其中localhost指本地才可连接,可以将其换成%指任意ip都能连接,也可以指定ip连接