13.1 设置更改root密码
默认MySQL密码为空
[[email protected] ~]# mysql -uroot -bash: mysql: 未找到命令 [[email protected] ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [[email protected] ~]# export PATH=$PATH:/usr/local/mysql/bin/ [[email protected] ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin [[email protected] ~]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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加入/etc/profile)
如果只是更改了profile文件,并没有执行export PATH,那么source /etc/profile可使环境变量生效
·设置mysql密码:
[[email protected] ~]# mysqladmin -uroot password '123456' Warning: Using a password on the command line interface can be insecure.
(最好命令行里不要包含密码)
·登录mysql:
[[email protected] ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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
·如果不知道mysql的密码,进行密码重置:
1,编辑 /etc/my.cnf ,增加skip-grant:
[[email protected] ~]# vi /etc/my.cnf
[[email protected] ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS!
2,进入mysql,use mysql,update user set password=password('密码') where user='root';
[[email protected] ~]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> 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> update user set password=password('123456') where user='root'; Query OK, 3 rows affected (0.00 sec) Rows matched: 4 Changed: 3 Warnings: 0 mysql> quit Bye
3,编辑 /etc/my.cnf ,去掉skip-grant:
[[email protected] ~]# vi /etc/my.cnf
(去掉之前增加的skip-grant)
4,重启mysql:
[[email protected] ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [email protected] ~]# mysql -uroot -p ##可以重新登录mysql了 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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
13.2 连接mysql
·连接本机MySQL:
[[email protected] ~]# mysql -uroot -p123456
·连接其他机器MySQL:
[[email protected] ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306
(-h 指定ip,-P 指定端口)
·使用socket连接MySQL:
[[email protected] ~]# mysql -uroot -p123456 -S/tmp/mysql.sock
(-S 指定socket位置,只适合在本机)
·连接MySQL后进行一些操作:
[[email protected] ~]# mysql -uroot -p123456 -e "show databases"
13.3 mysql常用命令
·查询库 show database;
[[email protected] ~]# mysql -uroot -p123456 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 1 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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;
·切换库 use mysql;
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
·查看库里的表 show tables;
mysql> show tables;
·查看表里的字段 desc tb_name;
mysql> desc user;
·查看建表语句 show create table tb_name\G;
·查看当前用户 select user();
·查看当前使用的数据库 select databsase();
·创建库 create database db1;
·创建表 use db1; create table t1(`id` int(4), `name` char(40));
如果想要定义其中的设置,可以
mysql> create table b1(`id`int(4),`name`char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.01 sec)
·查看当前数据库版本 select version();
·查看数据库状态 show status;
(数据不全部展示)
·查看各参数 show variables; show variables like 'max_connect%';
show variables;
(参数太多)
·修改参数 set global max_connect_errors=1000;
如果想要永久生效,需要退出到shell,vim /etc/my.cnf,定义max_connect_errors=1000;
·查看队列 show processlist; show full processlist;
如有错误,欢迎指正,互相学习,共同进步!!!
原文地址:http://blog.51cto.com/11530642/2089578