mysql -uroot -p grant all on *.* to 'user1'@'127.0.0.1' identified by '1234a'; //第一個*爲庫,第二個*爲表 quit mysql -uuser1 -p1234a -h127.0.0.1 [ctrl-d] mysql -uroot p grant all on *.* to 'user1' identified by '1234a'; exit mysql -uuser1 -p1234a -h試試看 還能夠: grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.83.1' identified by 'passwd'; grant all on db1.* to 'user3'@'%' identified by 'passwd'; show grants; 查看當前用戶權限 show grants for user1@127.0.0.1; show grants for user2@192.168.83.1; 兩行能夠用於遷移用戶IP,兩行改IP執行下,原密碼會保留
假設你想管理下用戶,只記得某個庫裏的某個表管理用戶的登陸,你須要這麼作:html
[root@ax-01 ~]# mysql -uroot -p Enter password: mysql> show databases; //查全部庫的名字 +--------------------+ | Database | +--------------------+ | information_schema | | cmsdb | | discuz | | mysql | //你看到mysql庫,想起來就這個庫裏,可是不知道該查那一個表 | performance_schema | | test | | wpdb | +--------------------+ mysql> use mysql; //選擇mysql庫 mysql> show tables; //查mysql裏面的全部表 | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | //你看到user表,想起來應該在這裏,可是不知道查那個字段 +---------------------------+ mysql> desc user; //查看全部user下的字段名(表頭) +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Host | char(60) | NO | PRI | | | | User | char(16) | NO | PRI | | | | Password | char(41) | NO | | | | 看到這裏你感受須要瞭解user,host,password這三個字段,由於登錄的時候要用到 mysql> select user,host,password from mysql.user; //查看這三個字段的內容 +------------+-----------+-------------------------------------------+ | user | host | password | +------------+-----------+-------------------------------------------+ | root | localhost | *8E1A3402D66F8DDD8D9D19596B706C6D238C0F34 | | root | ax-01 | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | ax-01 | | | wpuser | 127.0.0.1 | *E4D18EEE7AEB0071BDB1D931CD44AA9AE0D05293 | | discuzuser | 127.0.0.1 | *F47A72DB156C9E3F76410F41AC68CE966ACD6BDA | | cmsuser | 127.0.0.1 | *A9A4F564B109C6ECCF5EC6E2EB11DBD8925FEEDF | +------------+-----------+-------------------------------------------+ 看到這裏,你發任意用戶(空用戶名)不須要密碼就能夠登陸。顯然不行,須要刪掉。 刪東西前要養成備份的好習慣 [root@ax-01 ~]# mysqldump -uroot -paxianglinux mysql user > /tmp/user.sql mysql> delete from mysql.user where password=''; //刪掉 mysql> select user,host,password from mysql.user; //再看 +------------+-----------+-------------------------------------------+ | user | host | password | +------------+-----------+-------------------------------------------+ | root | localhost | *8E1A3402D66F8DDD8D9D19596B706C6D238C0F34 | | wpuser | 127.0.0.1 | *E4D18EEE7AEB0071BDB1D931CD44AA9AE0D05293 | | discuzuser | 127.0.0.1 | *F47A72DB156C9E3F76410F41AC68CE966ACD6BDA | | cmsuser | 127.0.0.1 | *A9A4F564B109C6ECCF5EC6E2EB11DBD8925FEEDF | +------------+-----------+-------------------------------------------+ mysql> flush privileges; mysql> quit;
開發,DBA須要深刻掌握,可是做爲系統管理員,基本的得會!mysql
select 查linux
select count(*) from mysql.user; 查mysql庫的user表有多少行 select * from mysql.db; 查mysql.db全部內容 select db from mysql.db; 查mysql.db的db字段 select db,user from mysql.db; 查看mysql.db的多個字段 select * from mysql.db where host like '192.168.%'; 模糊查詢
insert 插入sql
insert into db1.t1 values (1, 'abc'); 插入數據 desc db1.t1查看錶字段結構,明確插入字符格式
update 修改數據庫
update db1.t1 set name='aaa' where id=1;
刪除,當心用centos
delete from db1.t1 where id=1 //刪除id表頭字段下,=1的行 truncate table db1.t1; //清空表內容,字段表頭保留 drop table db1.t1; //刪除表 drop database db1; //刪除庫
備份庫 mysqldump -uroot -p123456 mysql > /tmp/mysql.sql 恢復庫 mysql -uroot -p123456 mysql < /tmp/mysql.sql 備份表 mysqldump -uroot -p123456 mysql user > /tmp/user.sql 恢復表 mysql -uroot -p123456 mysql < /tmp/user.sql 備份全部庫 mysqldump -uroot -p -A >/tmp/123.sql 只備份表結構 mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql