MySQL版本 5.6.35mysql
首次直接使用mysql会提示‘该命令不存在’,缘由是尚未将该命令加入环境变量,若是要使用该命令,须要使用其绝对路径:/usr/local/mysql/bin/mysql,为了方便,先将其加入系统环境变量:sql
[root@1 ~]# exprt PATH=$PATH:/usr/local/mysql/bin/
至此,mysql命令路径暂时加入环境变量,系统重启后该变量会失效,若要永久生效,须要将其加入环境变量配置文件:shell
[root@1 ~]# vim /etc/profile …… export PATH=$PATH:/usr/local/mysql/bin/ 刷新配置文件(不然不生效): [root@1 ~]# source /etc/profile
首次登录mysql,root用户没有密码,直接登陆:数据库
[root@1 ~]# mysql -uroot #-u:=user,指定用户名 Welcome to the MySQL monitor. Commands end with ; or \g. …… mysql> quit #退出
说明: 登陆mysql以后能够进行与mysql相关的一些操做,可是设置mysql用户的密码须要执行如下操做!vim
[root@1 ~]# mysqladmin -uroot password '123456' 再次登陆: [root@1 ~]# mysql -uroot ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
说明: 设置密码后直接登陆会报错(ERROR),须要输入密码登陆。安全
[root@1 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. mysql>
注: -p=passwd,使用密码登陆,在此能够将密码直接输入在命令行(跟在-p后面,不加空格:-p'123456'<此处单引号能够不加,可是当密码中有特殊符号时必须加,因此在命令行输入密码时养成习惯:加单引号>),也能够不在命令行输入,只跟-p选项,而后根据提示信息:“Enter password”,输入密码进行登陆(此方法不会暴露用户密码,安全)。dom
[root@1 ~]# mysqladmin -uroot -p'123456' password '1234567' [root@1 ~]# mysql -uroot -p'1234567' Welcome to the MySQL monitor. mysql>
更改为功!socket
先编辑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!
说明: 完成该操做以后就能够任意登陆mysql了(无需密码),因此此时mysql安全性不好,平时配置文件中必定不要添加该参数!!!ui
[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
密码更改为功!this
恢复配置文件: [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!
步骤: vim /etc/my.cnf-->添加skip-grant-->mysql restart-->登陆-->use mysql-->update user set password=...-->vim /etc/my.cnf-->删除skip-grant-->mysql restart。
[root@1 ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306 Welcome to the MySQL monitor. mysql> quit Bye
注: -h:=host,指定IP;-P:=port,指定端口。
[root@1 ~]# mysql -uroot -p123456 -S/tmp/mysql.sock Welcome to the MySQL monitor. mysql> quit Bye
注: -S:=socket,指定socket。此方法只适用于本地链接,等同于“mysql -uroot -p123456”。
[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 | +--------------------+
注: 该方法使用于shell脚本中。
查看库信息:
•查询库 show databases;
• 切换库 use mysql;
• 查看库里的表 show tables;
• 查看表里的字段 desc tb_name;
• 查看建表语句 show create table tb_name\G;
• 查看当前用户 select user();
• 查看当前使用的数据库 select databsase();
注: 以上命令均须要在mysql下执行;在mysql中每行命令末尾加上分号,表示该行命令执行结束。 tb_name即table name()表名。
[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)
与MySQL 5.6版本不一样,在安装MySQL 5.7过程当中(初始化)会自动生成root用户密码(随机),那么在安装完成后如何更改root用户密码?步骤以下:
[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
使用默认密码登陆: [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!
编辑配置文件: [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!