一、安装

1、执行安装命令

  1. brew install mysql

2、安装完后启动mysql

  1. mysql.server start

3、执行安全设置

  1. mysql_secure_installation

显示如下

  1. There are three levels of password validation policy:
  2. LOW Length >= 8
  3. MEDIUM Length >= 8, numeric, mixed case, and special characters
  4. STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

按照提示选择密码等级,并设置root密码

二、创建新的数据库、用户并授权

1、登录mysql

  1. mysql -u root -p

按提示输入root密码

  1. root@poksi-test-2019:~# mysql -u root -p
  2. Enter password:

2、创建数据库

  1. create database retail_db character set utf8mb4;

3、创建用户

  1. create user 'retail_u'@'%' identified by 'retail_PWD_123';

4、授权用户

  1. grant all privileges on retail_db.* to 'retail_u'@'%';
  1. flush privileges;

5、查看当前的数据库

  1. show databases;

6、显示当前数据库的表单

  1. show tables

三、建表

  1. CREATE TABLE t_user(
  2. key_id VARCHAR(255) NOT NULL PRIMARY KEY, -- id 统一命名为key_id
  3. user_name VARCHAR(255) NOT NULL ,
  4. password VARCHAR(255) NOT NULL ,
  5. phone VARCHAR(255) NOT NULL,
  6. deleted INT NOT NULL DEFAULT 0, -- 逻辑删除标志默认值
  7. create_time timestamp NULL default CURRENT_TIMESTAMP, -- 创建时间默认值
  8. update_time timestamp NULL default CURRENT_TIMESTAMP -- 修改时间默认值
  9. ) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8mb4;

四、检查mysql服务状态

先退出mysql命令行,输入命令

  1. systemctl status mysql.service

显示如下结果说明mysql服务是正常的

  1. mysql.service - MySQL Community Server
  2. Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
  3. Active: active (running) since Wed 2019-05-22 10:53:13 CST; 13min ago
  4. Main PID: 16686 (mysqld)
  5. Tasks: 29 (limit: 4667)
  6. CGroup: /system.slice/mysql.service
  7. └─16686 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
  8. May 22 10:53:12 poksi-test-2019 systemd[1]: Starting MySQL Community Server...
  9. May 22 10:53:13 poksi-test-2019 systemd[1]: Started MySQL Community Server.
  1. 2020-11-03T06:55:47.684349Z 1 [Note] A temporary password is generated for root@localhost: X5TsRh8+O-Vu
  2. If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference manual.