mySQL環境搭建

一、Windows下安裝MySQLhtml

二、Linux下安裝MySQLmysql

 

序章:sql

MySQL是個小型的數據庫,用來本身作小項目,作學習練習什麼的再適合不過了,不過新手總會被一些莫名奇妙的問題難住,想要學習什麼的,連環境都搭很差,簡直是受罪,我也是個飽受這種痛苦的新手,因此想把遇到的問題都總結下來,之後再碰到,不用處處去找資料。數據庫

 

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

可是仍是有不少像我這樣的強迫症患者,用了64位的操做系統,非要下64位的zip版本的MySQL內心才舒服。socket

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

 

一、Windows下安裝MySQLthis

我下的是最新版的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環境就已經安裝好了。

 

二、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)
相關文章
相關標籤/搜索