Install mysql under mac

Keywords: MySQL Database mysqladmin Oracle

1. Download server and workbench

2. Install 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.
The prompted root password must be noted down.

  • At the bottom of the settings, you can see the mysql option, click Enter and Start
  • Copy the following two instructions to the terminal to run and give two directory addresses a name.
    The goal is that the next execution can directly execute mysql or mysqladmin without having 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
  • Ultimate solution
    Above is a temporary one, the terminal reboot is invalid.
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)
  • Modify 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 user name you will create, host - specifies which host the user can login on. If the local user can use localhost, if you want the user to login from any remote host, you can use the wildcard%. password - the user's login password, the password can be empty, if it is empty, the user may not need a password to login to the server.

Example: 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)
  • Looking at all users, there's actually 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 Password

Order:
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword')
If the current login user uses SET PASSWORD = PASSWORD("new password");

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

  • User name added, may not have permission to operate the table, need to add permissions
    Command: GRANT privileges ON database ename. tablename TO'username'@'host'

Description: privileges - User's operation rights, such as SELECT, INSERT, UPDATE, etc.
Use ALL if you want to grant permissions.
Database name - database name, tablename - table name
If you want to grant the user the corresponding operation rights for all databases and tables, you can use the * representation, such as *.*

Example: GRANT SELECT, INSERT ON test.user TO'test'@%';
GRANT ALL ON *.* TO 'test'@'%';

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

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

Description: Privilege, database ename, tablename - with the authorization section.

Examples: REVOKE SELECT ON*. * FROM'test'@%';

3. Installation of workbench

After installation, you can create databases and tables through workbench or by command.

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> 

Posted by lisa71283 on Fri, 17 May 2019 09:29:49 -0700