Set mysql user password, connect to database remotely, common commands

Chapter 13 MySQL Common Operations

MySQL version 5.6.35

13.1 Setting and changing the root user password

The first time you use mysql directly, you will be prompted that the command does not exist. The reason is that the command has not been added to the environment variable. If you want to use this command, you need to use its absolute path: /usr/local/mysql/bin/mysql. For convenience, First add it to the system environment variable:

[root@1 ~]# exprt PATH=$PATH:/usr/local/mysql/bin/

At this point, the mysql command path is temporarily added to the environment variable. After the system restarts, the variable will become invalid. To take effect permanently, it needs to be added to the environment variable configuration file:

[root@1 ~]# vim /etc/profile
……
export PATH=$PATH:/usr/local/mysql/bin/

刷新配置文件(否则不生效):
[root@1 ~]# source /etc/profile

Set & Change Password

When you log in to mysql for the first time, the root user does not have a password and logs in directly:

[root@1 ~]# mysql -uroot
#-u:=user,指定用户名
Welcome to the MySQL monitor.  Commands end with ; or \g.
……
mysql> quit
#退出

Description:  After logging in to mysql, you can perform some operations related to mysql, but to set the password of the mysql user, you need to perform the following operations!

set password

[root@1 ~]# mysqladmin -uroot password '123456'  

再次登录:
[root@1 ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Note:  After setting the password, it will report an error (ERROR) when logging in directly. You need to enter the password to log in.

[root@1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.
mysql>

Note:  -p=passwd, use the password to log in, where you can enter the password directly on the command line (followed by -p, no space is added: -p'123456'<The single quotation mark can be omitted here, but when the password is in the If there are special symbols, it must be added, so make a habit when entering the password on the command line: add single quotation marks >), or you can not enter it on the command line, just follow the -p option, and then enter the password according to the prompt message: "Enter password". Login (this method will not expose user password, safe).

change the password

  • When the user password is known, make a password change:
[root@1 ~]# mysqladmin -uroot -p'123456' password '1234567'

[root@1 ~]# mysql -uroot -p'1234567'
Welcome to the MySQL monitor.
mysql>

Change succeeded!

  • When you forget your password, make a password change:
先编辑mysql配置文件:
[root@1 ~]# vim /etc/my.cnf
[mysqld]
skip-grant
#忽略授权!
datadir=/data/mysql
socket=/tmp/mysql.sock

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

Note:  After completing this operation, you can log in to mysql arbitrarily (no password is required), so the security of mysql is very poor at this time, and this parameter must not be added to the configuration file at ordinary times! ! !

[root@1 ~]# 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

Password changed successfully!

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

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

登录:
[root@1 ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> quit
Bye

Finished!
Steps:  vim /etc/my.cnf-->add skip-grant-->mysql restart-->login-->use mysql-->update user set password=...-->vim /etc/ my.cnf-->delete skip-grant-->mysql restart.

13.2 Connect to mysql (local, remote)

Remote connection: connect using IP/port

[root@1 ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306
Welcome to the MySQL monitor.
mysql> quit
Bye

Note:  -h:=host, specify IP; -P:=port, specify port.

Local connection: use socket connection

[root@1 ~]# mysql -uroot -p123456 -S/tmp/mysql.sock
Welcome to the MySQL monitor.
mysql> quit
Bye

Note:  -S:=socket, specify socket. This method only works for local connections and is equivalent to "mysql -uroot -p123456".

show all databases

[root@1 ~]# 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               |
+--------------------+

Note:  This method is used in shell scripts.

13.3 MySQL Common Commands

View library information:

• Query database show databases;

• switch library use mysql;

• View the tables in the library show tables;

• View the field desc tb_name in the table;

• View the table creation statement show create table tb_name\G;

• View the current user select user();

• View the currently used database select databasebsase();

 

Note:  The above commands all need to be executed under mysql; adding a semicolon at the end of each line of command in mysql indicates that the execution of the line of command ends. tb_name is the table name () table name.

Example:

[root@1 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()         |
+----------------+
| root@localhost |
+----------------+
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)

Extension: Change root password of MySQL 5.7

Unlike the MySQL 5.6 version, the root user password (random) is automatically generated during the installation of MySQL 5.7 (initialization), so how to change the root user password after the installation is complete? Proceed as follows:

View default password

[root@1 mysql]# cat /root/.mysql_secret
# The random password set for the root userat Fri Jan 10 20:00:34 2014 (local time): 3A)2DdJLkcFP

Change root password: Known default password

使用默认密码登录:
[root@1 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!

Change root password: don't know default password

编辑配置文件:
[root@1 mysql]# vi /etc/my.cnf

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

重启:  
[root@1 mysql]# /etc/init.d/mysqld restart

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

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

[root@1 mysql]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

重启: 
[root@1 mysql]# /etc/init.d/mysqld restart

Finished!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685442&siteId=291194637