1. 程式人生 > >MySQL環境部署

MySQL環境部署

MySql 環境部署

本Markdown編輯器使用[StackEdit][6]修改而來,用它寫部落格,將會帶來全新的體驗哦:

序章:

MySQL是個小型的資料庫,用來自己做小專案,做學習練習什麼的再適合不過了,不過新手總會被一些莫名奇妙的問題難住,想要學習什麼的,連環境都搭不好,簡直是受罪,我也是個飽受這種痛苦的新手,所以想把遇到的問題都總結下來,以後再碰到,不用到處去找資料。

新手在Windows環境下,建議下載Installer MSI版本的,安裝簡單直接Next…直到Finish…完成安裝,雖然只有32位的,但是作為學習練習,還是夠用了(比如學習Java、Python、C#、SQL等語言),可不能輸在搭建環境上,對吧!

但是還是有很多像我這樣的強迫症患者,用了64位的作業系統,非要下64位的zip版本的MySQL心裡才舒服。

1.windows下安裝mysql
下載最新版本的mysql

這裡寫圖片描述

可以看到上圖,MySQL5.7它沒有data目錄,如果沒有data目錄,安裝後啟動的時候就會報這個錯:

D:\Service\mysql57\bin>net start mysql
MySQL 服務正在啟動 .
MySQL 服務無法啟動。
服務沒有報告任何錯誤。
請鍵入 NET HELPMSG 3534 以獲得更多的幫助。

為了避免這個錯誤,需要使用命令生成data資料夾,按如下步驟安裝

  1. 首先需要將MySQL的bin錄了新增到環境變數,bin目錄下都是執行檔案,如果沒有放進環境變數,就需要進入到mysql的bin目錄下執行相關指令
  2. 將my-default.ini修改為my.ini,並且放到bin目錄下
  3. 修改my.ini,主要修改basedir和datadir,如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. # basedir = D:\Service\mysql57 # datadir = D:\Service\mysql57\data # port = ..... # server_id = ..... # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  1. 進入dos的命令列,一定要用administrator進入。

這裡寫圖片描述

  1. 進入MySQL的bin目錄,輸入mysqld –install可以安裝MySQL

D:>cd D:\Service\mysql57\bin
D:\Service\mysql57\bin>mysqld -install
Service successfully installed.

  1. 輸入以下命令,可以初始化MySQL資料庫,初始化了之後,會打印出MySQL的預設生成的密碼,下面標紅了的就是預設生成的密碼。
D:\Service\mysql57\bin>mysqld --initialize --user=mysql --console
2015-12-20T08:13:45.264865Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-12-20T08:13:45.854579Z 0 [Warning] InnoDB: New log files created, LSN=45790
2015-12-20T08:13:45.998772Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2015-12-20T08:13:46.098118Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9755c3ea-a6f1-11e5-81a3-74d02b122fb3.
2015-12-20T08:13:46.121617Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2015-12-20T08:13:46.135153Z 1 [Note] A temporary password is generated for [email protected]: g!gRw!d%M0Sj

初始化了以後,可以看到MySQL目錄下,多了data目錄

這裡寫圖片描述

  1. 啟動MySQL服務

D:\Service\mysql57\bin>net start mysql
MySQL 服務正在啟動 .
MySQL 服務已經啟動成功。

  1. 使用預設生成的密碼,進入mysql
D:\Service\mysql57\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.10

Copyright (c) 2000, 2015, 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.

mysql>
  1. 進入了mysql就可以修改預設密碼了(我把預設密碼修改成了root)

mysql> set password = password(‘root’) ;

這個時候,Windows下的MySQL環境就已經安裝好了。

2、Linux下安裝MySQL

如果有網路的話,Linux下安裝就簡單多了,我這用的CentOS安裝的

  1. 安裝MySQL服務,下面用yum安裝,它會自動安裝需要的依賴包,很方便,但是要用root使用者來安裝

[[email protected] ~]# yum install mysql-server
2. 啟動MySQL服務,第一次啟動服務會有點慢

[[email protected] ~]# /etc/init.d/mysqld restart
3. 啟動了MySQL服務,就可以使用ps命令,可以檢視到MySQL這個服務,說明服務已經啟動了

[[email protected] ~]# ps -ef | grep mysql
root 3474 1 0 22:29 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe –datadir=/var/lib/mysql –socket=/var/lib/mysql/mysql.sock –pid-file=/var/run/mysqld/mysqld.pid –basedir=/usr –user=mysql
mysql 3576 3474 0 22:29 pts/0 00:00:00 /usr/libexec/mysqld –basedir=/usr –datadir=/var/lib/mysql –user=mysql –log-error=/var/log/mysqld.log –pid-file=/var/run/mysqld/mysqld.pid –socket=/var/lib/mysql/mysql.sock
root 3614 3334 0 22:34 pts/0 00:00:00 grep mysql
4. 直接輸入mysql就可以進入MySQL了

[[email protected] ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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.

mysql>
5. 設定MySQL的密碼,我這邊設定密碼為root,以後就可以用這個密碼來登入MySQL了

mysql> set password = password(‘root’);
Query OK, 0 rows affected (0.00 sec)

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = D:\Service\mysql57
# datadir = D:\Service\mysql57\data
# port = .....
# server_id = .....


# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES