Mac环境下MySQL第一次启动产生疑惑的解决

ERROR 1045:Access denied for user ‘root‘@’localhost’ (using password: NO) (Mysql 5.7.12)

1
2
3
4
5
6
7
8
9
# 在安全模式下启动 mysql
➜ ~ sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
➜ ~ mysql -u root

# 5.7.12 修改root密码
➜ ~ mysql > UPDATE mysql.user SET Password=PASSWORD('root') WHERE User='root';
# 如果上条语句无效请尝试:mysql> update mysql.user set authentication_string=password('1234') where user='root';

➜ ~ mysql > FLUSH PRIVILEGES;

重启MySQL,尝试登陆。

1
2
➜  ~ mysql -u root -p
Enter password:

Mysql数据库误删root用户

  • 向user表插入root相关数据
1
2
3
4
5
6
# 在安全模式下启动 mysql
➜ ~ sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
➜ ~ mysql -u root
mysql> use mysql;
mysql> insert into user set user='root',ssl_cipher='',x509_issuer='',x509_subject='';
mysql> update user set Host='localhost',select_priv='y', insert_priv='y',update_priv='y', Alter_priv='y',delete_priv='y',create_priv='y',drop_priv='y',reload_priv='y',shutdown_priv='y',Process_priv='y',file_priv='y',grant_priv='y',References_priv='y',index_priv='y',create_user_priv='y',show_db_priv='y',super_priv='y',create_tmp_table_priv='y',Lock_tables_priv='y',execute_priv='y',repl_slave_priv='y',repl_client_priv='y',create_view_priv='y',show_view_priv='y',create_routine_priv='y',alter_routine_priv='y',create_user_priv='y' where user='root';
  • 修改密码
1
2
3
4
5
mysql -u root
mysql> update mysql.user set password=password('root') where user='root';
update user set host = '%' where user = 'root'; #设置远程机器可访问mysql,如果不需要可不是设置
mysql> flush privileges;
mysql> quit;
  • 重新启动Mysql,登录验证。

MySQL启动、重启、停止指令

  1. 启动MySQL服务
    sudo /usr/local/MySQL/support-files/mysql.server start
  2. 停止MySQL服务
    sudo /usr/local/mysql/support-files/mysql.server stop
  3. 重启MySQL服务
    sudo /usr/local/mysql/support-files/mysql.server restart

参考链接