13.1 设置更改root密码
13.2 连接mysql
13.3 mysql常用命令
扩展
mysql5.7 root密码更改 http://www.apelearn.com/bbs/thread-7289-1-1.html
myisam 和innodb引擎对比 http://www.pureweber.com/article/myisam-vs-innodb/
知乎上的答案 https://www.zhihu.com/question/20596402
mysql 配置详解:https://www.jb51.net/article/48082.htm
mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html
同学分享的亲身mysql调优经历: http://www.apelearn.com/bbs/thread-11281-1-1.html
13.1 设置更改root密码:
这里的root用户是MySQL的超级管理用户,跟linux操作系统的root类似,但是要注意区分,并不是一个用户,当然也可以创建一个普通用户去连接MySQL。
默认MySQL的root用户密码是空,直接连上去不用输密码就OK。但是这样不安全所以要给他设置一个密码。但是呢设置完了密码,好长时间没用忘记了,怎么去重置,下面就是这一部分内容:
因为之前我们用的MySQL是在/usr/local下,不能直接用mysql这个命令,所以要改一下他的变量
!!(小插曲,阿鑫在做的时候,提示报错ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)。百度后,在/etc/my.cnf里加入了
[client]
socket = /Data/mysql/mysql/mysql.sock (启动脚本的配置文件)
[mysqld]
socket = /Data/mysql/mysql/mysql.sock
就可以了) mysql.sock的文件或许不能放在/tmp下?
~1./usr/local/mysql/bin/mysql -uroot
~2.更改环境变量PATH,增加mysql绝对路径
~3.mysqladmin -uroot password 'wangxin789'
~4.mysql -uroot -pwangxin789
~5.密码重置
知道root密码的情况下:mysqladmin -uroot -p 'wangxin789' password 'axin789'
不知道root密码的情况下:
~6.vi /etc/my.cnf //[mysqld]增加 skip-grant(忽略授权,可直接登录)
~7. /etc/init.d/mysqld restart //重启mysql服务
~8.mysql -uroot //直接登录
~9.use mysql; //切换库(使用mysql这个库来修改密码)
~10.update user set password=password('wangxin789') where user='root';
~11.//将/etc/my.cnf的skip-grant去掉,并重启
实例:
[root@axinlinux-01 ~]# export PATH=$PATH:/usr/local/mysql/bin/ #加上 export后,意味着这个PATH变量可以在当前shell以及子shell中生效了。要让这个变量永久生效还要把这个变量放在/etc/profile下
[root@axinlinux-01 ~]# vim /etc/profile #复制这条变量,放在最后面就可以了
[root@axinlinux-01 ~]# source /etc/profile #还要执行source。在当前bash环境下读取并执行FileName中的命令
[root@axinlinux-01 ~]# mysqladmin -uroot password 'wangxin789' #命令行设置密码
[root@axinlinux-01 ~]# mysql -uroot -p #命令行登录
Enter password: 输入密码即可
MySQL [(none)]> quit 输入quit退出
Bye
[root@axinlinux-01 ~]# mysqladmin -uroot -p'wangxin789' password 'axin789' #命令行修改密码
[root@axinlinux-01 ~]# mysql -uroot -p #再次登录
Enter password: #输入密码即可
[root@axinlinux-01 ~]# vim /etc/my.cnf #增加skip-grant
[root@axinlinux-01 ~]# /etc/init.d/mysqld restart #重启
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!
[root@axinlinux-01 ~]# mysql -uroot #直接进入即可
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> use mysql; #use mysql; 切换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 [mysql]> select * from user; #select * from user; 这条命令可以看一下user表(存放用户密码权限等)
以上输出为乱码(加密)
MySQL [mysql]> update user set password=password('wangxin789') where user='root';
#这两个password的意思是。一个是要设置的密码,另一个是password这个函数(我们修改密码就是用password这个函数来修改的)
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
MySQL [mysql]> quit
Bye
[root@axinlinux-01 ~]# vim /etc/my.cnf #将skip-grant去掉
[root@axinlinux-01 ~]# /etc/init.d/mysqld restart #并重启
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@axinlinux-01 ~]# mysql -uroot -p #用新的密码即可登录
Enter password:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13.2 连接mysql:
~1.mysql -uroot -p123456
连接本机。输入用户名和密码
~2.mysql -uroot -p123456 -h127.0.0.1 -P3306
连接远程。比如从A服务器连接B的MySQL。输入远程的IP和端口(大P默认端口3306,可以改或3307)
~3.mysql -uroot -p123456 -S/tmp/mysql.sock
MySQL也可以使用sock来连接(!!只适合在本机)。跟~1.连接差不多,虽然没有指定sock但是默认就是这个sock。阿鑫在做的时候sock不在/tmp下
~4.mysql -uroot -p123456 -e “show databases”
-e列出他的数据库。命令行多用于脚本之中
实例:
~2.
[root@axinlinux-01 ~]# mysql -uroot -pwangxin789 -h127.0.0.1 -P3306
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> quit
Bye
~3.
[root@axinlinux-01 ~]# ps aux |grep mysql 看一下sock路径
root 1165 0.0 0.0 115432 1704 ? S 06:39 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/axinlinux-01.pid
mysql 1377 0.5 24.3 1368532 457084 ? Sl 06:39 0:04 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=axinlinux-01.err --pid-file=/data/mysql/axinlinux-01.pid --socket=/data/mysql/mysql/mysql.sock
root 1847 0.0 0.0 112720 976 pts/0 R+ 06:52 0:00 grep --color=auto mysql
[root@axinlinux-01 ~]# mysql -uroot -pwangxin789 -S/data/mysql/mysql/mysql.sock
用大S(-S)来指定sock。后面跟ps后的路径即可
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
[root@axinlinux-01 ~]# mysql -uroot -pwangxin789 -e "show databases"
下面四个就是他的库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13.3 mysql常用命令:
~1.查询库 show databases; 均以;作为命令结尾
~2.切换库 use mysql;
~3.查看库里的表 show tables;
~4.查看表里的字段 desc tb_name;
例如desc user查看一user表里有哪些字段(一般交由开发去做)
~5.查看建表语句 show create table tb_name\G;
\G会显示的更加规整,加\G可不用加;作为命令结尾
~6.查看当前用户 select user();
~7.查看当前使用的数据库 select database();
注意此时这个database后面没有s
~8.创建库 create database db1;
~9.创建表 use db1; create table t1(`id` int(4),`name` char(40));
在db1库里,创建表叫t1。定义字段。第一个字段叫id,int(4)表示定义它的格式最长是4,第二个字段叫name,char(40)定义字符串最长是40。使用反引号
也可指定他的CHARSET=latin1默认是latin1.可设置成utf8
~10.查看当前数据库版本 select version();
~11.查看数据库状态 show status; 将来生产环境中会经常用到
~12.查看各参数 show variables; show variables like 'max_connect%';
%表示统配(只记得前面的后面记不清了,可以使用)
~13.修改参数 set global max_connect_errors=1000;
当然也可以在/etc/my.cnf配置文件里修改。也可以在命令行里面,在内存中生效
想要开机生效就要在my.cnf里的[mysqld]里加入max_connect_errors=1000
~14.查看队列 show processlist; show full processlist;
!!查看mysql到底在干什么。哪些用户在连他,连他的时候在执行什么操作,有没有锁表。会经常用到
类似于linux里用ps或top查看有哪些操作
full表示完整的。于前面的相比,full更加的完整
实例:
~1.
MySQL [(none)]> show databases; #以分号作为结束语
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
~2.
MySQL [(none)]> 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
~3
MySQL [mysql]> show tables; #查看表
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func
......
~4.
MySQL [mysql]> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
......
~5.
MySQL [mysql]> show create table user\G #更加详细,排列更加规矩。可不加;
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
......
~6.
MySQL [mysql]> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
~7.
MySQL [mysql]> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
~8.
MySQL [mysql]> create database db1;
Query OK, 1 row affected (0.01 sec)
MySQL [mysql]> show databases; #看一下库。就有了我们创建的这个db1
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1
~9.
MySQL [mysql]> use db1 先进入到db1这个新建测试库里
Database changed
MySQL [db1]> create table t1(`id` int(4),`name` char(40)); 新建这个表
Query OK, 0 rows affected (0.08 sec)
MySQL [db1]> 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.00 sec)
MySQL [db1]> drop table t1; #先删除我们之前创的表t1
Query OK, 0 rows affected (0.02 sec)
MySQL [db1]> create table t1(`id` int(4),`name` char(40)) CHARSET=utf8; #然后我们在重新创建t1的时候指定他的CHARSET=utf8
Query OK, 0 rows affected (0.04 sec)
MySQL [db1]> show create table t1\G #再次查看刚创的这个表。CHARSET就=utf8
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
~10.
MySQL [db1]> select version(); #查看版本
+-----------+
| version() |
+-----------+
| 5.6.39 |
+-----------+
1 row in set (0.00 sec)
~12.
MySQL [(none)]> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.01 sec)
~14
MySQL [(none)]> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 2 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
MySQL [(none)]> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 2 | root | localhost | NULL | Query | 0 | init | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+