---恢復內容開始---
上次裝Oracle出的問題真是太多太多了,MySQL會簡單許多,這次就來分析下MySQL在Linux上的安裝與初始化。同樣系統是centos7.x
MySQL的安裝方式很多。我主要用了2種方式:源碼包方式(安裝內容模塊可以自定義,穩定性高,但是比較費時間)二進制包安裝方式(比較方便,初始化安裝后就可以用)
一、二進制包方式
1、登錄MySQL官網:https://www.mysql.com/downloads/
2、選擇需要的安裝版本(社區版)
3、安裝
上傳下載的MySQL5.7x到centos7x虛擬機內,或者wget下載。
解壓:
[root@centos7 opt]# tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
[root@centos7 opt]# mv mysql-5.7.25-linux-glibc2.12-x86_64 /usr/local/mysql [root@centos7 opt]# cd /usr/local/mysql/
[root@centos7 mysql]# useradd mysql [root@centos7 mysql]# mkdir data [root@centos7 mysql]# chown -R mysql:mysql /usr/local/mysql/
5.7.x之后的版本初始化數據庫不再使用mysql_install_db,而是使用: bin/mysqld --initialize.
[root@centos7 bin]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 2019-02-21 17:05:27 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2019-02-21 17:05:31 [WARNING] The bootstrap log isn't empty: 2019-02-21 17:05:31 [WARNING] 2019-02-21T09:05:28.203493Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead [root@centos7 bin]#
使用mysqld --initialize:
[root@centos7 bin]# ./mysqld --initialize --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 2019-02-21T09:07:47.784915Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-02-21T09:07:47.786735Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting. 2019-02-21T09:07:47.786768Z 0 [ERROR] Aborting [root@centos7 bin]# rm -rf /usr/local/mysql/data/* [root@centos7 bin]# ./mysqld --initialize --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 2019-02-21T09:09:32.773141Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-02-21T09:09:34.064508Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-02-21T09:09:34.252282Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-02-21T09:09:34.320052Z 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: 67cc2fdb-35b8-11e9-a738-000c29186a7c. 2019-02-21T09:09:34.321735Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-02-21T09:09:34.330050Z 1 [Note] A temporary password is generated for root@localhost: >aHMp/_V3(Aq [root@centos7 bin]#
ps:注意保存最后面的密碼。
創建配置文件:
[root@centos7 bin]# vim /etc/my.cnf
發現存在my.cnf文件看來系統自帶了mariadb,卸載它。
[root@centos7 bin]# rpm -qa |grep mariadb mariadb-libs-5.5.52-1.el7.x86_64 [root@centos7 bin]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64 [root@centos7 bin]# rpm -qa |grep mariadb
現在已經卸載了系統自帶的mariadb,繼續創建my.cnf
編輯my.cnf:
[root@centos7 support-files]# cp ./mysql.server /etc/in init.d/ inittab inputrc [root@centos7 support-files]# cp ./mysql.server /etc/init.d/mysqld [root@centos7 support-files]# chmod +x /etc/init.d/mysqld
設置開機自啟動:
[root@centos7 support-files]# chkconfig --add mysqld [root@centos7 support-files]# chkconfig --list 注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 如果您想列出 systemd 服務,請執行 'systemctl list-unit-files'。 欲查看對特定 target 啟用的服務請執行 'systemctl list-dependencies [target]'。 mysqld 0:關 1:關 2:開 3:開 4:開 5:開 6:關 netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關 network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
將MySQL服務添加到系統服務以便設置開機自啟:(2、3、4、5級別啟動時開啟MySQL)
redhalt7系統使用了systemctl 不過保留了原來的service 仍然可以使用。
啟動MySQL:
[root@centos7 support-files]# service mysqld start
Starting MySQL.. ERROR! The server quit without updating PID file (/usr/local/mysql/data/centos7.pid).
出現錯誤:不能更新進程id文件:
解決重新進行初始化:
在初始化前先刪除data里的全部文件
[root@centos7 bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --pid-file=/usr/local/mysql/data/mysql.pid 2019-02-22T01:02:07.626070Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-02-22T01:02:08.873600Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-02-22T01:02:09.114675Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-02-22T01:02:09.207208Z 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: 7ac3daf9-363d-11e9-b3db-000c29186a7c. 2019-02-22T01:02:09.209157Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-02-22T01:02:09.213326Z 1 [Note] A temporary password is generated for root@localhost: s-jwb*inb5L4 [root@centos7 bin]# service mysqld start Starting MySQL.Logging to '/usr/local/mysql/data/centos7.err'. SUCCESS! [root@centos7 bin]# ps -ef |grep mysql root 2919 1 0 09:02 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/centos7.pid mysql 3121 2919 2 09:02 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=centos7.err --pid-file=/usr/local/mysql/data/centos7.pid --socket=/usr/local/mysql/data/mysql.sock --port=3306 root 3151 2080 0 09:02 pts/0 00:00:00 grep --color=auto mysql [root@centos7 bin]#
登錄MySQL:
在/usr/local/mysql/bin目錄下執行:
[root@centos7 bin]# ./mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.25 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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>
這里因為在/etc/my.cnf里加入了skip-grant-tables選項因此不需要輸入密碼就可以登錄,沒有設置該參數,輸入初始化產生的初始密碼就可以了。(為了安全在更改密碼后該參數需要刪除)
更改密碼;
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set authentication_string=password('123456') where user='root' -> ; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye
第一次密碼登錄root用戶后必須執行:
mysql> alter user 'root'@'localhost' identified by 'xxx' PASSWORD EXPIRE NEVER account unlock; #原本密碼有效期為360天需要取消這個限制。
mysql> flush privileges;
重新登錄mysql:
更改my.cnf
刪除紅框部分,保存!
重啟mysql
[root@centos7 bin]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
登錄后注意修改root密碼期限。
登錄MySQL並創建一個能夠遠程登錄的用戶:
使用sqlyog驗證:
二、使用源碼包方式安裝MySQL5.7x
1.安裝編譯工具和所需要的依賴庫:
yum -y install gcc gcc-c++ make autoconf libtool-ltdl-devel gd-devel freetype-devel libxml2-devel libjpeg-devel libpng-devel openssl-devel curl-devel bison patch unzip libmcrypt-devel libmhash-devel ncurses-devel sudo bzip2 flex libaio-devel
發現:
手動安裝它:
一共安裝4個文件:
先安裝libmcrypt 在安裝devel。mhash也是同樣
安裝cmake編譯工具:
下載;
wget http://www.cmake.org/files/v3.1/cmake-3.1.1.tar.gz
安裝:
tar zxvf cmake-3.1.1.tar.gz cd cmake-3.1.1 ./bootstrap make && make install
安裝MySQL:
下載:
地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
編譯:
[root@centos opt]# tar -zxvf mysql-boost-5.7.25.tar.gz [root@centos opt]# cd ./mysql-5.7.25/ [root@centos mysql-5.7.25]#
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/opt/mysql-5.7.25/boost \
-DSYSCONFDIR=/etc \
-DEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DENABLED_LOCAL_INFILE=1 \
-DEXTRA_CHARSETS=all
可能出現;
報錯代碼:
CMake Error at cmake/boost.cmake:81 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=
解決辦法:
1.在/usr/local下創建一個名為boost的文件夾
mkdir -p /usr/local/boost
2.進入這個新創建的文件夾然后下載boost
wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
3.解壓
tar -xvzf boost_1_59_0.tar.gz
4.重新cmake,后面更改以下參數
-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost/ \
安裝:
make && make install
這個過程大概持續30min.......
接下來就是數據庫的初始化:
初始化方法與二進制包方法一致:這里就不在贅述了。