mysql重置root密码

英文原文参考地址

1、检查版本
mysql –version
MySQL output

mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper
Or output like this for MariaDB:

MariaDB output
mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1

2、停止服务

MySQL with:
sudosystemctl stop mysql

MariaDB wtih:
sudosystemctl stop mariadb

3、开启安全模式
sudo mysqld_safe –skip-grant-tables –skip-networking &

4、使用root免密码登录
mysql -uroot

MySQL prompt
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
MariaDB prompt
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MariaDB [(none)]>

5、FLUSH PRIVILEGES;
非常关键,必须先执行这个,然后执行后面的语句才有效

6、修改密码
For MySQL 5.7.6 and newer as well as MariaDB 10.1.20 and newer, use the following command.

ALTER USER’root’@’localhost’IDENTIFIED BY ‘new_password’;

For MySQL 5.7.5 and older as well as MariaDB 10.1.20 and older, use:

SET PASSWORD FOR ‘root’@’localhost’=PASSWORD(‘new_password’);

或者
UPDATE mysql.user SET authentication_string =PASSWORD(‘new_password’)WHERE User =’root’AND Host =’localhost’;

7、重启
For MySQL, use:
sudokillcat/var/run/mysqld/mysqld.pid

For MariaDB, use:
sudokill/var/run/mariadb/mariadb.pid

Then, restart the service using systemctl.

For MySQL, use:
sudo systemctl start mysql

For MariaDB, use:
sudo systemctl start mariadb

MACOS下升级MySQL数据库

1. 官网下载新版本的MySQL

2.在设置中关闭MySQL数据库

3.安装数据库,安装好之后打开利用Spotlight打开/usr/local文件夹,就会看到两个版本的mysql数据库,这两个文件中都有data,还有mysql指针这个文件夹

4.首先将新版本中的data文件夹重命名为dataold,

sudo mv 新版本路径/data  新版本路径/dataold

5.将老版本中的data文件夹复制到新版本中

sudo cp -rf  老版本路径/data   新版本路径

6.然后设置正确的权限

sudo chown -R  _mysql /usr/local/mysql-5.7.19-osx10.12-x86_64/data //后面跟的是新版本中data的路径

7.启动Mysql 修复数据库

sudo /usr/local/mysql/support-files/mysql.server start

8.运行升级程序

/usr/local/mysql/bin/mysql_upgrade -u username -p

//输入你原来数据库的用户名和密码 ,如果先前设置了环境变量直接输入mysql_upgrade 即可,别忘了用户名和密码

9.重启mysql数据库

sudo /usr/local/mysql/support-files/mysql.server restart

10.查看版本号

mysql -u username -p

mysql授权多个IP

以123.123.123.123 、123.123.123.124两个IP地址的用户访问db1为例,分别创建用户名user1、user2

1、为123.123.123.123上创建用户user1

create user ‘user1’@’123.123.123.123’ IDENTIFIED BY ‘XXXXXXX’;

grant select on db1.table1 to ‘user1’@’123.123.123.123’ ;

2、为123.123.123.124上创建用户user2

create user ‘user2’@’123.123.123.124’ IDENTIFIED BY ‘YYYYY’;

grant select on db1.table1 to ‘user2’@’123.123.123.124’ ;

说明:用户是按IP区分的,不同IP用户名可以相同

腾讯云图