mysql database using summary
Mysql command some of the major recording paper for everyday use, for future inquiries.
1. Change the root password
table of Contents
6. Review all of the fields in a table
9. Create a new database (you can specify the character set)
11. Review the database version
13. query the database parameters
14. The modified database parameters
15. Review the current database queue
16. Create a regular user and licensed to a database
19. Change the table of a row of data
22. Empty all tables in the database (the database name is eab12)
28. Check the user name rights
29. mysql command script execution
mysqladmin -uroot password 'yourpassword'
2. Telnet server mysql
mysql -uroot -p -h192.168.137.10 -P3306
3. Query database
show databases;
4. Go to a database
use databasename;
5. List database table
show tables;
6. Review all of the fields in a table
desc slow_log;
show create table slow_log\G; (不仅可以显示表信息,还可以显示建表语句)
7. Review the current user
select user();
8. View current database
select database();
9. Create a new database (you can specify the character set)
create database db1 charset utf8;
10. Create a new table
create table t1 (`id` int(4), `name` char(40));
11. Review the database version
select version();
12. Review the database state
show status; 当前会话状态
show global status; 全局数据库状态
show slave status\G; 查看主从数据库状态信息
13. query the database parameters
show variables;
14. The modified database parameters
show variables like 'max_connect%';
set global max_connect_errors = 1000;(重启数据库会失效,要在配置文件中修改)
15. Review the current database queue
show processlist;
16. Create a regular user and licensed to a database
grant all on databasename.* to 'user1'@'localhost' identified by '123456';
17. The data look-up table
select * from mysql.db; //查询该表中的所有字段
select count(*) from mysql.user; //count(*)表示表中有多少行
select db,user from mysql.db; //查询表中的多个字段
select * from mysql.db where host like '10.0.%'; //在查询语句中可以使用万能匹配 “%”
18. The insert a row
insert into db1.t1 values (1, 'abc');
19. Change the table of a row of data
update db1.t1 set name='aaa' where id=1;
20. Empty table data
truncate table db1.t1;
21. Delete table
drop table db1.t1;
22. Empty all tables in the database (the database name is eab12)
mysql -N -s information_schema -e "SELECT CONCAT('TRUNCATE TABLE ',TABLE_NAME,';') FROM TABLES WHERE TABLE_SCHEMA='eab12'" | mysql -f eab12
23. Delete the database
drop database db1;
24. Database Backup
mysqldump -uroot -p'yourpassword' mysql >/tmp/mysql.sql
25. Database Recovery
mysql -uroot -p'yourpassword' mysql </tmp/mysql.sql
26. New Ordinary users
CREATE USER name IDENTIFIED BY 'ssapdrow';
27. Change the user password
SET PASSWORD FOR name=PASSWORD('fdddfd');
28. Check the user name rights
SHOW GRANTS FOR name;
29. mysql command script execution
mysql -uuser -ppasswd -e"show databases"
echo "show databases"|mysql -uuser -ppassword
以下是执行大量mysql语句采用的方式
mysql -uuser -hhostname -ppasswd <<EOF
mysql语句
EOF
30. Modify the database name
-
If all the tables are MyISAM type, you can change the name of the folder directly.
→ Close the mysql db_name directory data directory rename new_db_name → open mysql -
New database, the new database rename all the old database tables, and then delete the old database. Specific operation command is as follows: Create a new database → rename the table name → delete the old database.
CREATE DATABASE new_db_name; RENAME TABLE db_name.table1 TO new_db_name.table1,db_name.table2 TO new_db_name.table2; DROP DATABASE db_name;
-
Use mysqldump command to export the data from the old data, and then import the new database. To do the following command: export data to create a new database → → → import data to delete the old database.
mysqldump -u root -p -h ip db_name > db_name_dump.SQL mysql -u root -p -h ip -e “CREATE DATABASE new_db_name” mysql -u root -p -h ip new_db_name < db_name_dump.SQL mysql -u root -p -h ip -e “DROP DATABASE db_name”