文章目录
一、安装
方式一:pkg 包安装
https://dev.mysql.com/downloads/mysql/
方式二:brew
1、执行安装命令
$ brew install mysql
sad学这些
2、安装完后启动mysql
$ mysql.server start
3、执行安全设置
$ mysql_secure_installation
显示如下
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
按照提示选择密码等级,并设置root密码
- Enter password for user root:
输入刚刚得到的密码 - New password:
输入新的密码 - Re-enter new password:
重复输入新的密码 - Change the password for root ? ((Press y|Y for Yes, any other key for No)
是否想改变root的密码,输入Y,重复设置密码。 - Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No)
输入Y - Remove anonymous users? (Press y|Y for Yes, any other key for No)
删除匿名用户,输入Y - Disallow root login remotely? (Press y|Y for Yes, any other key for No)
是否禁止远程登录,输入N,(但事后发现还是不能远程登录,可以试试Y) - Remove test database and access to it? (Press y|Y for Yes, any other key for No)
是否删除test数据库,输入N - Reload privilege tables now? (Press y|Y for Yes, any other key for No)
是否重新加载权限表,输入Y
安装完成
可以通过mysql -uroot -p
登录,密码是刚刚设置的
如果上述密码没有设置完,这里会报错:
$ mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
二、创建新的数据库、用户并授权
1、登录mysql
mysql -u root -p
按提示输入root密码
root@poksi-test-2019:~# mysql -u root -p
Enter password:
2、创建数据库
create database retail_db character set utf8mb4;
3、创建用户
create user 'retail_u'@'%' identified by 'retail_PWD_123';
4、授权用户
grant all privileges on retail_db.* to 'retail_u'@'%';
flush privileges;
5、查看当前的数据库
> show databases;
选中数据库
use s_db;
6、显示当前数据库的表单
show tables;
三、建表
CREATE TABLE t_user(
key_id VARCHAR(255) NOT NULL PRIMARY KEY, -- id 统一命名为key_id
user_name VARCHAR(255) NOT NULL ,
password VARCHAR(255) NOT NULL ,
phone VARCHAR(255) NOT NULL,
deleted INT NOT NULL DEFAULT 0, -- 逻辑删除标志默认值
create_time timestamp NULL default CURRENT_TIMESTAMP, -- 创建时间默认值
update_time timestamp NULL default CURRENT_TIMESTAMP -- 修改时间默认值
) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8mb4;
四、检查mysql服务状态
先退出mysql命令行,输入命令
systemctl status mysql.service
显示如下结果说明mysql服务是正常的
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-05-22 10:53:13 CST; 13min ago
Main PID: 16686 (mysqld)
Tasks: 29 (limit: 4667)
CGroup: /system.slice/mysql.service
└─16686 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
May 22 10:53:12 poksi-test-2019 systemd[1]: Starting MySQL Community Server...
May 22 10:53:13 poksi-test-2019 systemd[1]: Started MySQL Community Server.
语法手册
https://dev.mysql.com/doc/refman/8.0/en/
报错等
1045, “Access denied for user ‘root’@‘localhost’ (using password: YES)”)
可能的错误原因
- 用户名或密码错误;
- 没有给用户授权
如果你使用 root 用户,可以使用下列语句:
mysql> GRANT ALL PRIVILEGES ON *.* to 'root'@'localhost' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES; # 刷新权限
Query OK, 0 rows affected (0.00 sec)
百度出来很多文章会给这样的语句,实际使用会报错:
因为 Grant 语句不需要使用 identified by。
mysql> GRANT ALL PRIVILEGES ON *.* to 'root'@'localhost' identified by '123456Aa*' WITH GRANT OPTION;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '123456Aa*' WITH GRANT OPTION' at line 1
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
使用 iterms 报这个错时,可以使用 terminal 试试。
caching_sha2_password
使用数据库管理工具,如 navicat 连接数据库,可能会报如下错误:
Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/lib/plugin/caching_sha2_password.so, 2): image not found
出现这个原因是mysql8 之前的版本中加密规则是 mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password。
解决问题方法有两种,一种是升级 navicat 驱动,一种是把 mysql 用户登录密码加密规则还原成mysql_native_password。
以下示例使用第二种方式:
$ mysql -u root -p # 进入 mysql 交互模式
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456Aa*' PASSWORD EXPIRE NEVER; # 修改加密规则
Query OK, 0 rows affected (0.02 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456Aa*'; # 更新用户的密码
Query OK, 0 rows affected (0.02 sec)
mysql> FLUSH PRIVILEGES; # 刷新权限
Query OK, 0 rows affected (0.00 sec)
再次使用客户端连接,
2003, "Can't connect to MySQL server on 'localhost' ([Errno 61] Connection refused)"