52.更改root密码 链接mysql mysql命令

13.1 设置更改root密码html

13.2 链接mysqlmysql

13.3 mysql经常使用命令linux

扩展 web

mysql5.7 root密码更改 http://www.apelearn.com/bbs/thread-7289-1-1.htmlsql

myisam 和innodb引擎对比 http://www.pureweber.com/article/myisam-vs-innodb/shell

知乎上的答案 https://www.zhihu.com/question/20596402数据库

mysql 配置详解:https://www.jb51.net/article/48082.htmvim

mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html安全

同窗分享的亲身mysql调优经历: http://www.apelearn.com/bbs/thread-11281-1-1.htmlbash

 

 

 

 

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 |

+----+------+-----------+------+---------+------+-------+-----------------------+

相关文章
相关标签/搜索