Install mysql under mac

1. Download server and workbench

2. Install the server

2018-04-22T09:29:10.385999Z 1 [Note] A temporary password is generated for root@localhost: 8Jj)>opkXLuN
If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference manual.
提示的root密码一定要记下来

  • At the bottom of the settings, you can see the mysql option, click to enter and start
  • Copy the following two commands to the terminal to run, and give the two directory addresses a name.
    The purpose is that the next execution can directly execute mysql or mysqladmin, and there is no need to switch directories back and forth.
zhaojunyandeMBP:~ zhaojunyan$ alias mysql=/usr/local/mysql/bin/mysql
zhaojunyandeMBP:~ zhaojunyan$ alias mysqladmin=/usr/local/mysql/bin/mysqladmin
zhaojunyandeMBP:~ zhaojunyan$ mysql
  • The ultimate solution The
    above is a temporary solution, and it will be invalid after restarting the terminal.
zhaojunyandeMBP:~ zhaojunyan$ sudo vim ~/.bash_profile 
zhaojunyandeMBP:~ zhaojunyan$ source ~/.bash_profile 
zhaojunyandeMBP:~ zhaojunyan$ cat ~/.bash_profile 
export GOPATH=/Users/zhaojunyan/go
export GOBIN=$GOPATH/bin
export MYSQLBIN=/usr/local/mysql/bin
export PATH=$PATH:$GOBIN:$MYSQLBIN

zhaojunyandeMBP:~ zhaojunyan$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.16 MySQL Community Server (GPL)
  • Change the password, the initial password has been written down before
zhaojunyandeMBP:~ zhaojunyan$ **mysql -uroot -p**
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.16

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password for 'root'@'localhost' = password('root');
Query OK, 0 rows affected, 1 warning (0.01 sec)
  • Add User
    Command : CREATE USER 'username'@'host' IDENTIFIED BY 'password';

Description: username - the username you will create, host - specify the host on which the user can log in, if it is a local user, you can use localhost. If you want the user to log in from any remote host, you can use the wildcard %. password - the The user's login password, the password can be empty, if it is empty, the user does not need a password to log in to the server.

例子: CREATE USER ‘test’@’localhost’ IDENTIFIED BY ‘123456’;
CREATE USER ‘test’@’%’ IDENTIFIED BY ‘123456’;
CREATE USER ‘test’@’%’ IDENTIFIED BY ”;
CREATE USER ‘test’@’%’;

mysql> create user 'test'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
  • View all users, in fact, there is also a table
mysql> select User,Host FROM mysql.user;
+-----------+-----------+
| User      | Host      |
+-----------+-----------+
| mysql.sys | localhost |
| root      | localhost |
| test      | localhost |
+-----------+-----------+
3 rows in set (0.00 sec)

mysql> quit
  • Setting and changing user passwords

Command:
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword')
If it is the current login user, use SET PASSWORD = PASSWORD("newpassword");

Example: SET PASSWORD FOR 'test'@'%' = PASSWORD("123456");

  • The user name is added, and there may be no permission to operate the table. You need to add the permission
    Command : GRANT privileges ON databasename.tablename TO 'username'@'host'

Description: privileges - the user's operation authority, such as SELECT, INSERT, UPDATE, etc.
If you want to grant the authority, use ALL.
databasename - database name, tablename-table name
If you want to grant the user the corresponding operation authority on all databases and tables, then It can be represented by *, such as *.*

例子: GRANT SELECT, INSERT ON test.user TO ‘test’@’%’;
GRANT ALL ON *.* TO ‘test’@’%’;

Note: The user authorized by the above command cannot authorize other users.
If you want the user to be authorized, use the following command:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;

  • Revoke authorization
    command :
    REVOKE privilege ON databasename.tablename FROM 'username'@'host';

Description: privilege, databasename, tablename - same as authorization section.

例子: REVOKE SELECT ON *.* FROM ‘test’@’%’;

3. Install workbench

After installation, you can create databases and tables through workbench, or you can create them through commands

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)

mysql> create table person(name varchar(20), age tinyint(4));
ERROR 1046 (3D000): No database selected
mysql> use testdb;
Database changed
mysql> create table person(name varchar(20), age tinyint(4));
Query OK, 0 rows affected (0.02 sec)

mysql> 

Guess you like

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