先启动mysql:html
[root@nginx ~]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! [root@nginx ~]# ps aux |grep mysql root 1407 0.4 0.0 113308 1624 pts/0 S 20:27 0:00 /bin/sh /usr/local/mysql//bin/mysqld_safe --datadir=/data/mysql/ --pid-file=/data/mysql//nginx.pid mysql 1515 5.5 24.1 1300824 451192 pts/0 Sl 20:27 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=/data/mysql/nginx.err --pid-file=/data/mysql//nginx.pid root 1539 0.0 0.0 112720 972 pts/0 R+ 20:27 0:00 grep --color=auto mysql
登陆mysql:mysql
mysql -uroot //以用户root登陆 [root@nginx ~]# mysql -uroot -bash: mysql: 未找到命令
若是此时提示:mysql,无此命令,是由于没有把/usr/local/mysql/bin/mysql 加入PATH环境变量中nginx
能够临时加入:mysql就生效了sql
[root@nginx ~]# export PATH=$PATH:/usr/local/mysql/bin/ [root@nginx ~]# mysql 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>
要永久生效,就要在/etc/profile 下加入shell
[root@nginx ~]# vim /etc/profile ##省略内容 unset -f pathmunge export PATH=$PATH:/usr/local/mysql/bin/mysql [root@nginx ~]# source /etc/profile //使配置生效
或者用别名:数据库
alias mysql='/usr/local/mysql/bin/mysql'
指定用户和密码登陆:vim
[root@nginx ~]# mysql -uroot -p //回车,而后输入密码便可登陆,无密码直接按回车。 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 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>
若是root没有密码,能够设置密码:bash
[root@nginx ~]# mysqladmin -uroot password '7826078' //在单引号内设置密码,按回车 Warning: Using a password on the command line interface can be insecure. [root@nginx ~]# mysql -uroot -p7826078 //登陆验证 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 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>
修改密码:socket
[root@nginx ~]# mysqladmin -uroot -p'7826078' password '961303' //按回车修改生效 Warning: Using a password on the command line interface can be insecure. [root@nginx ~]# mysql -uroot -p961303 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 11 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>
若是密码忘记了,能够重置密码:tcp
[root@nginx ~]# vim /etc/my.cnf //在配置文件的[mysqld]下增长 # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] skip-grant //忽略受权 [root@nginx ~]# /etc/init.d/mysqld restart //重启一下mysql服务。 Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@nginx ~]# 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) 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函数生成的 +-------------------------------------------+ | password | +-------------------------------------------+ | *3169EA1E35BFDB495D332C42C34EBB9E797543DD | | | | | | | +-------------------------------------------+ 4 rows in set (0.00 sec) mysql> update user set password=password('7826078') where user='root'; //在user表里更新root用户的密码 Query OK, 4 rows affected (0.01 sec) Rows matched: 4 Changed: 4 Warnings: 0 [root@nginx ~]# vim /etc/my.cnf //在配置文件的[mysqld]下,删除这行 skip grant [root@nginx ~]# /etc/init.d/mysqld restart //重启一下mysql服务。而后就能够用新密码登陆了 Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@nginx ~]# mysql -uroot -p7826078 //用新密码登陆成功 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 3 Server version: 5.6.36 MySQL Community Server (GPL)
链接本机的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"
查询库:
show databases;
切换到指定库:
use mysql; //mysql是指定的库名
查看表:
show tables;
查看表的字段:
desc 表名;
查看表是怎么建立的:
show creat table 表名\G; //G 纵向显示
查看当前登陆用户:
select user();
查看当前使用的库:
select database();
建立库:
creat databases db1; // db1为库名
建立表:
creat table 表名('字段1' 类型,'字段2' 类型); craet table t1('id' int(4),'name' char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
查看数据库版本:
select version();
查看状态:
show status;
查看各参数:
show variables;
查看具体参数:
show variables like 'max_connect%'; //%是通配符
修改参数:
set global max_connect_errors=1000; //在内存中生效,要永久生效,该my.cnf文件
查看队列:
show processlist; //查看进程信息,mysql在作什么操做
查看完整队列信息:
show full processlist; //完整的进程信息,什么用户链接、执行什么操做、有无锁表等。