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@localhost php-fpm.d]# ps aux |grep mysql
root 933 0.0 0.1 115436 1748 ? S 08:49 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/www.wangxin.com.pid
mysql 1083 0.0 46.0 1303012 459092 ? Sl 08:49 0:18 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=www.wangxin.com.err --pid-file=/data/mysql/www.wangxin.com.pid --socket=/tmp/mysql.sock
root 2307 0.0 0.2 133424 2792 pts/0 T 15:00 0:00 /usr/local/mysql/bin/mysql -uroot
root 2325 0.0 0.0 112724 992 pts/0 R+ 15:11 0:00 grep --color=auto mysql
更改环境变量PATH,增加mysql绝对路径
[root@localhost ~]# export PATH=$PATH:/usr/local/mysql/bin/ #加上 export后,意味着这个PATH变量可以在当前shell以及子shell中生效了。要让这个变量永久生效还要把这个变量放在/etc/profile下 提示没有目录
https://jingyan.baidu.com/article/63acb44af85a2861fcc17efb.html
永久生效,把mysql环境变量,添加到/etc/profile配置文件:
[root@localhost ~]# vim /etc/profile #复制这条变量,放在最后面就可以了
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
PATH=$PATH:/usr/local/mysql/bin/
-- 插入 --
[root@localhost ~]# source /etc/profile #还要执行source。在当前bash环境下读取并执行FileName中的命令
[root@localhost ~]# mysqladmin -uroot password 'wangxin789' #命令行设置密码
[root@localhost ~]# mysql -uroot -p #命令行登录
Enter password: 输入密码即可
MySQL [(none)]> quit 输入quit退出
Bye
[root@localhost ~]# mysqladmin -uroot -p'wangxin789' password 'axin789' #命令行修改密码
[root@localhost~]# mysql -uroot -p #再次登录
Enter password: #输入密码即可
[root@localhost ~]# vim /etc/my.cnf #增加skip-grant
[mysqld]
skip-grant
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /data/mysql
# port = .....
# server_id = .....
socket = /tmp/mysql.sock
[root@localhost~]# /etc/init.d/mysqld restart #重启
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!
[root@localhost~]# mysql -uroot #直接进入即可
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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 [(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, 3 rows affected (0.00 sec)
Rows matched: 4 Changed: 3 Warnings: 0
MySQL [mysql]> quit
Bye
[root@localhost~]# vim /etc/my.cnf #将skip-grant去掉
[root@localhost~]# /etc/init.d/mysqld restart #并重启
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@localhost ~]# 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 -p'123456' -S/tmp/mysql.sock
MySQL也可以使用sock来连接(!!只适合在本机)。跟~1.连接差不多,虽然没有指定sock但是默认就是这个sock。阿鑫在做的时候sock不在/tmp下
~4.mysql -uroot -p'123456' -e “show databases”
-e列出他的数据库。命令行多用于脚本之中
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
实例:
~2.
[root@localhost ~]# 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@localhost ~]# ps aux |grep mysql 看一下sock路径
root 2307 0.0 0.0 133424 676 pts/0 T 15:00 0:00 /usr/local/mysql/bin/mysql -uroot
root 5016 0.0 0.1 113308 1648 pts/0 S 16:58 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.pid
mysql 5167 0.0 45.4 1303008 453080 pts/0 Sl 16:58 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.err --pid-file=/data/mysql/localhost.pid --socket=/tmp/mysql.sock
root 5221 0.0 0.0 112724 988 pts/0 S+ 17:16 0:00 grep --color=auto mysql
[root@localhost ~]# mysql -uroot -pxiaoq36035 -S/tmp/mysql.sock
用大S(-S)来指定sock。后面跟ps后的路径即可
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.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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.
[root@localhost ~]# 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.01 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
Database changed
~3
MySQL [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)
~4.
MySQL [mysql]> desc user; 查看表里的字段
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| File_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Show_db_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Repl_slave_priv | enum('N','Y') | NO | | N | |
| Repl_client_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
| Create_tablespace_priv | enum('N','Y') | NO | | N | |
| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| plugin | char(64) | YES | | mysql_native_password | |
| authentication_string | text | YES | | NULL | |
| password_expired | enum('N','Y') | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
43 rows in set (0.01 sec)
~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 '',
`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '',
`ssl_cipher` blob NOT NULL,
`x509_issuer` blob NOT NULL,
`x509_subject` blob NOT NULL,
`max_questions` int(11) unsigned NOT NULL DEFAULT '0',
`max_updates` int(11) unsigned NOT NULL DEFAULT '0',
`max_connections` int(11) unsigned NOT NULL DEFAULT '0',
`max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
`plugin` char(64) COLLATE utf8_bin DEFAULT 'mysql_native_password',
`authentication_string` text COLLATE utf8_bin,
`password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
PRIMARY KEY (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges'
1 row in set (0.00 sec)
~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.00 sec)
MySQL [mysql]> show databases; #看一下库。就有了我们创建的这个db1
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
~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.09 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.01 sec)
MySQL [db1]> create table t1(`id` int(4),`name` char(40)) CHARSET=utf8; #然后我们在重新创建t1的时候指定他的CHARSET=utf8
Query OK, 0 rows affected (0.03 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.43 |
+-----------+
1 row in set (0.00 sec)
~12.
MySQL [(none)]> show variables like 'max_connect%'; 查看各参数 show variables; show variables like 'max_connect%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
~14
MySQL [(none)]> show processlist; 查看队列
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 11 | root | localhost | db1 | 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 |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 11 | root | localhost | db1 | Query | 0 | init | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

被折叠的 条评论
为什么被折叠?



