MySQL common management commands

1. mysql installation

	yum -y install mysql-server
	yum -y install php-mysql

	vi /etc/my.cnf
	[mysqld]
	datadir = / var / lib / mysql
	socket=/var/lib/mysql/mysql.sock
	user=mysql
	# Disabling symbolic-links is recommended to prevent assorted security risks
	symbolic-links=0
	default-character-set=utf8

	[mysqld_safe]
	log-error=/var/log/mysqld.log
	pid-file=/var/run/mysqld/mysqld.pid

	chkconfig mysqld on

	chkconfig --list mysqld

	/etc/rc.d/init.d/mysqld start initialize mysql

	mysqladmin -u root -p password

	delete from mysql.user where user=''; delete anonymous user

	flush privileges; ← Refresh to make the above operations take effect


2. connect to mysql
   mysql -h host address -u username -p user password

   mysql -h110.110.110.110 -uroot -pabcd123 (Note: u and root do not need to add spaces, the others are the same)

   mysql -uroot -p;
   mysql -uroot -pnewpassword;
   mysql mydb -uroot -p;
   mysql mydb -uroot -pnewp

3. Account Settings
   use mysql;
   delete from User where User="";
   update User set Password=PASSWORD('pass') where User='root';

4. Add account
  Add data to the user table
  use grant

  grant all on mydb.* to NewUserName@HostName identified by "password" ;
  grant usage on *.* to NewUserName@HostName identified by "password";
  grant select,insert,update on mydb.* to NewUserName@HostName identified by "password";
  grant update,delete on mydb.TestTable to NewUserName@HostName identified by "password";

  Allow remote management
  http://www.cnblogs.com/smallstone/archive/2010/04/29/1723838.html

  Global administrative rights:
	FILE: Read and write files on the MySQL server.
	PROCESS: Show or kill service threads belonging to other users.
	RELOAD: Reload the access control list, refresh the log, etc.
	SHUTDOWN: Shut down the MySQL service.
	Database/DataTable/DataColumn Permissions:
	Alter: Modify existing data tables (such as adding/deleting columns) and indexes.
	Create: Create a new database or data table.
	Delete: Delete the records of the table.
	Drop: Drop a data table or database.
	INDEX: Create or drop an index.
	Insert: Add records to the table.
	Select: Display/search table records.
	Update: Modify an existing record in the table.
  Special permissions:
	ALL: Allow to do anything (same as root).
	USAGE: Only allow login -- nothing else.
5. Change Password

  mysqladmin -u username -p old password password new password

  mysqladmin -uroot -password ab12 Note: Because root has no password at the beginning, the -p old password item can be omitted.

6. Backup the database

  mysqldump --opt school>school.bbb

  export the entire database
	mysqldump -u username -p --default-character-set=latin1 database name > exported file name (the default encoding of the database is latin1)
	mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql
  export a table
	mysqldump -u username -p database name table name > exported file name
	mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql
  export a database structure
	mysqldump -u wcnc -p -d –add-drop-table smgp_apps_wcnc >d:wcnc_db.sql
	-d no data --add-drop-table add a drop table before each create statement
  import database
	Common source commands
		Enter the mysql database console,
		Such as mysql -u root -p
		mysql>use database
		Then use the source command, followed by a script file (such as the .sql used here)
		mysql>source wcnc_db.sql

	Using the mysqldump command

		mysqldump -u username -p dbname < filename.sql

	use mysql command

		mysql -u username -p -D dbname < filename.sql


7. Change the table name:
	Command: rename table original table name to new table name;
	For example: in the table MyClass name is changed to YouClass
	mysql> rename table MyClass to YouClass;
  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327005691&siteId=291194637