您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Linux 7如何安裝Mysql 5.7”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Linux 7如何安裝Mysql 5.7”這篇文章吧。
在Oracle Linux 7.1中安裝MySql 5.7 。mysql安裝位置:/mysqlsoft/mysql,數據庫文件數據位置:/mysqldata/mysql。
1.首先下載安裝介質
mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
2. 在根目錄下創建文件夾mysqlsoft和數據庫數據文件/mysqldata/mysql
[root@cs2 /]# mkdir -p /mysqlsoft [root@cs2 /]# mkdir -p /mysqldata/mysql [root@cs2 /]# ls -lrt /mysqldata/ total 0 drwxr-xr-x 2 root root 6 May 31 11:58 mysql
3.上傳介質mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz到/mysqlsoft目錄中并解壓
[root@cs2 /]# cd mysqlsoft [root@cs2 mysqlsoft]# ls -lrt total 628704 -rw-r--r-- 1 root root 643790848 Apr 20 2018 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz [root@cs2 mysqlsoft]# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz [root@cs2 mysqlsoft]# ls -lrt total 628704 -rw-r--r-- 1 root root 643790848 Apr 20 2018 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz drwxr-xr-x 9 root root 120 May 31 12:11 mysql-5.7.22-linux-glibc2.12-x86_64
建議一般不要修改默認文件名,通過軟連接來完成
[root@cs2 mysqlsoft]# mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql [root@cs2 mysqlsoft]# ls -lrt total 628704 -rw-r--r-- 1 root root 643790848 Apr 20 2018 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz drwxr-xr-x 9 root root 120 May 31 13:28 mysql-5.7.22-linux-glibc2.12-x86_64 lrwxrwxrwx 1 root root 35 May 31 13:33 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64 [root@cs2 mysqlsoft]# cd mysql [root@cs2 mysql]# ls bin COPYING docs include lib man README share support-files
4. 創建mysql用戶與用戶組
[root@cs2 mysql]# groupadd mysql [root@cs2 mysql]# useradd -r -g mysql -s /bin/false mysql
因為用戶只用于所有權目的,而不是登錄目的,useradd命令使用-r與-s /bin/false選項來創建一個用戶沒有登錄服務器主機的權限。
5.修改/mysqlsoft/mysql與/mysqldata/mysql目錄權限
[root@cs2 /]# chown -R mysql:mysql /mysqlsoft/mysql [root@cs2 /]# chown -R mysql:mysql /mysqldata/mysql [root@cs2 /]# chmod -R 775 /mysqlsoft/mysql [root@cs2 /]# chmod -R 775 /mysqldata/mysql
6. MySQL對于libaio庫有依賴性。臺果這個libaio庫沒有安裝那么數據目錄初始化與后續的數據庫服務啟動將會失敗,安裝libaio庫執行以下操作:
查詢是否安裝了libaio庫
[root@cs2 local]# yum search libaio Loaded plugins: langpacks Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast =========================================================================================================== N/S matched: libaio ============================================================================================================ libaio.i686 : Linux-native asynchronous I/O access library libaio.x86_64 : Linux-native asynchronous I/O access library libaio-devel.i686 : Development files for Linux-native asynchronous I/O access libaio-devel.x86_64 : Development files for Linux-native asynchronous I/O access Name and summary matches only, use "search all" for everything.
如果沒有安裝,可以執行下面的命令來安裝
[root@cs2 local]# yum install libaio Loaded plugins: langpacks Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast file:///run/media/jy/OL-7.1%20Server.x86_64/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /run/media/jy/OL-7.1%20Server.x86_64/repodata/repomd.xml" Trying other mirror. Package libaio-0.3.109-12.el7.x86_64 already installed and latest version Nothing to do
7.配置mysql參數
只是設置幾個簡單的mysql運行參數
[root@cs2 ~]# vi /mysqlsoft/mysql/my.cnf [mysqld] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql bind-address=0.0.0.0 user=mysql port=3306 log-error=/mysqldata/mysql/mysql.err pid-file=/mysqldata/mysql/mysqld.pid socket = /mysqldata/mysql/mysql.sock character-set-server=utf8 default-storage-engine=INNODB explicit_defaults_for_timestamp = true "/mysqlsoft/mysql/my.cnf" [New] 67L, 1642C written
注意:log-error 一定要配置,因為如果mysql啟動錯誤,可以從日志文件中找到錯誤原因。其次bind—address配置0.0.0.0是為了監聽所有的連接。還有就是socket參數所指定的mysql.sock文件的路徑最好設置為/tmp/mysql.sock,因為unix socket文件的缺省位置在/tmp目錄中。
8.初始化mysql
[root@cs2 /]# cd /mysqlsoft/mysql/bin [root@cs2 bin]# ./mysqld --user=mysql --defaults-file=/mysqlsoft/mysql/my.cnf --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql --initialize [root@cs2 mysql]# cat mysql.err 2019-05-31T06:01:50.260643Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. 2019-05-31T06:01:50.260731Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set. 100 100 2019-05-31T06:01:53.795162Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-05-31T06:01:54.049268Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-05-31T06:01:54.102171Z 0 [ERROR] unknown variable 'defaults-file=/mysqlsoft/mysql/my.cnf' 2019-05-31T06:01:54.102193Z 0 [ERROR] Aborting
網上有人說是文件權限的問題,然后重新授權chmod 664 my.cnf 但并沒有解決,有人建議調整一下參數順序就好!!!最后調整了一下參數順序果然有效:
[root@cs2 bin]# ./mysqld --defaults-file=/mysqlsoft/mysql/my.cnf --initialize --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql --user=mysql [root@cs2 mysql]# cat mysql.err 2019-05-31T06:05:06.362925Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. 2019-05-31T06:05:06.362994Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set. 100 100 2019-05-31T06:05:09.779913Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-05-31T06:05:10.026707Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-05-31T06:05:10.094462Z 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: 0be6983a-836a-11e9-a341-005056a092af. 2019-05-31T06:05:10.109209Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-05-31T06:05:10.110107Z 1 [Note] A temporary password is generated for root@localhost: ,;pm93qnL%-j 2019-05-31T06:05:14.966324Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode. 2019-05-31T06:05:14.966373Z 1 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode. 2019-05-31T06:05:14.966391Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode. 2019-05-31T06:05:14.966419Z 1 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode. 2019-05-31T06:05:14.966428Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode. 2019-05-31T06:05:14.966441Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode. 2019-05-31T06:05:14.966493Z 1 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode. 2019-05-31T06:05:14.966508Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
其中[Note] A temporary password is generated for root@localhost: ,;pm93qnL%-j的root@localhost: 后面跟的是mysql數據庫登錄的臨時密碼,各人安裝生成的臨時密碼不一樣。可以看到到日志文件沒有報錯,而且有了臨時密碼,表示初始化成功。
9. 如果想服務能夠部署自動支持安全連接,使用mysql_ssl_rsa_setup工具來創建缺省SSL與RSA文件
[root@cs2 bin]# ./mysql_ssl_rsa_setup --datadir=/mysqldata/mysql Generating a 2048 bit RSA private key ......................................................................+++ ..............................................................+++ writing new private key to 'ca-key.pem' ----- Generating a 2048 bit RSA private key .............+++ ..............+++ writing new private key to 'server-key.pem' ----- Generating a 2048 bit RSA private key .....................................+++ ................................................+++ writing new private key to 'client-key.pem' -----
9.啟動mysql服務
[root@cs2 /]# sh /mysqlsoft/mysql/support-files/mysql.server start /mysqlsoft/mysql/support-files/mysql.server: line 239: my_print_defaults: command not found /mysqlsoft/mysql/support-files/mysql.server: line 259: cd: /usr/local/mysql: No such file or directory Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
啟動mysql服務命令會報錯,因為沒有修改mysql的配置文件
修改Mysql配置文件,修改前為以下內容
if test -z "$basedir" then basedir=/usr/local/mysql bindir=/usr/local/mysql/bin if test -z "$datadir" then datadir=/usr/local/mysql/data fi sbindir=/usr/local/mysql/bin libexecdir=/usr/local/mysql/bin else bindir="$basedir/bin" if test -z "$datadir" then datadir="$basedir/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" fi
修改后的內容如下
[root@cs2 ~]# vi /mysqlsoft/mysql/support-files/mysql.server if test -z "$basedir" then basedir=/mysqlsoft/mysql bindir=/mysqlsoft/mysql/bin if test -z "$datadir" then datadir=/mysqldata/mysql fi sbindir=/mysqlsoft/mysql/bin libexecdir=/mysqlsoft/mysql/bin else bindir="$basedir/bin" if test -z "$datadir" then datadir="$basedir/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" fi [root@cs2 ~]# cp /mysqlsoft/mysql/support-files/mysql.server /etc/init.d/mysqld [root@cs2 ~]# chmod 755 /etc/init.d/mysqld
10.啟動mysql
[root@cs2 ~]# service mysqld start Starting MySQL.. SUCCESS!
11.配置環境變量
[root@cs2 ~]# vi /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates. export MYSQL_HOME=/mysqlsoft/mysql/ export PATH=$PATH:$MYSQL_HOME/bin
11.登錄Mysql
初始化成功后,查看初始化密碼
[root@cs2 ~]# cat /mysqldata/mysql/mysql.err | grep password 2019-05-31T06:05:10.110107Z 1 [Note] A temporary password is generated for root@localhost: ,;pm93qnL%-j
并輸入剛剛復制的密碼,但是 卻提示不能通過mysql.sock文件實現連接
[root@cs2 bin]# ./mysqladmin -u root -p password Enter password: mysqladmin: connect to server at 'localhost' failed error: 'Can't connect to local MySQL server through socket '/mysqlsoft/mysql/mysql.sock' (2)' Check that mysqld is running and that the socket: '/mysqlsoft/mysql/mysql.sock' exists!
這里就奇怪了,因為在my.cnf文件中設置的socket文件路徑為/mysqldata/mysql/mysql.sock,但mysql所使用的文件不是啟動服務所生成的。
[root@cs2 ~]# cat /mysqlsoft/mysql/my.cnf [mysqld] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql bind-address=0.0.0.0 user=mysql port=3306 log-error=/mysqldata/mysql/mysql.err pid-file=/mysqldata/mysql/mysqld.pid socket = /mysqldata/mysql/mysql.sock character-set-server=utf8 default-storage-engine=INNODB explicit_defaults_for_timestamp = true [root@cs2 mysql]# ls -lrt total 286896 -rw-r----- 1 mysql mysql 134217728 May 31 14:05 ib_logfile1 -rw-r----- 1 mysql mysql 56 May 31 14:05 auto.cnf drwxr-x--- 2 mysql mysql 8192 May 31 14:05 performance_schema drwxr-x--- 2 mysql mysql 4096 May 31 14:05 mysql drwxr-x--- 2 mysql mysql 8192 May 31 14:05 sys -rw------- 1 mysql mysql 1679 May 31 14:25 ca-key.pem -rw-r--r-- 1 mysql mysql 1107 May 31 14:25 ca.pem -rw------- 1 mysql mysql 1679 May 31 14:25 server-key.pem -rw-r--r-- 1 mysql mysql 1107 May 31 14:25 server-cert.pem -rw------- 1 mysql mysql 1679 May 31 14:25 client-key.pem -rw-r--r-- 1 mysql mysql 1107 May 31 14:25 client-cert.pem -rw------- 1 mysql mysql 1679 May 31 14:25 private_key.pem -rw-r--r-- 1 mysql mysql 451 May 31 14:25 public_key.pem -rw-r----- 1 mysql mysql 291 May 31 18:05 ib_buffer_pool -rw------- 1 mysql mysql 6 May 31 18:08 mysql.sock.lock srwxrwxrwx 1 mysql mysql 0 May 31 18:08 mysql.sock -rw-r----- 1 mysql mysql 6 May 31 18:08 mysqld.pid -rw-r----- 1 mysql mysql 101396 May 31 18:08 mysql.err -rw-r----- 1 mysql mysql 12582912 May 31 18:08 ibtmp1 -rw-r----- 1 mysql mysql 12582912 May 31 18:08 ibdata1 -rw-r----- 1 mysql mysql 134217728 May 31 18:08 ib_logfile0
可以看到在/var/lib/mysql目錄中的mysql.sock是指向/mysqlsoft/mysql/mysql.sock文件的。
[mysql@cs2 ~]$ ls -lrt /var/lib/mysql 總用量 110604 -rw-rw----. 1 27 27 50331648 10月 11 2017 ib_logfile1 drwx------. 2 27 27 4096 10月 11 2017 performance_schema drwx------. 2 27 27 4096 10月 11 2017 mysql -rw-rw----. 1 27 27 56 10月 11 2017 auto.cnf -rw-rw----. 1 27 27 50331648 5月 30 18:32 ib_logfile0 -rw-rw----. 1 27 27 12582912 5月 30 18:32 ibdata1 lrwxrwxrwx 1 root root 27 5月 31 15:44 mysql.sock -> /mysqlsoft/mysql/mysql.sock
如果使用-S選項來指定生成的mysql.sock文件進行登錄是可以成功登錄的
[mysql@cs2 mysql]$ mysql -S /mysqldata/mysql/mysql.sock -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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> quit
修改socket文件路徑為/mysqlsoft/mysql/mysql.sock
[root@cs2 ~]# cat /mysqlsoft/mysql/my.cnf [mysqld] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql bind-address=0.0.0.0 user=mysql port=3306 log-error=/mysqldata/mysql/mysql.err pid-file=/mysqldata/mysql/mysqld.pid socket = /mysqlsoft/mysql/mysql.sock character-set-server=utf8 default-storage-engine=INNODB explicit_defaults_for_timestamp = true
再重啟mysql服務
[root@cs2 ~]# service mysqld stop Shutting down MySQL.. SUCCESS! [root@cs2 ~]# service mysqld start Starting MySQL.. SUCCESS! [mysql@cs2 ~]$ 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.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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>
這就可以登錄了。
12.重置root用戶密碼
[mysql@cs2 ~]$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.22 Copyright (c) 2000, 2018, 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> set password=password("123456"); Query OK, 0 rows affected, 1 warning (0.00 sec)
13.設置允許遠程登錄mysql
如果要遠程訪問數據庫,只需要把擁有全部權限的root賬號對應的記錄的Host字段改為%就可以了
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 host='%' where user='root'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> grant all privileges on *.* to root@'%'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
測試遠程登錄
-bash-4.2$ mysql -h 10.11.13.19 -P 3306 -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.22 MySQL Community Server (GPL) 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> 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> SELECT DISTINCT CONCAT('User: [', user, '''@''', host, '];') AS USER_HOST FROM user; +------------------------------------+ | USER_HOST | +------------------------------------+ | User: [root'@'%]; | | User: [mysql.session'@'localhost]; | | User: [mysql.sys'@'localhost]; | +------------------------------------+ 3 rows in set (0.05 sec)
以上是“Linux 7如何安裝Mysql 5.7”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。