Linux11.1 设置更改Mysql的root密码及连接mysql

MySQL的root密码相关操作

  默认MySQL的root用户密码为空,可以直接登录

[root@chy002 ~]# /usr/local/mysql/bin/mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql>

  更改环境变量PATH,增加mysql绝对路径

[root@chy002 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@chy002 ~]# PATH=$PATH:/usr/local/mysql/bin/
[root@chy002 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> 

  设置MySQL的root用户密码

[root@chy002 ~]# mysqladmin -uroot password 'chyuanliu'
Warning: Using a password on the command line interface can be insecure.
[root@chy002 ~]# mysql -uroot -pchyuanliu
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 6
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql>

  更改密码

[root@chy002 ~]# mysqladmin -uroot -pchyuanliu password 'chyuanliu.1'
Warning: Using a password on the command line interface can be insecure.

  mysql的root密码重置

[root@chy002 ~]# vi /etc/my.cnf
#增加一行配置
skip-grant
[root@chy002 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@chy002 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> usemysql;
mysql> update user set password=password('liuchengyuan') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> Ctrl-C -- exit!
Aborted
[root@chy002 ~]# mysql -uroot -pliuchengyuan
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql>

#再修改配置文件,去掉skip-grant,重启mysql

连接mysql

  连接本机的mysql

mysql -uroot -p123456  ==  mysql -uroot -p123456 -S/tmp/mysql.sock

  连接远程的mysql

mysql -uroot -p123456 -h远程IP -P3306

  连接mysql后操作命令,多用于脚本

[root@chy002 ~]# mysql -uroot -pliuchengyuan -e "show databases"

  

原文地址:https://www.cnblogs.com/chyuanliu/p/9364147.html