mysql維護管理的幾點小技巧(自我總結)

一、mysql數據庫用戶密碼修改方法

 

(1)、在知道mysql數據庫root用戶密碼條件下修改root用戶密碼wxsemico
方法一:
[root@centos6 ~]# mysql -u root -p
Enter password: 輸入root密碼
mysql> show databases;
mysql> use mysql;
mysql> update user set password=password('wxsemico') where user='root';
mysql> flush privileges;
mysql> exit

方法二:
[root@centos6 ~]# mysqladmin -u root -p2013@wxsemico password wxsemico 

/紅色原密碼,藍色新密碼
[root@centos6 ~]# mysql -u root -pwxsemico
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 386
Server version: 5.1.42-log Source distribution

Copyright (c) 2000, 2012, 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>

/使用新密碼wxsemico登錄成功

 

(3)、在忘記mysql數據庫root用戶密碼條件下修改root用戶密碼爲2013@wxsemico
[root@centos6 ~]# mysqladmin -u root -p shutdown
Enter password:
140523 18:50:33 mysqld_safe mysqld from pid file /usr/local/mysql/var/centos6.pid ended
[1]+  Done                    ./mysqld_safe --user=root --skip-grant-tables  (wd: /usr/local/mysql/bin)
(wd now: ~)
[root@centos6 ~]# ps -ef | grep mysql
root      2751  1971  0 18:50 pts/0    00:00:00 grep mysql
/先關閉掉mysql數據庫服務
/由於mysql爲源碼安裝方式先切換至mysql安裝目錄下的bin目錄
[root@centos6 ~]# cd /usr/local/mysql/bin/
[root@centos6 bin]# ll
total 16600
-rwxr-xr-x. 1 mysql mysql   10664 May 22  2013 innochecksum
-rwxr-xr-x. 1 mysql mysql    1424 May 22  2013 msql2mysql
-rwxr-xr-x. 1 mysql mysql 1794627 May 22  2013 myisamchk
-rwxr-xr-x. 1 mysql mysql 1667899 May 22  2013 myisam_ftdump
-rwxr-xr-x. 1 mysql mysql 1667299 May 22  2013 myisamlog
-rwxr-xr-x. 1 mysql mysql 1711644 May 22  2013 myisampack
-rwxr-xr-x. 1 mysql mysql 1398357 May 22  2013 my_print_defaults
-rwxr-xr-x. 1 mysql mysql  269500 May 22  2013 mysql
-rwxr-xr-x. 1 mysql mysql  110848 May 22  2013 mysqlaccess
-rwxr-xr-x. 1 mysql mysql   31997 May 22  2013 mysqladmin
-rwxr-xr-x. 1 mysql mysql  157601 May 22  2013 mysqlbinlog
-rwxr-xr-x. 1 mysql mysql   10675 May 22  2013 mysqlbug
-rwxr-xr-x. 1 mysql mysql   26276 May 22  2013 mysqlcheck
-rwxr-xr-x. 1 mysql mysql  415234 May 22  2013 mysql_client_test
-rwxr-xr-x. 1 mysql mysql    6137 May 22  2013 mysql_config
-rwxr-xr-x. 1 mysql mysql    4169 May 22  2013 mysql_convert_table_format
-rwxr-xr-x. 1 mysql mysql   23253 May 22  2013 mysqld_multi
-rwxr-xr-x. 1 mysql mysql   16690 May 22  2013 mysqld_safe
-rwxr-xr-x. 1 mysql mysql   91933 May 22  2013 mysqldump
-rwxr-xr-x. 1 mysql mysql    6602 May 22  2013 mysqldumpslow
-rwxr-xr-x. 1 mysql mysql    3245 May 22  2013 mysql_find_rows
-rwxr-xr-x. 1 mysql mysql     483 May 22  2013 mysql_fix_extensions
-rwxr-xr-x. 1 mysql mysql    5894 May 22  2013 mysql_fix_privilege_tables
-rwxr-xr-x. 1 mysql mysql   31485 May 22  2013 mysqlhotcopy
-rwxr-xr-x. 1 mysql mysql   26643 May 22  2013 mysqlimport
-rwxr-xr-x. 1 mysql mysql   14623 May 22  2013 mysql_install_db
-rwxr-xr-x. 1 mysql mysql    7694 May 22  2013 mysql_secure_installation
-rwxr-xr-x. 1 mysql mysql   16689 May 22  2013 mysql_setpermission
-rwxr-xr-x. 1 mysql mysql   24611 May 22  2013 mysqlshow
-rwxr-xr-x. 1 mysql mysql   46541 May 22  2013 mysqlslap
-rwxr-xr-x. 1 mysql mysql  192366 May 22  2013 mysqltest
-rwxr-xr-x. 1 mysql mysql 1368006 May 22  2013 mysql_tzinfo_to_sql
-rwxr-xr-x. 1 mysql mysql   65042 May 22  2013 mysql_upgrade
-rwxr-xr-x. 1 mysql mysql  159255 May 22  2013 mysql_waitpid
-rwxr-xr-x. 1 mysql mysql    3818 May 22  2013 mysql_zap
-rwxr-xr-x. 1 mysql mysql 1388574 May 22  2013 perror
-rwxr-xr-x. 1 mysql mysql 1380024 May 22  2013 replace
-rwxr-xr-x. 1 mysql mysql 1379698 May 22  2013 resolveip
-rwxr-xr-x. 1 mysql mysql 1381612 May 22  2013 resolve_stack_dump
[root@centos6 bin]# ./mysqld_safe --user=root --skip-grant-tables &   /忽略授權表
[1] 2778
[root@centos6 bin]# 140523 18:55:56 mysqld_safe Logging to '/usr/local/mysql/var/centos6.err'.
140523 18:55:57 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var

[root@centos6 bin]# cd   /切換至root家目錄
[root@centos6 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.42-log Source distribution

Copyright (c) 2000, 2012, 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>
/無需密碼即可登錄mysql數據庫,通過前面的方法重新設置root用戶新密碼即可!


[root@centos6 bin]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.42-log Source distribution

Copyright (c) 2000, 2012, 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> select version();
+------------+
| version()  |
+------------+
| 5.1.42-log |
+------------+
1 row in set (0.00 sec)
/查詢mysql數據庫版本

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

/查詢當前登錄用戶

mysql> select user from mysql.user;
+-------+
| user  |
+-------+
| cacti |
| root  |
| root  |
|       |
| root  |
|       |
| cacti |
| root  |
| user1 |
+-------+
9 rows in set (0.00 sec)

mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*D4CD521B67294775430C15B348B47051EBA4B810' WITH GRANT OPTION |
| GRANT ALL PRIVILEGES ON `cacti`.* TO 'root'@'localhost'                                                                                |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

/查看授權情況

mysql> grant select,insert,update,delete on *.* to zhang3@localhost identified by "zhang3";
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
/增加一個用戶zhang3密碼也爲zhang3,讓它可以從本機登錄並對所有數據庫有查詢、插入、修改、刪除權限
mysql> exit
[root@centos6 ~]# mysql -u zhang3 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 448
Server version: 5.1.42-log Source distribution

Copyright (c) 2000, 2012, 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> select user();
+------------------+
| user()           |
+------------------+
| zhang3@localhost |
+------------------+
1 row in set (0.00 sec)
/新建用戶zhang3登錄數據庫成功

 

二、如何封裝源碼安裝的mysql爲系統服務


[root@centos6 ~]# cd /usr/local/mysql/share/mysql/
[root@centos6 mysql]# ll
total 1268
-rw-r--r--. 1 mysql mysql   1153 May 22  2013 binary-configure
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 charsets
-rw-r--r--. 1 mysql mysql   3805 May 22  2013 config.huge.ini
-rw-r--r--. 1 mysql mysql   1661 May 22  2013 config.medium.ini
-rw-r--r--. 1 mysql mysql    905 May 22  2013 config.small.ini
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 czech
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 danish
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 dutch
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 english
-rw-r--r--. 1 mysql mysql 416006 May 22  2013 errmsg.txt
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 estonian
-rw-r--r--. 1 mysql mysql 591883 May 22  2013 fill_help_tables.sql
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 french
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 german
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 greek
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 hungarian
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 italian
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 japanese
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 korean
-rw-r--r--. 1 mysql mysql   5006 May 22  2013 mi_test_all
-rw-r--r--. 1 mysql mysql   1517 May 22  2013 mi_test_all.res
-rw-r--r--. 1 mysql mysql   4778 May 22  2013 my-huge.cnf
-rw-r--r--. 1 mysql mysql  20167 May 22  2013 my-innodb-heavy-4G.cnf
-rw-r--r--. 1 mysql mysql   4752 May 22  2013 my-large.cnf
-rw-r--r--. 1 mysql mysql   4763 May 22  2013 my-medium.cnf
-rw-r--r--. 1 mysql mysql   2401 May 22  2013 my-small.cnf
-rwxr-xr-x. 1 mysql mysql   1061 May 22  2013 mysqld_multi.server
-rw-r--r--. 1 mysql mysql  42249 May 22  2013 mysql_fix_privilege_tables.sql
-rw-r--r--. 1 mysql mysql    837 May 22  2013 mysql-log-rotate
-rwxr-xr-x. 1 mysql mysql  12302 May 22  2013 mysql.server
-rw-r--r--. 1 mysql mysql   1878 May 22  2013 mysql_system_tables_data.sql
-rw-r--r--. 1 mysql mysql  16527 May 22  2013 mysql_system_tables.sql
-rw-r--r--. 1 mysql mysql   9669 May 22  2013 mysql_test_data_timezone.sql
-rw-r--r--. 1 mysql mysql    589 May 22  2013 ndb-config-2-node.ini
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 norwegian
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 norwegian-ny
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 polish
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 portuguese
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 romanian
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 russian
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 serbian
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 slovak
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 spanish
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 swedish
drwxr-xr-x. 2 mysql mysql   4096 May 22  2013 ukrainian
[root@centos6 mysql]# cp mysql.server /etc/rc.d/init.d/mysql
[root@centos6 init.d]# chkconfig --list | grep mysql
[root@centos6 init.d]# chkconfig --add mysql
[root@centos6 init.d]# chkconfig --list | grep mysql
mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@centos6 init.d]# service mysql start
Starting MySQL....[  OK  ]
[root@centos6 init.d]# ps -ef | grep mysql
root      2184     1  0 23:34 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/var --pid-file=/usr/local/mysql/var/centos6.pid
mysql     2287  2184  0 23:34 pts/0    00:00:00 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql --log-

error=/usr/local/mysql/var/centos6.err --pid-file=/usr/local/mysql/var/centos6.pid --socket=/tmp/mysql.sock --port=3306
root      2329  1849  0 23:35 pts/0    00:00:00 grep mysql
[root@centos6 init.d]# service mysql stop
Shutting down MySQL.[  OK  ]
[root@centos6 init.d]# ps -ef | grep mysql
root      2356  1849  0 23:35 pts/0    00:00:00 grep mysql

 

三、數據庫的備份和恢復


[root@centos6 ~]# mysql -uroot -pwxsemico
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1218
Server version: 5.1.42-log Source distribution

Copyright (c) 2000, 2012, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cacti              |
| mysql              |
| test               |
| ***                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use ***;
Database changed
mysql> show tables;
+---------------+
| Tables_in_*** |
+---------------+
| ***user       |
+---------------+
1 row in set (0.00 sec)

mysql> desc ***user;
+----------+-----------+------+-----+---------+-------+
| Field    | Type      | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+-------+
| name     | char(20)  | NO   | PRI | NULL    |       |
| password | char(128) | YES  |     | NULL    |       |
| active   | int(10)   | NO   |     | 1       |       |
+----------+-----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from ***user;
+--------+-------------------------------------------+--------+
| name   | password                                  | active |
+--------+-------------------------------------------+--------+
| user1  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |
| user2  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |
| user3  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |
| zhang3 | zhang123                                  |      1 |
+--------+-------------------------------------------+--------+
4 rows in set (0.00 sec)

 

(1)、數據庫***備份
root@centos6 ~]# mkdir /backup
[root@centos6 ~]# mysqldump -uroot -pwxsemico *** > /backup/***_bk.sql
[root@centos6 ~]# mysqldump -uroot -pwxsemico *** | gzip > /backup/***_bk.sql 
[root@centos6 ~]# cd /backup
[root@centos6 backup]# ll
total 8
-rw-r--r-- 1 root root 776 May 24 03:05 ***_bk.sql
-rw-r--r-- 1 root root 776 May 24 03:08 ***_bk.sql.gz
/如若需要備份的數據庫容量較大,可採用gzip壓縮方式來減少容量

 

(2)、刪除數據庫***中***user表後恢復數據
mysql> drop table ***user;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from ***.***user;
ERROR 1146 (42S02): Table '***.***user' doesn't exist
mysql> exit
Bye
[root@centos6 ~]# mysql -u root -pwxsemico *** < /backup/***_bk.sql
[root@centos6 ~]# mysql -u root -pwxsemico
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1497
Server version: 5.1.42-log Source distribution

Copyright (c) 2000, 2012, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cacti              |
| mysql              |
| test               |
| ***                |
+--------------------+
5 rows in set (0.00 sec)

mysql> select * from ***.***user;
+--------+-------------------------------------------+--------+
| name   | password                                  | active |
+--------+-------------------------------------------+--------+
| user1  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |
| user2  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |
| user3  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |
| zhang3 | zhang123                                  |      1 |
+--------+-------------------------------------------+--------+
4 rows in set (0.00 sec)
/數據庫***中***user表及其數據恢復成功

 

(3)、直接刪除***數據庫後其數據恢復
[root@centos6 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1558
Server version: 5.1.42-log Source distribution

Copyright (c) 2000, 2012, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cacti              |
| mysql              |
| test               |
| ***                |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database ***;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cacti              |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[root@centos6 ~]# mysql -uroot -pwxsemico *** < /backup/***_bk.sql
ERROR 1049 (42000): Unknown database '***'
[root@centos6 ~]# mysql -uroot -pwxsemico
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1572
Server version: 5.1.42-log Source distribution

Copyright (c) 2000, 2012, 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> create database ***;
Query OK, 1 row affected (0.00 sec)
/刪除***數據庫後恢復數據庫之前需先創建***數據庫
mysql> exit
Bye
[root@centos6 ~]# mysql -uroot -pwxsemico *** < /backup/***_bk.sql
[root@centos6 ~]# mysql -u root -pwxsemico
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1580
Server version: 5.1.42-log Source distribution

Copyright (c) 2000, 2012, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cacti              |
| mysql              |
| test               |
| ***                |
+--------------------+
5 rows in set (0.00 sec)

mysql> select * from ***.***user;
+--------+-------------------------------------------+--------+
| name   | password                                  | active |
+--------+-------------------------------------------+--------+
| user1  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |
| user2  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |
| user3  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |      1 |
| zhang3 | zhang123                                  |      1 |
+--------+-------------------------------------------+--------+
4 rows in set (0.00 sec)
/***數據庫及其***user表中的數據恢復成功

 

四、其他相關操作


show status
/顯示數據庫的運行狀態
show databases;
/顯示數據庫列表
use mysql;
/打開mysql數據庫
show tables;
/顯示mysql數據庫中的數據表
describle 表名;
/顯示數據表的結構
create database 庫名;
/創建特定名稱數據庫
drop database 庫名;
/刪除特定名稱數據庫
create table 表名(字段設定列表)
/創建數據庫中的特定表
drop table 表名;
/刪除特定名稱的表
delete from 表名;
/清空表記錄
select * from 表名;
/顯示錶中的記錄
insert into 表名 values(, ,)
/插入值到表中
alter table 表名 add column <字段名><字段選項>
/修改字段

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章