先啓動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; //完整的進程信息,什麼用戶鏈接、執行什麼操做、有無鎖表等。