標籤:lamp linux mysql
1、登入mysql的幾種方式:
#mysql -uroot -p123456 使用socket串連登入;
查看socket檔案位置 ps -aux | grep mysqld
[[email protected] ~]# ps aux |grep mysqldroot 941 0.0 0.1 6268 1416 ? S 21:01 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.localdomain.pidmysql 1056 0.0 4.4 392208 45456 ? Sl 21:01 0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --log-error=/data/mysql/localhost.localdomain.err --pid-file=/data/mysql/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306
指定socket檔案位置的方法:/etc/my.cnf設定檔、編譯的時候
#mysql -uroot -p123456 -S /socket檔案的路徑 使用指定socket檔案登入;
# /usr/local/mysql/bin/mysql -uroot -p123456 -S /tmp/mysql.sock
mysql -uroot -p123456 -h127.0.0.1 -P3306 使用ip地址 3306連接埠串連,連接埠可以自訂;
# /usr/local/mysql/bin/mysql -uroot -p123456 -h127.0.0.1 -P3306
直接在shell裡面執行mysql語句,需要加-e 參數
# mysql -uroot -p123456 -e "use mysql;select host,user from user where user=‘root‘";
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6C/AC/wKiom1VPYr6gO7i9AAE_HVFikEg388.jpg" title="-e.jpg" alt="wKiom1VPYr6gO7i9AAE_HVFikEg388.jpg" />
查看在mysql執行的命令曆史 cat /root/.mysql_history
首先刪除檔案,然後將檔案軟串連指向黑洞,防止命令泄露。
ln -s /dev/null /root/.mysql_history
2、mysql的常用操作:
查看所有的庫 show databases;
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6C/AC/wKiom1VPY7Cia8T0AACoqsVyTSM768.jpg" title="12.jpg" alt="wKiom1VPY7Cia8T0AACoqsVyTSM768.jpg" />
查看某個庫的表 use db; show tables; db為database列出來的庫名;
mysql> use discuz;
mysql> show tables;
查看錶的欄位 desc tb; tb為tables列出來的表的名字;
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6C/A7/wKioL1VPZAGglzrBAAIBYXilGBU286.jpg" title="desc.jpg" alt="wKioL1VPZAGglzrBAAIBYXilGBU286.jpg" />
查看建表語句 show create table tb; 可以複製命令自己建立其他表;可以查看錶的預設引擎,以及預設字元集;
唯寫表名是在本資料庫下面,也可以庫.表查看其它庫的表;
# show create table pre_home_show;
# show create table mysql.user;
ENGINE=MyISAM DEFAULT CHARSET=utf8
查看當前是哪個使用者 select user();
查看當前所在的庫 select database();
建立庫 create database db1;
建立表 create table t1 (`id` int(4), `name` char(40));
查看資料庫版本 select version();
查看mysql狀態 show status;
查看mysql提供的儲存引擎 show engines;
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="border:1px solid rgb(221,221,221);background-position:50% 50%;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6C/AC/wKiom1VPYnDx9iGcAAS-q5_PNnM944.jpg" title="engines.jpg" alt="wKiom1VPYnDx9iGcAAS-q5_PNnM944.jpg" />
修改mysql參數:
查看所有的變數 show variables;
show variables like ‘max_connect%‘; %代表統配符,與 * 一樣,多個任一字元;
show variables like ‘‘; 引號裡為空白,也可以顯示所有的變數;引號內可以寫關鍵字加%,查看關鍵字的項目;
修改參數的值 set global max_connect_errors = 1000;
mysql> show variables like ‘max_connect%‘;
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 10 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.01 sec)
mysql> set global max_connect_errors=10000;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘max_connect%‘;
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 10000 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
查看當前預設的儲存引擎 show variables like ‘%storage_engine%‘;
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6C/A7/wKioL1VPY9DwmovWAADk2tGL_8E093.jpg" title="storage_engine.jpg" alt="wKioL1VPY9DwmovWAADk2tGL_8E093.jpg" />
/data/mysql/ 目錄下面存放的資料庫的內容,有資料庫名對應的目錄;
myisam儲存一個檔案有3種格式(.frm .MYD .MYI )
查看mysql隊列 show processlist; show full processlist;顯示全部的資訊。
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6C/A7/wKioL1VPZ2-jDWZoAAF--RJnKiY575.jpg" title="11.jpg" alt="wKioL1VPZ2-jDWZoAAF--RJnKiY575.jpg" />
建立普通使用者並授權
grant all on *.* to user1 identified by ‘123456‘;
grant all on db1.* to ‘user2‘@‘10.0.2.100‘ identified by ‘111222‘;
grant all on db1.* to ‘user3‘@‘%‘ identified by ‘231222‘;
更改密碼 UPDATE mysql.user SET password=PASSWORD("newpwd") WHERE user=‘username‘ ;
查詢顯示表的行數 select count(*) from mysql.user; myisam 顯示速度比較快;
查詢表的所有內容 select * from mysql.db;
條件查詢表的內容 select * from mysql.db where host like ‘10.0.%‘;
mysql> select * from mysql.db\G; 如顯示亂碼,需要加\G,列表顯示;
更新記錄 update db1.t1 set name=‘aaa‘ where id=1;
mysql> update huang.name set name=‘aaa‘ where id=1;
Query OK, 0 rows affected (0.01 sec)
Rows matched: 0 Changed: 0 Warnings: 0
刪除表的資料 delete form tb where id=1;
清空表 truncate table db1.t1;
刪除表 drop table db1.tb1;
刪除資料庫 drop database db1;
修複表 repair table tb1 [use frm]; [use frm]為索引檔案,索引重建一下;
3、mysql備份與恢複
使用mysqldump備份,適合資料量較小的;
備份 mysqldump -uroot -p123456 db > /tmp/1.sql 重新導向到一個檔案裡面;
恢複 mysql -uroot -p123456 db < 1.sql 反向重新導向,相當於複製了一個資料庫;db必須真實存在;
只備份一個表 mysqldump -uroot -p db tb > 2.sql
恢複一個表 mysql -uroot -p123456 db < 2.sql
mysqldump -uroot -p123456 -d db tb > 3.sql 只備份表的語句;
備份時指定字元集 mysqldump -uroot -p123456 --default-character-set=utf8 db >1.sql 指定字元集防止亂碼;
恢複也指定字元集 mysql -uroot -p123456 --default-character-set=utf8 db < 1.sql
本文出自 “模範生的學習部落格” 部落格,請務必保留此出處http://8802265.blog.51cto.com/8792265/1650084
mysql基本操作講解