设置MySQL数据库密码,连接数据库以及基本操作的常用命令

一、更改MySQL的root用户密码

1、首次进入数据库

[[email protected] ~]# /usr/local/mysql/bin/mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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

首次进入数据库使用了绝对路径,直接使用mysql命令是不行的,因为/usr/local/mysql/bin/不再PATH这个环境变量里。还有在首次进入数据库时,密码为。退出时,输入quit或者exit即可。

2、把mysql命令绝对路径加入环境变量

[[email protected] ~]# export PATH=$PATH:/usr/local/mysql/bin    //临时加入环境变量,重启就会失效
[[email protected] ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile      //追加到profile文件中,使环境变量永久生效
[[email protected] ~]# source /etc/profile     //重新加载配置
[[email protected] ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> 

3、设置和更改root密码

[[email protected] ~]# mysqladmin -uroot password ‘123456‘                  //在实际生产环境中请勿设置如此简单的密码
Warning: Using a password on the command line interface can be insecure.
[[email protected] ~]# mysqladmin -uroot password ‘123456‘
Warning: Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot     //设置好密码之后,再次使用之前的登录密令就报错了
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[[email protected] ~]# mysql -uroot -p123456     //输入密码后再登录,-p选项后面直接跟密码,不能有空格
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.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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
[[email protected] ~]# mysql -uroot -p     //-p后面不加密码,以交互的形式输入密码
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> exit
Bye
[[email protected] ~]# mysqladmin -uroot -p123456 password ‘zlinux‘   //修改密码
Warning: Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot -pzlinux     //使用新密码登录
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 9
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> 

4、忘记root密码时的操作

[[email protected] ~]# vim /etc/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]
skip-grant                                     //在[mysqld]下加入该字段

[[email protected] ~]# /etc/init.d/mysqld restart      //重启mysql
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[[email protected] ~]# mysql -uroot              //现在就无需密码了
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> 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> update user set password=password(‘zlinux123456‘) where user=‘root‘;            //更新密码
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> exit
Bye
[[email protected] ~]# vim /etc/my.cnf    //删除skip-grant字段
[[email protected] ~]# /etc/init.d/mysqld restart   //重启
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[[email protected] ~]# mysql -uroot    //无法登陆了
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[[email protected] ~]# mysql -uroot -pzlinux123456    //使用新密码登录
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 2
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> exit
Bye

二、连接数据库

[[email protected] ~]# mysql -uroot -p -h192.168.242.128 -P3306            //-P指定端口, -h指定ip进行登录
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> exit
Bye
[[email protected] ~]# mysql -uroot -p -S/tmp/mysql.sock    //使用sock登录,只适用于本地连接,等同于“mysql -uroot -p123456”
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> exit
Bye

三、MySQL基本操作的常用命令

查询当前所有库:show databases;
切换库: use mysql;
查看库里的表: show tables;
查看表所有字段: desc tb_name; //tb_name表示字段名
查看建表语句: show create table tb_name\G; //\G表示由竖排显示(示的更加有条理)
查看当前用户: select user();
查看当前使用的数据库: select databsase();
创建库: create database db1;
创建表: use db1; create table t1(id int(4), name char(40));
查看当前数据库版本: select version();
查看数据库状态: show status;
查看各参数 :show variables; show variables like ‘max_connect%‘;
修改参数: set global max_connect_errors=1000;
查看队列:show processlist; show full processlist;

简单演示:

[[email protected] ~]# mysql -uroot -p -S/tmp/mysql.sock
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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.01 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                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

mysql> desc func;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| name  | char(64)                     | NO   | PRI |         |       |
| ret   | tinyint(1)                   | NO   |     | 0       |       |
| dl    | char(128)                    | NO   |     |         |       |
| type  | enum(‘function‘,‘aggregate‘) | NO   |     | NULL    |       |
+-------+------------------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

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

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

mysql> 

原文地址:http://blog.51cto.com/3069201/2090005

时间: 2024-10-17 17:18:02

设置MySQL数据库密码,连接数据库以及基本操作的常用命令的相关文章

95.更改MySQL的root用户密码,MySQL基本操作的常用命令

更改MySQL的root用户密码 1.首次进入数据库 [[email protected] ~]# /usr/local/mysql/bin/mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, O

用phpMyAdmin修改mysql数据库密码

1初始数据库密码为空.用phpMyAdmin修改mysql数据库密码2第一步,点击phpMyAdmin里的用户选项.用phpMyAdmin修改mysql数据库密码3选择root localhost用户名,点击编辑权限.用phpMyAdmin修改mysql数据库密码4此时会出来修改权限的页面,里面可以设置的选项还是比较多的,暂时不管其他的,往下拖,会有一个修改密码的区域.用phpMyAdmin修改mysql数据库密码5输入两次新的密码,下面有个生成按钮,这是根据你当前设置的密码加密之后生成新的密码

如何设置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数据库密码步骤 1.修改mysql配置文件/etc/my.cnf  [mysqld] port        = 3306 socket      = /tmp/mysql.sock skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 256 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buff

破解本地MySQL数据库密码

破解本地MySQL数据库密码: 1.用系统管理员登陆系统. 2.停止MySQL的服务. Windows:运行net stop mysql关闭数据库 3.进入命令窗口,然后进入 MySQL的安装目录,比如我的安装目录是c:\mysql,进入C:\mysql\bin 4.跳过权限检查启动MySQL, c:\mysql\bin>mysqld-nt ––skip-grant-tables 或则:c:\mysql\bin>mysqld ––skip-grant-tables mysqld.exe是微软W

Mysql 数据库密码管理

1.密码为空时设置密码为 123456: mysqladmin -u root password '123456' 2.知道现有密码,需要修改密码为 123123: 2.1.第一种设置方法 mysql -u root -p123456 mysql> use mysql; mysql> update user set password=password('123123') where user="root"; mysql> flush  privileges; 2.2.

mysql数据库密码破解

7.1和7.1.1由于越狱不成熟,半完美越狱后电脑上无法访问系统越狱目录,如var usr 等等. 今天有些意外地发现,可以在电脑上使用手机的越狱目录我手机 i4 7.1.1 联通 半完美越狱,没装Afc2Add,也没装Appsync 附上  --->我的半完美越狱过程 好了,下面直接正题 一.前提,必须安装ifile! 打开ifile,并转到 /var/mobile/media 目录下,然后点击右上角的 [ 编辑 ]如图: 二.点左下角的 + 号创建,如图: 三.点 [ 类型],选择[符号链接

设置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命令路径暂时加入环境变量,系统重启后该变量会失效,若要永

wampserver修改mysql数据库密码后phpMyAdmin无法连接数据库

phpMyAdmin开始能打开,但是我在权限里把root的密码改了,再打开phpMyAdmin就显示:“phpMyAdmin 试图连接到 MySQL 服务器,但服务器拒绝连接.您应该检查 config.inc.php 中的主机.用户名和密码,并且确定这些信息与 MySQL 服务器的管理员所给出的信息一致.”,奇怪的是我把config.inc.php里的$cfg['Servers'][$i]['password']项里已经填上密码了呀,为什么还是不行呢?而且为什么phpMyAdmin没有登陆界面呢