安装MySQL
一、下载dmg包安装
1、下载MySQL dmg 包, 从官网 : http://www.mysql.com/downloads/ 进入, 点击下方的DOWNLOADS : MySQL Community Server
选择dmg文件下载
接着, 会跳转到如下页面, 你只需要选择不登录,直接下载即可
打开下载好的dmg文件,安装。
2、进入系统偏好设置,点击MySQL,开启MySQL服务
3、环境变量配置(windows也是这样run的。可以不配置, 但每次必须在msyql的安装目录下,执行mysql命令。)
在Finder的侧边栏中单击“应用程序”,然后在“实用工具”中,双击启动“终端”命令。
在终端中输入添加MySQL路径的命令:
普通权限
PATH="$PATH":/usr/local/mysql/bin
在终端登录到MySQL的命令如下:mysql -u root -p
如果显示如上图所示的内容,表示已经成功登陆MySQL服务器。
MySQL my.cnf配置文件解决中文乱码问题
1.在 /etc 新建 my.cnf 文件
因为private这个文件夹是隐藏的,可以打开终端使用命令 open /etc/
打开文件
sudo vim my.cnf
2.将如下配置内容写入到文件中
# Example MySQL config file for small systems.
#
# This is for a system with little memory (<= 64M) where MySQL is only used
# from time to time and it's important that the mysqld daemon
# doesn't use much resources.
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.
# The following options will be passed to all MySQL clients
[client]
default-character-set=utf8
#password = your_password
port = 3306
socket = /tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
#default-character-set=utf8
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
server-id = 1
# Uncomment the following if you want to log updates
#log-bin=mysql-bin
# binary logging format - mixed recommended
#binlog_format=mixed
# Causes updates to non-transactional engines using statement format to be
# written directly to binary log. Before using this option make sure that
# there are no dependencies between transactional and non-transactional
# tables such as in the statement INSERT INTO t_myisam SELECT * FROM
# t_innodb; otherwise, slaves may diverge from the master.
#binlog_direct_non_transactional_updates=TRUE
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /usr/local/mysql/data
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /usr/local/mysql/data
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
default-character-set=utf8
#no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
[mysqlhotcopy]
interactive-timeout
3.保存文件esc退出编辑模式,:wq退出,再重新启动MySQL。
终端进行查看
PATH="$PATH":/usr/local/mysql/bin
mysql -u root -p
输入修改后的密码
查看字符集
show variables like '%char%';
我这边显示的mysql字符集
+--------------------------+-----------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.20-macos10.12-x86_64/share/charsets/ |
+--------------------------+-----------------------------------------------------------+
8 rows in set (0.00 sec)
终端命令启动mysql
sudo /usr/local/mysql/support-files/mysql.server start
遇到的问题顺便在这里提下, 说不定能帮读者少走点弯路, 参考资料 http://stackoverflow.com/questions/13480170/access-denied-for-mysql-error-1045
在终端直接输入 Mysql -u root -p
, 弹出输入密码, 输入密码后就报如下错误
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
如果输入 mysql -u root
, 会显示另外一种错误
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
解决方案:
安装时候弹出:
2016-06-30T03:14:00.243422Z 1 [Note] A temporary password is generated for root@localhost: lKXyE0O(qd4o
If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference manual.
注意:临时密码就是lKXyE0O(qd4o
Mac OS X - 重置 MySQL Root 密码
您是否忘记了Mac OS 的MySQL的root密码? 通过以下4步就可重新设置新密码:
1. 停止 mysql server. 通常是在 ‘系统偏好设置’ > MySQL > ‘Stop MySQL Server’
2. 打开终端,输入:
sudo /usr/local/mysql/bin/mysqld_safe –skip-grant-tables
1. 打开另一个新终端,输入:
sudo /usr/local/mysql/bin/mysql -u root
注意:mysql>
UPDATE mysql.user SET authentication_string=PASSWORD(‘新密码’) WHERE User=’root’;
FLUSH PRIVILEGES;
\q
1. 重启MySQL.
*以上方法针对 MySQL V5.7.9, 旧版的mysql请使用:UPDATE mysql.user SET Password=PASSWORD(‘新密码’) WHERE User=’root’;
有时候连接数据库会失败,可能是密码问题,这个操作需要多。
Navicat Premium连接的数据库时候记得选上(Save password)保存密码
安装Navicat Premium
Navicat Premium for Mac 11.1.8 介绍
Navicat Premium for mac是一个可多重连接的数据库管理工具,Navicat 的功能足以符合专业开发人员的所有需求,但是对数据库服务器的新手来说又相当容易学习。它可让你以单一程序同時连接到目前世面上所有版本的主流数据库并进行管理和操作,支持的数据库有: MySQL、SQL Server、SQLite、Oracle 及 PostgreSQL。让管理不同类型的数据库更加方便。
Navicat Premium 结合了其他 Navicat 成员的功能。有了不同数据库类型的连接能力,Navicat Premium 支持在 MySQL、SQL Server、SQLite、Oracle 及 PostgreSQL 之间传输数据。包括存储过程,事件,触发器,函数,视图等。
Navicat Premium 适用于三种平台 – Microsoft Windows、Mac OS X 及 Linux。它可以让用户连接本机或远程服务器、提供一些实用的数据库工具如数据模型工具、数据同步、结构同步、导入、导出、备份、还原及报表以协助管理数据。
百度网盘下载地址:点击下载Navicat Premium for Mac 11.1.8
百度网盘提取密码:2mwc
安装完毕后打开
1.安装后第一次打开会弹出此框要求你连接mysql。连接名随便填,password 默认为 root.
2.完成之后,进入主界面你会看到你刚看创建的Connection.
3.打开myServerSQL,你会发现里面有几个已经创建的数据库,这是系统自动帮你创建的,不用管他。右键新建数据库new Database
Default Character Set 选择utf8.
Default Collation 选择utf8_bin.
这样选择可以避免出现中文乱码出现。
4.接下来在我们刚才创建的数据库下建表。
右键->new table:
在这个表中,我建立了三个域,ID,adminName,password。
在此界面下可以选择类型,长度,小数点后长度,是否为空,和设置为主键等功能。
选中一个域后,还可以在界面底部设置其他属性,有默认值,Comment,Column Format等
这里我给ID设为自增长。
点击save:
创建成功。
特别注意,最后在使用JDBC连接数据库时url应当在后面加上?useUnicode=true&characterEncoding=UTF-8
如下:
public class ConnectDB {
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//-----在url后面添加useUnicode=true&characterEncoding=UTF-8这些参数是为了让mysql数据库可以识别中文-----
String url = "jdbc:mysql://localhost:3306/vote?useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "root";
try {
connection = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
}
连接数据库时出现的问题:
我的解决办法是修改密码。在navicat下修改密码很简单
在User下找到与上面对应出异常的用户。这里是root@localhost
点击进入:
右键新建用户
在password 和 Confirm password 两项中填入root.
save即可解决问题。
- 在系统偏好 中,中止MYSQL服务.;
- cd/usr/local/mysql-…../bin
sudo ./mysqld_safe–skip-grant-tables - 登录MySQL;
mysql - 置空root用户的密码;
mysql> update mysql.user set password=‘’ whereUser=’root’;
mysql> flush privileges;
mysql> quit - 重新启动MySQL服务,
- 添加密码mysql> mysqladmin -u root -p password 123 密码改为123
参考链接:http://www.yiduoyu.com/87.html
参考链接:https://www.jianshu.com/p/326c1aaa1052