mysql5.7+和mariadb的root密码修改,添加用户,授权,远程连接等

 https://blog.csdn.net/hemowolf/article/details/50826940

其实想要重置 5.7 的密码很简单,就一层窗户纸:

1、修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1

这一行配置让 mysqld 启动时不对密码进行验证

2、重启 mysqld 服务:systemctl restart mysqld

3、使用 root 用户登录到 mysql:mysql -u root 

4、切换到mysql数据库,更新 user 表:

update user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where user = 'root';

在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string

5、退出 mysql,编辑 /etc/my.cnf 文件,删除 skip-grant-tables=1 的内容

6、重启 mysqld 服务,再用新密码登录即可

 

另外,MySQL 5.7 在初始安装后(CentOS7 操作系统)会生成随机初始密码,并在 /var/log/mysqld.log 中有记录,可以通过 cat 命令查看,找 password 关键字

找到密码后,在本机以初始密码登录,并且(也只能)通过 alter user 'root'@'localhost' identified by 'root' 命令,修改 root 用户的密码为 root,然后退出,重新以root用户和刚设置的密码进行登录即可。

 

删除root的远程连接

mysql>use mysql;

mysql>delete from user where user='root' and host='%';

mysql>flush privileges;

 

添加所有权限远程连接用户[root权限]

mysql>use mysql;

mysql> CREATE USER 'yx'@'%' IDENTIFIED BY 'Fk123456=';  #刚开始设置的密码必须符合长度8位以上,且必须含有数字,小写或大写字母,特殊字
Query OK, 1 row affected, 3 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on *.* to newuser@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

 

添加只读权限的用户

use mysql;
CREATE USER 'zys'@'%' IDENTIFIED BY 'Fk123456=';   mysql5.8需要加  CREATE USER 'zys'@'%'  IDENTIFIED WITH mysql_native_password BY 'Fk123456=';
flush privileges;
grant SELECT on admin.* to zys@'%'; #如果是所有库,那么就将admin.* 改为*.*
flush privileges;

 

mysql数据备份导出命令

mysqldump -uroot -p123456 --single-transaction game>game.sql  #导出备份

mysqldump -h183.xx.xx.xx -uusername -ppwd --single-transaction passport>passport.sql #远程导出备份

mysql -u root -p database < xx.sql  #导入

 

 

#########################################################################################################

#########################################################################################################

#########################################################################################################

这里mariadb配置

添加所有权限远程连接用户[root权限]

mysql>use mysql;

mysql> grant all on jumpserver.* to 'jumpserver'@'127.0.0.1' identified by 'weakPassword';  #添加并授权,这里0 row affected是正常的,主要注意mariadb的%只代表远程,内网的ip需要一个个的加,比如docker的内网,都需要按内网ip重新加一次,这个和mysql不太一样

mysql> grant all on jumpserver.* to 'jumpserver'@'localhost' identified by 'weakPassword'; 

mysql> grant all on jumpserver.* to 'jumpserver'@'docker_inter_addr' identified by 'weakPassword'; 


Query OK, 0 row affected, 3 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

 


分享到: 微信 更多