使用Linux首次登录需要把mysql加入环境变量$PATH
[root@dl-001 ~]# export PATH=$PATH:/usr/local/mysql/bin/
#说明:此时可以使用命令 # mysql -uroot就可以登录mysql。但是需要添加到配置文件中重启才会永久生效。配置如下:
[root@dl-001 ~]# vim /etc/profile //添加至配置文件中
.........................
export PATH=$PATH:/usr/local/mysql/bin/
.........................
[root@dl-001 ~]# source /etc/profile //保存退出之后,要刷新才会生效
登录mysql
[root@dl-001 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
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> quit
Bye
说明: -u 指定的是用户,-p指定的是密码。首次登录密码默认为空,直接回车即可。
设置密码并登录
[root@dl-001 ~]# mysqladmin -uroot password 'mysqldl991124' // 设置密码
Warning: Using a password on the command line interface can be insecure.
[root@dl-001 ~]# mysql -uroot //设置密码完成之后,不指定密码是登录不成功的,需要-p指定密码。
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@dl-001 ~]# mysql -uroot -p'mysqldl991124' //指定密码即可登录。
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 11
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 -uroot -p 此时不输入密码直接回车,在下一行中也会提示输入密码。这两种方式都可以登录。
注意:的是mysql每执行一段命令,命令的结尾要加上;才会生效。
更改密码并登录
1,当知道原密码时,进行密码更改
[root@dl-001 ~]# mysqladmin -uroot -p'mysqldl991124' password '1234567'
Warning: Using a password on the command line interface can be insecure.
//说明:第一个''是原密码,第二个''是更改后的密码。需要输入正确的原密码才能更改成功。
[root@dl-001 ~]# mysql -uroot -p'1234567' //指定更改后的密码并登录
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 15
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> //成功登录
2,当忘记原密码时,进行密码更改
步骤:
1,编辑 vim /etc/my.cnf 添加配置文件 skip-grant ;
2,重启mysql # /etc/init.d/mysqld restart ;
3,此时不用输入密码登录到mysql # mysql -uroot ;
4,在mysql中修改密码并退出 mysql> update user set password=password(‘mysqldl991124’) where user=’root’; ;
5,在删除/etc/my.cnf中添加的配置文件;
6,重启mysql # /etc/init.d/mysqld restart;
7,此时就可以使用更改后的密码登录了!!!
具体操作如下:
[root@dl-001 ~]# vim /etc/my.cnf
[mysqld]
skip-grant //1,添加此行即可。意思是忽略授权。也就是说不用用户名密码了,只能就能登录mysql。
datadir=/data/mysql
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[root@dl-001 ~]# /etc/init.d/mysqld restart //2,重新启动
Shutting down MySQL.. SUCCESS!
Starting MySQL....... SUCCESS!
// 说明: 完成该操作之后就可以无需密码登录mysql了,所以此时mysql安全性很差,平时配置文件中一定不要添加该参数。
[root@dl-001 ~]# 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> //登录成功
mysql> use mysql; //切换mysql库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from user //该表中存放的是用户相关信息(密码、授权…)
-> ;
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-----------------------+------------------+
| Host | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_ci
...................................
mysql> select password from user where user='root'; //查看root用户的密码
+-------------------------------------------+
| password |
+-------------------------------------------+
| *6A7A490FB9DC8C33C2B025A91737077A7E9CC5E5 | //此密码是使用password生成的
| |
| |
| |
+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> update user set password=password('mysqldl991124') where user='root'; //更改密码为 mysqldl991124
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> exit
Bye
//此时密码已经更改完成,但是需要,恢复配置文件重启并登录。操作如下
[root@dl-001 ~]# vim /etc/my.cnf
[mysqld]
skip-grant //删除此行
datadir=/data/mysql
socket=/tmp/mysql.sock
//wq保存退出
[root@dl-001 ~]# /etc/init.d/mysqld restart //重新启动mysql
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@dl-001 ~]# mysql -uroot -p'mysqldl991124' //使用更改后的密码登录成功!
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 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>