第十三章 MySQL操作

來源:互聯網
上載者:User

標籤:linux

13.1 更改root密碼
13.1.1 更改密碼
預設是沒有密碼的。
/etc/init.d/mysql start 啟動mysql
ps aux |grep mysql 查看是否啟動了
mysql -uroot 報找不到指令錯誤
因為mysql命令不在環境變數中,可以使用如下方法:
ls /usr/local/mysql/bin/mysql
1)用絕對位址
/usr/local/mysql/bin/mysql -uroot
更改環境變數PATH,增加mysql絕對路徑
2)暫時加入環境變數
export PATH=$PATH:/usr/local/mysql/bin/
3)永久生效,修改profil檔案,,增加命令句。
vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin/
source /etc/profile //更新一下即可
mysqladmin -uroot password ‘123456‘ //設定密碼123456
提示了:你將密碼在當前命令列顯示出來了,不×××全
mysql -uroot -p123456 //使用密碼123456登陸
mysqladmin -uroot -p‘123456‘ password ‘567890‘ 更改密碼為567890

13.1.2 密碼重設
修改 vi /etc/my.cnf //增加一行skip-grant忽略授權

重啟mysql服務 /etc/init.d/mysqld restart
mysql -uroot 不要求輸入密碼即可登陸
登陸後切換庫 use mysql;
select * from user; 這個表存放了使用者/密碼/許可權等
select password from user; 選出密碼
使用命令更改root密碼為aminglinux
update user set password=password(‘aminglinux‘) where user=‘root‘;
修改設定檔將忽略授權取消
vi /etc/my.cnf //刪掉增加的行skip-grant忽略授權
/etc/init.d/mysqld restart 重啟mysql服務
mysql -uroot -paminglinux 使用新密碼登陸

13.2 串連Mysql
mysql -uroot -paminglinux //預設也是指定了socket串連
mysql -uroot -paminglinux -S/tmp/mysql.sock //指定socket串連
mysql -uroot -paminglinux -h127.0.0.1 -P3306 //指定IP連接埠串連,預設3306
mysql -uroot -paminglinux -e "show databases" //登陸把所有的資料庫列出來

13.3 Mysql常用命令
庫由表組成,表由欄位組成。
查詢庫 show databases;
切換庫 use mysql;
查看庫裡的表 show tables;
查看錶裡的欄位 desc user; user為表的名字
查看建表語句 show create table user\G user為表的名字; \G豎排顯示
加\G後,其實就不用加分號了。 它本身就是一個結束符號。?
加了分號後,mysql以為我們又敲了另外一個命令,而另外一個命令是空,就報了這個ERROR
查看目前使用者 select user(); //分別查看一下使用者名稱
mysql -uroot -paminglinux -h192.168.188.128 -P3306 //aming-01
mysql -uroot -paminglinux -h127.0.0.1 -P3306 //localhost
查看當前使用的資料庫 select database();

建立庫 create database db1; //db1為建立的庫
建立表 use db1; create table t1(id int(4), name char(40)); //t1為庫db1的表


查看當前資料庫版本 select version();
查看資料庫狀態 show status;
查看各參數 show variables; show variables like ‘max_connect%‘;
修改參數 set global max_connect_errors=1000; //臨時的,要永久的修改設定檔
查看隊列 show processlist; show full processlist;

13.4 Mysql 使用者管理
預設有一個root使用者。
//建立user1使用者,授權所有的許可權密碼是123456,限制在localhost登入
grant all on . to ‘user1‘ @‘localhost‘ identified by ‘123456‘;
grant完之後,還要執行? flush privileges;
mysql -uuser1 -p123456 -h127.0.0.1 -P3306 登陸一下
mysql -uuser1 -p123456 也可以,因為授權了localhost,localhost就是針對socket
針對具體許可權授權
grant SELECT,UPDATE,INSERT on db1. to ‘user2‘@‘192.168.188.1‘ identified by ‘123456‘;
針對所有ip授權
grant all on db1.
to ‘user3‘@‘%‘ identified by ‘passwd‘;
查看授權
show grants;
針對使用者查看授權 查看許可權後可以複製內容授權給另一個使用者或者地址
show grants for [email protected];

13.4 Mysql常用語句
select count() from mysql.user; //查看mysql的user表的函數
select
from mysql.db\G //查看mysql的所有內容 \G
select db from mysql.db; //查看錶的欄位
select db,user from mysql.db; //查看錶的兩個欄位
select from mysql.db where host like ‘192.168.%‘; //模糊查看
select
from db1.t1\G
insert into db1.t1 values (1, ‘abc‘); //插入欄位ID+name
update db1.t1 set name=‘aaa‘ where id=1; //更新欄位ID為1的內容
delete from db1.t1 where id=1; //刪除ID=1的表
truncate table db1.t1; //清空表內容,但是欄位還存在
drop table db1.t1; //刪除表
?drop database db1; //刪除庫

13.4 Mysql Database Backup與恢複
備份庫到檔案中? mysqldump -uroot -paminglinux mysql > /tmp/mysql.sql
mysql -uroot -paminglinux -e "create database mysql2" //建立一個庫mysql2
恢複庫 mysql -uroot -paminglinux mysql2 < /tmp/mysql.sql
備份表 mysqldump -uroot -paminglinux mysql user > /tmp/user.sql //備份庫user表
恢複表 mysql -uroot -paminglinux mysql < /tmp/user.sql
備份所有庫 mysqldump -uroot -p -A >/tmp/123.sql
只備份表結構 mysqldump -uroot -paminglinux -d mysql > /tmp/mysql.sql

第十三章 MySQL操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.