设置mysql用户密码(5.6/5.7)、远程连接数据库、常用命令

第13章 MySQL常用操作

MySQL版本 5.6.35

13.1 设置、更改root用户密码

首次直接使用mysql会提示‘该命令不存在’,原因是还没有将该命令加入环境变量,如果要使用该命令,需要使用其绝对路径:/usr/local/mysql/bin/mysql,为了方便,先将其加入系统环境变量:

[[email protected] ~]# exprt PATH=$PATH:/usr/local/mysql/bin/
至此,mysql命令路径暂时加入环境变量,系统重启后该变量会失效,若要永久生效,需要将其加入环境变量配置文件:

[[email protected] ~]# vim /etc/profile
……
export PATH=$PATH:/usr/local/mysql/bin/

刷新配置文件(否则不生效):
[[email protected] ~]# source /etc/profile
设置 & 更改密码

首次登陆mysql,root用户没有密码,直接登录:

[[email protected] ~]# mysql -uroot
#-u:=user,指定用户名
Welcome to the MySQL monitor. Commands end with ; or \g.
……
mysql> quit
#退出
说明: 登录mysql之后可以进行与mysql相关的一些操作,但是设置mysql用户的密码需要执行以下操作!

设置密码

[[email protected] ~]# mysqladmin -uroot password ‘123456‘

再次登录:
[[email protected] ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
说明: 设置密码后直接登录会报错(ERROR),需要输入密码登录。

[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.
mysql>
注: -p=passwd,使用密码登录,在此可以将密码直接输入在命令行(跟在-p后面,不加空格:-p‘123456‘<此处单引号可以不加,但是当密码中有特殊符号时必须加,所以在命令行输入密码时养成习惯:加单引号>),也可以不在命令行输入,只跟-p选项,然后根据提示信息:“Enter password”,输入密码进行登录(此方法不会暴露用户密码,安全)。

更改密码

当知道用户密码时,进行密码更改:
[[email protected] ~]# mysqladmin -uroot -p‘123456‘ password ‘1234567‘

[[email protected] ~]# mysql -uroot -p‘1234567‘
Welcome to the MySQL monitor.
mysql>
更改成功!

忘记密码时,进行密码更改:
先编辑mysql配置文件:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
skip-grant
#忽略授权!
datadir=/data/mysql
socket=/tmp/mysql.sock

重启mysql服务:
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL..................... SUCCESS!
说明: 完成该操作之后就可以任意登录mysql了(无需密码),所以此时mysql安全性很差,平时配置文件中一定不要添加该参数!!!

[[email protected] ~]# mysql -uroot
Welcome to the MySQL monitor.
mysql> use mysql;
#切换mysql库
Database changed
mysql> select * from user\G;
#查看用户的表信息,该表中存放的是用户相关信息(密码、授权…)
#G选项的作用是使输出信息有序显示,不加该选项,显示内容会很乱
mysql> select password from user;
#查看用户密码,显示结果Wie加密字符串!
mysql> update user set password=password(‘123456‘) where user=‘root‘;
Query OK, 4 rows affected (0.11 sec)
Rows matched: 4 Changed: 4 Warnings: 0
#将密码更改为‘123456’
mysql> quit
Bye
密码更改成功!

恢复配置文件:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

重启mysql服务:
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL........... SUCCESS!

登录:
[[email protected] ~]# mysql -uroot -p‘123456‘
Welcome to the MySQL monitor.
mysql> quit
Bye
Finished!
步骤: vim /etc/my.cnf-->添加skip-grant-->mysql restart-->登录-->use mysql-->update user set password=...-->vim /etc/my.cnf-->删除skip-grant-->mysql restart。

13.2 连接mysql(本地、远程)

远程连接:使用IP/port连接

[[email protected] ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306
Welcome to the MySQL monitor.
mysql> quit
Bye
注: -h:=host,指定IP;-P:=port,指定端口。

本地连接:使用socket连接

[[email protected] ~]# mysql -uroot -p123456 -S/tmp/mysql.sock
Welcome to the MySQL monitor.
mysql> quit
Bye
注: -S:=socket,指定socket。此方法只适用于本地连接,等同于“mysql -uroot -p123456”。

显示所有数据库

[[email protected] ~]# mysql -uroot -p‘123456‘ -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
注: 该方法使用于shell脚本中。

13.3 MySQL常用命令

查看库信息:

mark

以下命令需要在切换库(use mysql)之后执行:

mark

编辑库:

mark

注: 以上命令均需要在mysql下执行;在mysql中每行命令末尾加上分号,表示该行命令执行结束。 tb_name即table name()表名。

示例:

[[email protected] mysql]# mysql -uroot -p‘123456‘
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 5
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)

mysql> use 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> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| time_zone |
| time_zone_leap_second |
+---------------------------+
28 rows in set (0.00 sec)

mysql> desc time_zone;
+------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+----------------+
| Time_zone_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Use_leap_seconds | enum(‘Y‘,‘N‘) | NO | | N | |
+------------------+------------------+------+-----+---------+----------------+
2 rows in set (0.11 sec)

mysql> show create table time_zone\G;
#G=grep筛选文字内容,规律显示出来
1. row
Table: time_zone
Create Table: CREATE TABLE time_zone (
Time_zone_id int(10) unsigned NOT NULL AUTO_INCREMENT,
Use_leap_seconds enum(‘Y‘,‘N‘) NOT NULL DEFAULT ‘N‘,
PRIMARY KEY (Time_zone_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT=‘Time zones‘
1 row in set (0.03 sec)

ERROR:
No query specified

mysql> select user();
+----------------+
| user() |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.07 sec)

mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)

mysql> select * from user\G;
创建库:
mysql> create database db1;
Query OK, 1 row affected (0.02 sec)

创建表:
mysql> use db1;
#先切换到指定库下
Database changed
mysql> create table t1(id int(4),name char(40));
#括号中是定义字段及字段格式,使用反引号引起来
Query OK, 0 rows affected (1.51 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35 |
+-----------+
1 row in set (0.06 sec)

mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name | Value |
+-----------------------------------------------+-------------+
| Aborted_clients | 0 |
| Aborted_connects | 0 |
+-----------------------------------------------+-------------+

mysql> show variables\G;

mysql> show variables like ‘max_connect%‘\G;
#like表示匹配;%是通配符

更改参数:
mysql> set global max_connect_errors=110;
Query OK, 0 rows affected (0.04 sec)
#在此只是临时更改,如果要永久更改,需要编辑配置文件

查看队列:
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 5 | root | localhost | db1 | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.01 sec)

mysql> drop table t1;
Query OK, 0 rows affected (0.32 sec)

mysql> drop database db1;
Query OK, 0 rows affected (0.10 sec)
扩展:MySQL5.7之更改root密码

与MySQL 5.6版本不同,在安装MySQL 5.7过程中(初始化)会自动生成root用户密码(随机),那么在安装完成后如何更改root用户密码?步骤如下:

查看默认密码

[[email protected] mysql]# cat /root/.mysql_secret

The random password set for the root userat Fri Jan 10 20:00:34 2014 (local time): 3A)2DdJLkcFP

更改root密码:已知默认密码

使用默认密码登录:
[[email protected] mysql]# /usr/local/mysql/bin/mysql -uroot -p‘3A)2DdJLkcFP‘
Welcome to the MySQL monitor.
Your MySQL connection id is 3
Server version: 5.7.17

设置新密码:
方法1:
mysql> set password = password(‘123456‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)
方法2:
mysql> SET PASSWORD FOR ‘root‘@localhost = PASSWORD(‘123456‘);
mysql> quit
Bye
Finished!

更改root密码:不知道默认密码

编辑配置文件:
[[email protected] mysql]# vi /etc/my.cnf

[mysqld]
skip-grant-tables
datadir=/data/mysql
socket=/tmp/mysql.sock
#增加参数:skip-grant-tables

重启:
[[email protected] mysql]# /etc/init.d/mysqld restart

登录:此时不需要密码
[[email protected] mysql]# /usr/local/mysql/bin/mysql -uroot

更改密码:
mysql> update user set authentication_string=password(‘12456‘) where user=‘root‘;
mysql>quit

[[email protected] mysql]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

重启:
[[email protected] mysql]# /etc/init.d/mysqld restart

原文地址:http://blog.51cto.com/13242922/2089628

时间: 2024-08-04 07:13:16

设置mysql用户密码(5.6/5.7)、远程连接数据库、常用命令的相关文章

Mysql用户密码设置修改和权限分配

我的mysql安装在c:\mysql 一.更改密码 第一种方式: 1.更改之前root没有密码的情况 c:\mysql\bin>mysqladmin -u root password "your password" 2.更改之前root有密码的情况,假如为123456 c:\mysql\bin>mysqladmin -u root -p123456 password "your password" 注意:更改的密码不能用单引号,可用双引号或不用引号 第二

实现MySQL 用户密码的设置步骤

以下的文章主要向大家描述的是实现MySQL用户密码的实际操作流程以及在其实际草组过程中值得我们大家注意的事项的详细说明,之前我对MySQL 用户密码的实际操作有很多的不解之处,看完此篇文章相信你会豁然开朗. Method 1: 在/usr/local/MySQL/bin/下: ./MySQLadmin -u root password 'new_password' 一般安装时用此方法设置. 2.root 用户修改其他用户的MySQL 用户密码: MySQL> set password for '

修改mysql用户密码

修改mysql用户密码 目录 mysqladmin命令 UPDATE user 语句 SET PASSWORD 语句 root密码丢失的情况(待验证) mysqladmin命令(回目录) 格式如下(其中,USER为用户名,PASSWORD为新密码): mysqladmin -u USER -p password PASSWORD 该命令之后会提示输入原密码,输入正确后即可修改.例如,设置root用户的密码为123456,则 mysqladmin -u root -p password 12345

如何设置mysql root密码

如何设置mysql root密码: 1.首次安装 root初始密码为空,注意就是没有密码,直接敲回车即可进入 D:\wamp\bin\mysql\mysql5.6.17\bin>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.17 MySQL Comm

【纯干货、小技巧】遗忘MySQL用户密码怎么办?

遗忘MySQL用户密码怎么办? 在信息化社会,充分有效地管理和利用各类信息资源,是进行科学研究和决策管理的前提条件.数据库技术是管理信息系统.办公自动化系统.决策支持系统等各类信息系统的核心部分,是进行科学研究和决策管理的重要技术手段. 数据库,就是本身可视为电子化的文件柜--存储电子文件的处所,用户可以对文件中的数据进行新增.截取.更新.删除等操作. 数据库指的是以一定方式储存在一起.能为多个用户共享.具有尽可能小的冗余度的特点.是与应用程序彼此独立的数据集合. 即然是数据库那么就会存在管理数

profile_oracle设置某用户密码永不过期

原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46888139 oracle设置某用户密码永不过期 为ETL_TEST用户设置密码永不过期. select  *  from  dba_users  where  username = 'ETL_TEST'; 查看dba_users字典,可知道ETL_TEST用户到2015年11月23日到期. select  username,

ubuntu 设置root用户密码并实现root用户登录

一:设置root用户密码 在ubuntu中root用户的密码是随机的,所以需要我们自己起设置root用户的密码 在终端命令行中执行 sudo passwd 这时候会提示你输入当前用户密码,输入成功之后,下面输入的就是root用户的密码 这时候root用户密码就设置成功了,使用su root,然后输入刚刚输入的root用户密码就可以转到root用户下了 二:实现root用户登录系统 虽然我们已经设置了root用户密码,并且可以使用su root转到root用户角色下,但是当我们重新启动ubuntu

Metasploit中数据库的密码查看以及使用pgadmin远程连接数据库

我们都知道,在msf下进行渗透测试工作的时候,可以将结果数据保存到数据库中,方便各个小组成员在渗透测试过程中的数据同步. 例如,Metasploit提供了db_nmap命令,它能够将Nmap扫描结果直接存入数据库中,此外还提供了db_import命令,支持多达20中扫描器扫描结果的导入. Metasploit支持多种数据库,如:MySQL.SQLite3.PostgreSQL,其中PostgreSQL被作为默认数据库使用. 要启用msfconsole的时候,首先需要启用postgresql和me

2018-03-28设置及修改mysql用户密码学习笔记

退出mysql方法 quit或者exit 设置及修改mysqlroot用户密码 安装mysql后,默认管理员root密码为空,这很不安全,需要设置一个密码,在安装mysql单实例后,有个初始优化的一些安全措施: 为root设置了密码 删除无用的mysql库内的用户账号. 删除默认存在的test数据库 除了以上方法,针对mysql数据库的用户处理,我们还有更严格的做法如下: 增加system并提升为超级管理员,即和root等价的用户,只是名字不同. mysql>grant all privileg