Mac如何安装MySQL

  1. 在终端输入brew install mysql安装MySQL,完成之后重启终端。

    We've installed your MySQL database without a root password. To secure it run:

    ​ mysql_secure_installation

    MySQL is configured to only allow connections from localhost by default

    To connect run:

    ​ mysql -uroot

    To have launchd start mysql now and restart at login:

    brew services start mysql

    Or, if you don't want/need a background service you can just run:

    mysql.server start

    ==> Summary

    /usr/local/Cellar/mysql/5.7.19: 322 files, 233MB

    上面说已经安装了MySQL但是没有密码。

  2. 输入mysql再按tab会提示一些和mysql相关的命令,证明安装成功

    mysql mysqlbinlog

    mysql.server mysqlcheck

    mysql_client_test mysqld

    mysql_client_test_embedded mysqld_multi

    mysql_config mysqld_safe

    mysql_config_editor mysqldump

    mysql_embedded mysqldumpslow

    mysql_install_db mysqlimport

    mysql_plugin mysqlpump

    mysql_secure_installation mysqlshow

    mysql_ssl_rsa_setup mysqlslap

    mysql_tzinfo_to_sql mysqltest

    mysql_upgrade mysqltest_embedded

    mysqladmin mysqlxtest

至此,mysql就安装完了。

MySQL简单上手

  1. MySQL和直接使用sqlite不同,需要有一个MySQL server在,之后通过socket去链接上MySQL,然后才可以对数据库进行相关操作。

    1. 启动和关闭服务

      mysql.server start
      Starting MySQL
      . SUCCESS! 
      
      mysql.server stop
      Shutting down MySQL
      . SUCCESS! 
      

      之后重新开启,来进行后面的操作

      1. 配置root用户

        MySQL有自己的用户体系,默认用户是 root账户,没有密码。可以通过下列命令来设置密码

        mysqladmin -u root password
        New password: 
        Confirm new password: 
        Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
        

        包括mysqladmin在内,其他的mysql需要指明用户的时候加 -u username,如果用户添加过密码,需要使用-p指令,所以如果已经设置过root用户的密码之后再修改密码,后面就需要加-p,否则按照前面的就会报错

        mysqladmin -u root password
        mysqladmin: connect to server at 'localhost' failed
        error: 'Access denied for user 'root'@'localhost' (using password: NO)'
        
        mysqladmin -u root password -p
        Enter password: 
        New password: 
        Confirm new password: 
        Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
        

      2. 使用root用户连接数据库

        mysql -u root -p
        Enter password: 
        Welcome to the MySQL monitor.  Commands end with ; or \g.
        Your MySQL connection id is 6
        Server version: 5.7.19 Homebrew
        
        Copyright (c) 2000, 2017, 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.
        
      3. 一些基本操作

        • 查看数据库

          SHOW DATABASES;
          +--------------------+
          | Database           |
          +--------------------+
          | information_schema |
          | mysql              |
          | performance_schema |
          | sys                |
          +--------------------+
          4 rows in set (0.01 sec)
          
        • 创建数据库 使用数据库 查看表

          CREATE DATABASE mydb;
          Query OK, 1 row affected (0.00 sec)
          SHOW DATABASES;
          +--------------------+
          | Database           |
          +--------------------+
          | information_schema |
          | mydb               |
          | mysql              |
          | performance_schema |
          | sys                |
          +--------------------+
          5 rows in set (0.00 sec)
          USE mydb
          Database changed
          SHOW TABLES;
          Empty set (0.00 sec)
          

创建一个新用户

​ root用户的权限一般不会随便拿去使用,所以创建一些用户,并分配对应的权限。下面的命令创建的是一个名字是user12 密码是123456 的本地用户

CREATE USER user12@localhost IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
之后分配mydb数据库的权限
GRANT ALL ON mydb.* to user12@localhost;
Query OK, 0 rows affected (0.00 sec)

GUI 工具

Mac 有一个好用且免费的GUI工具 Sequel Pro

用socket链接数据库之后,可以进行很多操作。

以上大部分内容来源自binderclip 在此表示感谢

你可能感兴趣的:(Mac如何安装MySQL)