Query all current users/IP/passwords: use mysql; select user,host,password from user;
Refresh user table permissions (required to run when modifying users or user permissions): FLUSH privileges;
添加用户:CREATE USER ‘username’@’host’ IDENTIFIED BY ‘password’;(例: CREATE USER ‘test’@’localhost’ IDENTIFIED BY ‘test’;)
Authorize users: GRANT privileges ON databasename.tablename TO 'username'@'host'; (Example: GRANT privileges ON mysql.user TO 'test'@'localhost';)
Authorize all privileges to the user: GRANT ALL privileges ON . TO 'username'@'host'; (Example: GRANT ALL privileges ON . TO 'test'@'localhost';)
撤销用户权限:REVOKE privilege ON databasename.tablename FROM ‘username’@’host’;(例: REVOKE privilege ON mysql.user FROM ‘test’@’localhost’;)
View all permissions of the current user: SHOW grants FOR 'username'@'host'; (Example: SHOW grants FOR 'test'@'localhost';)
Change user password: SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword'); (Example: SET PASSWORD FOR 'test'@'localhost' = PASSWORD('test'); )
To change the root user password when the root password is forgotten:
First stop the mysql service service mysql stop
Open a new window and run mysqld_safe –skip-grant-tables& (open mysql safe mode)
Run mysql in another window (login mysql)
在mysql命令行中运行 use mysq; update user set password=password(‘password’) where user = ‘username’;(例:update user set password=password(‘root’) where user = ‘root’;)
Delete user: DROP USER 'username'@'host'; (Example: DROP USER 'test'@'localhost';)
Single table backup: mysqldump -u username -p dbname tablename > BackName.sql (example: mysqldump -uroot -p test tablename1 > all.sql)
Mysql data import
source ** .sql (Example: source /tmp/test.sql, if the sql file includes the creation of the database, you do not need to select the database, if not, you need to select the database first)
Mysql csv related
导出csv文件:select 字段名1,字段名2.. from dbname.tablename into outfile ‘BackupName.csv’ fields terminated by ‘,’ optionally enclosed by ” lines terminated by ‘/n’;(例:select user,host,password from user into outfile ‘/tmp/test.sql’ fields terminated by ‘,’ optionally enclosed by ” lines terminated by ‘/n’;)
Unable to export file
Run SHOW VARIABLES LIKE "secure_file_priv"; view output folder
View output folders
If the returned result is empty or you want to change the export directory, you need to change the mysql configuration. The default configuration file of mysql is in /etc/my.cnf. You can check whether there is a secure_file_priv option. If so, you can change it directly. If not, you need to add secure_file_priv=output Folder (for example: secure_file_priv=/tmp/), if you change the configuration, you need to restart mysql.
导入csv文件:load data local infile ‘BackupName.csv’ into table tablename fields terminated by ‘,’ lines terminated by ‘/n’(first_name,last_name,email);
Common Commands and Techniques
Default configuration file address: /etc/my.cnf
Show all databases: show tables
Selected library: use library name (eg: use mysql)
Show all tables of the currently selected library: show tables
Display all field descriptions of a table: desc table name (for example: desc user)