Mysql usage skills and experience

This article is for personal notes only

  • Mysql user operation
    • 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';)
  • Mysql data backup
    • Only import table structure: mysqldump –opt -d dbname -u username -p > BackupName.sql (example: mysqldump –opt -d test -uroot -p > BackupName.sql)
    • 整库备份:mysqldump -u username -p –all-databases > BackupName.sql (例:mysqldump -uroot -p –all-databases > all.sql)
    • 多库备份:mysqldump -u username -p –databases dbname1 dbname2 …-> BackupName.sql(例: mysqldump -uroot -p –databases mysql test > all.sql)
    • Single database backup: mysqldump -u username -p dbname > BackupName.sql (Example: mysqldump -uroot -p mysql > mysql.sql)
    • 多表备份:mysqldump -u username -p –databases dbname –tables tablename1 tablename2 …> BackupName.sql(例:mysqldump -root -p –databases test –tables tablename1 tablename2 > all.sql)
    • 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)
    • Show master table status: show master status
    • Show slave table status: show slave status

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325428387&siteId=291194637