mysql 5个常用操作,添加用户配置对应数据库,开启远程访问,修改密码

时间:2022-09-18 22:35:46

1. 创建 mysql 用户:user1

mysql> CREATE USER 'user1'@'localhost' IDENTIFIED BY 'user1_password';

2. 创建数据库:test_db

mysql> CREATE DATABASE IF NOT EXISTS `test_db` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;

3. 用户授权数据库

只允许本地访问,使用如下:

mysql> GRANT ALL PRIVILEGES ON test_db.* TO user1@'localhost';
mysql> flush privileges;

4. 开启远程访问,使用如下:

mysql> GRANT ALL PRIVILEGES ON test_db.* TO user1@'%' IDENTIFIED BY 'user1_password';
mysql> flush privileges;

并开启mysql允许远程访问配置,在 my.cnf 47行, bind-address 前增加 # 号,注释掉 bind-address = 127.0.0.1
sudo vi /etc/mysql/my.cnf

#bind-address = 127.0.0.1

重启 mysql

sudo service mysql restart

5. mysql 修改用户名密码

mysql> use mysql;
mysql> UPDATE user SET password=password("test123") WHERE user='root';
mysql> flush privileges;
mysql> exit;

原文地址: http://www.howtoing.com/mysql-5-commonly-used-actions-add-user-configuration-database-open-the-remote-access-change-password/