1、设置更改root密码
root用户是mysql的超级管理员用户,默认mysql的 root 用户密码是空的,直接就可以连接上去,不需要输入密码,但是不安全,所以就需要设置一个密码
为了方便使用mysql服务,将mysql目录加入到环境变量里
查看mysql是否启动 ps aux|grep mysql;若是没有启动mysql的话,将mysql启动起来 service mysqld star
[root@localhost ~]# ps aux|grep mysql
root 1100 0.0 0.1 115432 1716 ? S 20:03 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.localdomain.pid
mysql 1215 7.8 44.8 981304 452364 ? Sl 20:03 0:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/localhost.localdomain.err --pid-file=/data/mysql/localhost.localdomain.pid
root 1391 0.0 0.0 112724 984 pts/0 S+ 20:04 0:00 grep --color=auto mysql
- 登录mysql:使用mysql -uroot命令,但是mysql命令会提示不存在,因为安装的mysql是在/usr/local/mysql/bin/mysql,而这个目录并不在环境变量PATH里面,所以它会报错 ,若想要这个命令直接运行,需要把PATH做一个更改;临时加入;mysql就生效了
[root@localhost ~]# mysql -uroot
-bash: mysql: 鏈壘鍒板懡浠?
[root@localhost ~]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@localhost ~]# 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> quit
Bye
- 若想要变量永久生效,还需要将export PATH=$PATH:/usr/local/mysql/bin/ 放入到 /etc/profile 最后面
- 假设若是没有运行 export PATH=$PATH:/usr/local/mysql/bin/ 命令,那也不能运行mysql,因为变量还没有生效,想要这个变量生效,在配置文件中加入命令后,还需要执行source /etc/profile 命令,重新加载
- 一般是使用mysql -uroot -p命令 -p,表示指定密码
- 密码为空的时候,直接回车就可进入到mysql,并可以在其中操作一些mysql的一些行为
- 退出mysql,输入 quit 即可
- 设置mysql密码,命令为mysqladmin -uroot passwd '123456' 在 ' ' 为密码
[root@localhost ~]# mysqladmin -uroot password '123456'
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p //登录验证,输入密码后进入
Enter password:
- 修改密码:知道mysql密码,去修改mysql密码
[root@localhost ~]# mysqladmin -uroot -p'123456' password '654321'
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
在明文指定密码的时候,密码可以加单引号,也可以不加单引号,建议加上单引号,防止密码有特殊符号的存在——>(若是不加单引号,而密码中又有特殊符号,就有可能会不识别)
- 不知道mysql的root密码,去更改密码
- 先去更改 /etc/my.cnf 下配置文件中加入skip-grant,(skip-grant ,表示忽略授权,也就是说操作mysql的时候不需要用户名和密码了,能直接登录)
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
skip-grant //忽略授权
# innodb_buffer_pool_size = 128M
[root@localhost ~]# /etc/init.d/mysqld restart //重启一下mysql服务。
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost ~]# mysql -uroot //然后再登录就不用密码了
mysql> use mysql //切换到mysql库,修改mysql库下的user表
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 password from user where user='root'; //查询root用户的密码字段,密码是加密显示的,是password函数生成的
mysql> update user set password=password('123456') where user='root'; //在user表里更新root用户的密码
Query OK, 4 rows affected (0.03 sec)
Rows matched: 4 Changed: 4 Warnings: 0
- 再去 /etc/my.cnf 配置文件中删除免授权配置,即删除skip-grant;若是不删除,那么之后所有的用户都不需要输入密码,就可以登录进去,这样安全性太低
- 重启mysql服务 service mysqld restart
- 重启完之后,再用新的密码测试下,可以登陆就表示密码更改成功
[root@localhost ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.......... SUCCESS! [root@localhost ~]# mysql -uroot -p Enter password:
2、连接mysql
- 连接本机的mysql: tcpip协议通信
mysql -uroot -p密码 //直接输入用户名和密码,默认就是连接本机的mysql
连接远程的mysql: tcpip协议通信
mysql -uroot -p密码 -h127.0.0.1 -P3306 //-h指定远程ip,-P指定远程端口
- 如果本机安装了多个mysql,可以指定socket连接mysql: socket协议通信
mysql -uroot -p密码 -S/tmp/mysql.sock //也是连接本机的mysql
- 在shell中常用的,连接mysql后,执行一些命令: 列出所有数据库
mysql -uroot -p密码 -e "show databases"
[root@localhost ~]# 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 |
+--------------------+
3、mysql常用命令
- 查询库 show databases;
- 切换库 use mysql;
- 查看库里的表 show tables;
- 查看表里的字段 desc tb_name;
- 查看建表语句 show create table tb_name\G;
- 查看当前用户 select user();
- 查看当前使用的数据库 select database();
- 创建库 create database db1;
mysql> create database db1;
Query OK, 1 row affected (0.06 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.04 sec)
- 创建表 use db1; create table t1(id int(4), name char(40));
mysql> use db1;
Database changed
mysql> create table t1(id int(4), name char(40));
Query OK, 0 rows affected (1.19 sec)
mysql> show create table t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.35 sec)
- 查看当前数据库版本 select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35 |
+-----------+
1 row in set (0.04 sec)
- 查看数据库状态 show status;
mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name | Value |
+-----------------------------------------------+-------------+
| Aborted_clients | 0 |
| Aborted_connects | 1 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Binlog_stmt_cache_disk_use | 0 |
| Binlog_stmt_cache_use | 0 |
- 查看各参数 show variables; show variables like 'max_connect%'; //%是通配符
mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
- 修改参数 set global max_connect_errors=1000; //仅在内存中生效;若想重启依然生效,那需要修改配置文件/etc/my.cnf
mysql> set global max_connect_errors=1000;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 1000 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
- 查看队列 show processlist; show full processlist;
mysql> show processlist; //查看进程信息,mysql在做什么操作
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 8 | root | localhost | db1 | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
mysql> show full processlist; //完整的进程信息,什么用户连接、执行什么操作、有无锁表等
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 8 | root | localhost | db1 | Query | 0 | init | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)
4、mysql用户管理
- 创建用户并授权
- grant all on . to 'user1' identified by 'passwd';(grant 表示授权;all 表示所有权限, SELECT,UPDATE,INSERT;查看,创建,删除等等)
- 若是登录到mysql中后,输错了字符,并按了回车键,直接输入分号 ; 就会推出, 回到mysql的命令行
- 退出mysql除了使用 quit 命令,还可以使用 exit 命令,还可以ctrl+d快捷键退出
登录到mysql ,创建普通用户user1
- grant all on . to 'user1'@'127.0.0.1' identified by '123456a'; //在输入命令的时候,千万要注意符号,一旦漏失了符号 ' ',那么后面就无法登录到user1的mysql
- 'user1'@'127.0.0.1' 指定用户@指定来源IP (指定用户可以写 % 就是通配,表示所有的IP)如果指定了来源IP,那么只能通过来源IP登录
- 符号*.* 表示所有库,所有表
- 第一个 * 表示库名,可以写成mysql.* 那就表示对mysql所有的表
- identified by 'passwd' 指定user1的mysql密码
- grant语句,是不会记录到命令历史中的因为不安全
mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456a';
Query OK, 0 rows affected (0.06 sec)
mysql> quit
退出数据库,并测试user1是否可以登录
[root@localhost ~]# mysql -uuser1 -p123456a
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES)
会看到登录失败,因为它默认的是sock,需要指定 -h 指定IP,会看到成功登录到user1的数据库
[root@localhost ~]# mysql -uuser1 -p'123456a' -h127.0.0.1
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
授权localhost,授权本地,用sock去连接(用root用户登陆)
mysql> grant all on *.* to 'user1'@'localhost' identified by '123456a';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@localhost ~]# mysql -uuser1 -p123456a
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
这时不加-h 也可以登录到user1了,因为现在授权就是针对localhost,localhost就是针对的sock
show grants for [email protected]; 指定用户去查看授权(需在root用户下查看)
mysql> show grants for user1@'127.0.0.1';
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*B012E8731FF1DF44F3D8B26837708985278C3CED' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec
5、常用sql语句
- 统计指定表的数据行数::select count(*) from mysql.user;
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
| 8 |
+----------+
1 row in set (0.03 sec)
- 查看db表的所有数据内容:少用*,耗资源:select * from mysql.db;
mysql> select * from mysql.db\G // 纵向显示
*************************** 1. row ***************************
Host: %
Db: test
User:
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Grant_priv: N
References_priv: Y
- 查看db表的db字段:select db from mysql.db;
mysql> select db from mysql.db;
+---------+
| db |
+---------+
| test |
| test\_% |
+---------+
2 rows in set (0.00 sec)
- 查看多个字段:select db,user from mysql.db;
mysql> select db,user from mysql.db;
+---------+------+
| db | user |
+---------+------+
| test | |
| test\_% | |
+---------+------+
2 rows in set (0.00 sec)
like模糊匹配:查找host字段为192.168.*.*的所有行数据:select * from mysql.db where host like '192.168.%'; // %匹配
插入表t1一行数据:insert into db1.t1 values (1, 'abc');
mysql> insert into db1.t1 values (1,'abc');
Query OK, 1 row affected (0.05 sec)
mysql> select count(*) from db1.t1;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.03 sec)
- 修改字段值 id=1对应的name字段:update db1.t1 set name='aaa' where id=1;
- 清空一个表的数据,表结构还在:truncate table db1.t1;
- 丢弃表:数据与表结构都删除:drop table db1.t1;
- 丢弃一个库:drop database db1; //整个数据库都删除,包括结构与数据
总结
- 在使用mysql的时候,少用 * 这样的操作,因为若是一个表里面的内容很多,select count(*)这样操作就会很耗时,浪费资源
- 数据库中常用引擎是myisam和innodb,默认mysql库里面都是使用的myisam引擎
- 特点:myisam引擎,能自动去统计有多少行
- 在select count(*)查看表的时候会很快
- use mysql;
- show create table user\G;
- 特点:innodb引擎,不会自动统计行数,每次去查询,每次去统计行数,就会很耗时
- use db1
- show create table t1;
- 所以select count(*)这种操作尽量减少,会耗费太多资源 6、mysql数据库备份恢复
- 备份库 mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
[root@localhost ~]# mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
- 恢复库 mysql -uroot -p123456 mysql < /tmp/mysql.sql
- 恢复是,必须保证目录一致
[root@localhost ~]# mysql -uroot -p123456 mysql < /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
- 备份表 mysqldump -uroot -p123456 mysql user > /tmp/user.sql
- 恢复表 mysql -uroot -p123456 mysql < /tmp/user.sql
[root@localhost ~]# mysqldump -uroot -p123456 mysql user > /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p123456 mysql user > /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
- 备份所有库 mysqldump -uroot -p123456 -A >/tmp/123.sql //-A 表示all所有的意思
- 只备份表结构 mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql
[root@localhost ~]# mysqldump -uroot -p123456 -A >/tmp/123.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.