MySQL環境部署


閱讀目錄:

1、Windows下安裝MySQL

2、Linux下安裝MySQL

 

序章:

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

 

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

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

MySQL下載地址:http://dev.mysql.com/downloads/mysql/

 

1、Windows下安裝MySQL

我下的是最新版的MySQL,解壓后,目錄如下:

image

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

D:\Service\mysql57\bin>net start mysql
MySQL 服務正在啟動 .
MySQL 服務無法啟動。

服務沒有報告任何錯誤。

請鍵入 NET HELPMSG 3534 以獲得更多的幫助。

 

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

 

1. 進入dos的命令行,一定要用administrator進入。

image

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

D:\>cd D:\Service\mysql57\bin

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

3. 輸入以下命令,可以初始化MySQL數據庫,初始化了之后,會打印出MySQL的默認生成的密碼,下面標紅了的就是默認生成的密碼。

D:\Service\mysql57\bin>mysqld --initialize --user=root --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 root@localhost: g!gRw!d%M0Sj

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

image

4. 啟動MySQL服務

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

5. 使用默認生成的密碼,進入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>

6. 進入了mysql就可以修改默認密碼了(我把默認密碼修改成了root)

mysql> set password = password('root') ;

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

 

2、Linux下安裝MySQL

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

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

[root@bogon ~]# yum install mysql-server

2. 啟動MySQL服務,第一次啟動服務會有點慢

[root@bogon ~]# /etc/init.d/mysqld restart

3. 啟動了MySQL服務,就可以使用ps命令,可以查看到MySQL這個服務,說明服務已經啟動了

[root@bogon ~]# 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了

[root@bogon ~]# 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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM