MySQL [database] Learning Series (a) install and configure frequently used commands

1. MySQL Learning Resources

The Tutorial-MySQL-Runoob
Runoob-SQL-the Tutorial
MySQL learning materials Summary

2. MySQL Installation

  • MySQL developed by the Swedish company MySQL AB, now part of Oracle Corporation
  • MySQL is an open source relational database management system
  • Into MySQL Community Edition and Enterprise Edition

Detailed installation tutorial: MySQL Installation (Mac version) - Denver

3. Mac PATH environment variable

Environment variable Details: the MAC and set the environment variable PATH PATH View

3.1 The current user-level environment variable

  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile
  • ~/.bashrc

3.2 the PATH Syntax

#中间用冒号隔开
export PATH=$PATH:<PATH 1>:<PATH 2>:<PATH 3>:------:<PATH N>

3.3 View PATH

echo $PATH

3.4 will be added to MySQL environment variables

(1) Add the environment variable to MySQL

If you do not configure the environment variables, execute the command mysql, mysql you must install directory (mysql directory) /usr/local/mysqlunder, so we choose to configure the environment variables

In the terminal, the user enters the directory, perform vim .bash_profileor direct execution vim ~/.bash_profile, add the following, press escenter :wqto exit and save

# mysql
export PATH=${PATH}:/usr/local/mysql/bin
#快速启动、结束MySQL服务, 可以使用alias命令
alias mysqlstart='sudo /usr/local/mysql/support-files/mysql.server start'
alias mysqlstop='sudo /usr/local/mysql/support-files/mysql.server stop'

(2) Use source ~/.bash_profileenvironment variable settings to take effect
Here Insert Picture Description
(3) Then we can execute mysql command at any place, start MySQL (password)

mysql -uroot -p

Here Insert Picture Description

4. MySQL directory structure

  • bin directory to store executable file
  • data catalog, store data files
  • docs directory, store documents
  • include directory contains the header file storage
  • lib directory, file repositories
  • share directory, error messages and character set files

5. Common Commands

5.1 mysql command

  • MySQL service is turned on (I ~/.bash_profileset the alias; password is computer code)
mysqlstart
  • Shut down MySQL service
mysqlstop
  • Start MySQL
mysql -uroot -p
  • Three ways to exit the MySQL
mysql > exit;
mysql > quit;
mysql > \q;

5.2 Common prompt

parameter description
\D Complete date
\d The current database
\h server nickname
\ in Current user

Does not modify the prompt, mysql default prompt is

mysql>

The prompt can not let us know which database operations, on which server

Modify MySQL prompt

Method a: is specified by the client connection parameters

mysql -uroot -p密码 --prompt 提示符

Method two: the client has connected to the prompt by prompt

mysql>prompt 提示符

MySQL database prompt arguments detailed

5.3 mysql mysql statement norms and common commands

Print version

SELECT VERSION();

Displays the current date and time

SELECT NOW();

Displays the current user

SELECT USER();

MySQL statement specification

  • Keywords and function names are all uppercase (lowercase although not error)
  • Database names, table names, field names all lower case
  • SQL statements must end with a semicolon

show databases;

5.4 Common parameters

parameter description
-u root 或 --user=root username
After -p XXX or -p Enter password input password
-P xxx The port number
-h or --host = name server nickname
-V or --version Output version information

6. Operation Database

6.1 Create a database CREATE

After the default installation comes with four mysql database

[]It represents write from time to write

CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[DEFAULT] CHARACTER SET [=] charset_name

Creating a database

CREATE DATABASE t1;

Show all databases

SHOW DATABASES;

Error message

SHOW WARNINGS

View encoding database created

SHOW CREATE DATABASE t1;

Encoding database is set utf8orgbk

SHOW CREATE DATABASE t2 CHARACTER SET utf8;

Here Insert Picture Description

6.2 modify the database ALTER

ALTER {DATABASE | SCHEMA} db_name
[DEFAULT] CHARACTER SET [=] charset_name

Here Insert Picture Description

6.3 Delete database DROP

DROP {DATABASE | SCHEME} [IF EXISTS] db_name

Here Insert Picture Description

7. integration issues

Mysql 7.1 solve every time you turn requires source ~ / .bash_profile

Zsh terminal used to transform, so it seems after starting the terminal will start from the zsh shell, so there is no command to be executed in .bash_profile

Solution: vim ~ / .zshrc

Add command: source ~ / .bash_profile

So each will be executed first source ~ / .bash_profile from Item2 start the terminal, so that you can directly access mysql

7.2 MySQL8.0 modified version of the password method

In the terminal the following command sequence

(1)mysql -uroot -p'原来的密码' 
(2)show databases;
(3)use mysql;
(4)ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的新密码'
(5)quit;
(6)mysql -uroot -p'你的新密码' 

Add Profile version 7.3 MySQL8.0 my.cnf

Mac default is no end of the configuration file my.cnf

Solution:
In the following command input terminal

sudo vim  /etc/my.cnf

Then you can add the contents of the configuration they want to add in the file, for example, the following code

[mysql]
prompt=\\U [\\d]>

Start mysql again, the effect is as follows
Here Insert Picture Description

Published 308 original articles · won praise 149 · Views 150,000 +

Guess you like

Origin blog.csdn.net/qq_43827595/article/details/104568850