Mac系統安裝MySQL及Navicat Premium
安裝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
- 1
在終端登入到MySQL的命令如下:mysql -u root -p
如果顯示如上圖所示的內容,表示已經成功登陸MySQL伺服器。
MySQL my.cnf配置檔案解決中文亂碼問題
1.在 /etc 新建 my.cnf 檔案
因為private這個資料夾是隱藏的,可以開啟終端使用命令 open /etc/
開啟檔案
sudo vim my.cnf
- 1
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
3.儲存檔案esc退出編輯模式,:wq退出,再重新啟動MySQL。
終端進行檢視
PATH="$PATH":/usr/local/mysql/bin
- 1
mysql -u root -p
- 1
輸入修改後的密碼
檢視字符集
show variables like '%char%';
- 1
我這邊顯示的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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
終端命令啟動mysql
sudo /usr/local/mysql/support-files/mysql.server start
- 1
在終端直接輸入 Mysql -u root -p
, 彈出輸入密碼, 輸入密碼後就報如下錯誤
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- 1
如果輸入 mysql -u root
, 會顯示另外一種錯誤
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- 1
解決方案:
安裝時候彈出:
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步就可重新設定新密碼:
-
停止 mysql server. 通常是在 ‘系統偏好設定’ > MySQL > ‘Stop MySQL Server’
-
開啟終端,輸入:
sudo /usr/local/mysql/bin/mysqld_safe –skip-grant-tables
- 開啟另一個新終端,輸入:
sudo /usr/local/mysql/bin/mysql -u root
注意:mysql>
UPDATE mysql.user SET authentication_string=PASSWORD(‘新密碼’) WHERE User=’root’;
FLUSH PRIVILEGES;
\q
- 重啟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。它可以讓使用者連線本機或遠端伺服器、提供一些實用的資料庫工具如資料模型工具、資料同步、結構同步、匯入、匯出、備份、還原及報表以協助管理資料。
安裝完畢後開啟
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
連線資料庫時出現的問題:
我的解決辦法是修改密碼。在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