1. 程式人生 > >MYSQL資料的安裝、配置

MYSQL資料的安裝、配置

---恢復內容開始---

linux安裝mysql服務分兩種安裝方法:

  1、原始碼安裝,優點是安裝包比較小,只有十多M,缺點是安裝依賴的庫多,安裝編譯時間長,安裝步驟複雜容易出錯。

  2、使用官方編譯好的二進位制檔案安裝,優點是安裝速度快,安裝步驟簡單,缺點是安裝包很大,376M左右。我這裡官方編譯好的Linux二進位制包安裝mysql。

(記的第一次安裝MYSQL是在學習Hadoop的時候,部署Hive,用到了,再往後就沒有用過MYSQL一直用的Oracle,這一次幾方面原因,再次安裝Mysql,記錄下來過程,最主要的是自己電腦上的內容太多,又不是連續操作,怕忘記埠、密碼等等,順便學習Python操作Mysql時用。)

一、軟體下載

  到mysql官網下載mysql編譯好的二進位制安裝包,在下載頁面Select Platform:選項選擇linux-generic,然後把頁面拉到底部,64位系統下載Linux - Generic (glibc 2.5) (x86, 64-bit)。

  地址:https://dev.mysql.com/downloads/mysql/

  

  軟體包上傳、解壓步驟略......

二、安裝環境檢查(初次安裝可忽略)

  1.檢查是否有rpm包,如果沒有用rpm安裝過mysql,不應該有殘留,如果有,需要刪掉

    檢查語法: rpm -qa|grep -i mysql

    刪除語法: rpm -e <包的名字>

    如果遇到依賴,無法刪除,使用 rpm -e --nodeps <包的名字> 不檢查依賴,直接刪除rpm包。

  2.解除安裝系統自帶的mariadb

    檢查語法: rpm -qa|grep Mariadb

    刪除語法: rpm -e <包的名字>

  3.檢視所有的 mysql目錄 ,並刪除

    find / -name mysql

三、安裝

  1.建立mysql使用者組和mysql使用者

    groupadd  mysql                                         //建立mysql 使用者組

    useradd -g mysql mysql                             //建立一個使用者名稱為mysql的使用者並加入mysql使用者組

  2.通過ssh工具,將MySQL安裝包 mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz 拖拽到 /software目錄下並解壓

    解壓指令tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz

    3.將解壓後的安裝包複製到/opt 目錄下,並重命名mysql

    cp -Rf mysql-8.0.13-linux-glibc2.12-x86_64 /opt/mysql

  4.修改許可權 

    cd /opt/mysql

    chown –R mysql                       //把當前目錄下的檔案及目錄的屬性改為mysql使用者

    chgrp –R mysql                     //把當前目錄下的檔案所屬的元件改為mysql組

  5.建立data目錄,作為資料庫儲存位置

    mkdir /opt/mysql/data

  6.配置my.cnf檔案

    此檔案非常重要,初始化之前一定要把此檔案放到 /etc 目錄下,

    此檔案內容如下(路徑根據自己的實際情況):

[[email protected] mysql]# vim /etc/my.cnf
新增/修改如下:
[client]   port
= 3306   socket = /tmp/mysql.sock [mysqld]   character_set_server=utf8   init_connect='SET NAMES utf8'   basedir=/opt/mysql   datadir=/opt/mysql/data   socket=/tmp/mysql.sock

  7.初始化mysql

    /opt/mysql/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data

[[email protected] opt]# /opt/mysql/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
2018-11-23T05:37:20.419244Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default.
                  Consider not using this option as it' is deprecated and will be removed in a future release.
2018-11-23T05:37:20.435233Z 0 [System] [MY-013169] [Server] /opt/mysql/bin/mysqld (mysqld 8.0.13) initializing of server in progress as process 18027
2018-11-23T05:37:20.456938Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3,
                  but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2018-11-23T05:37:23.561315Z 5 [Note] [MY-010454] [Server] A temporary password is generated for [email protected]: ti_g<4t/ZvcC
2018-11-23T05:37:25.859931Z 0 [System] [MY-013170] [Server] /opt/mysql/bin/mysqld (mysqld 8.0.13) initializing of server has completed
[[email protected] opt]#

    注意:把初始密碼拷貝下來,備份

    chown -R root .  //把當前目錄下的檔案及目錄的屬性改為root 使用者
    chown -R mysql data

 

四、啟動並檢查

  1.啟動mysql,並檢視是否已經啟動成功

    /opt/mysql/bin/mysqld_safe --user=mysql &

[[email protected] opt]# /opt/mysql/bin/mysqld_safe --user=mysql &
[1] 8875
[[email protected] opt]# 2018-11-23T03:42:59.200011Z mysqld_safe Logging to '/var/log/mysqld.log'.
2018-11-23T03:42:59.240320Z mysqld_safe Starting mysqld daemon with databases from /opt/mysql/data

  中間啟動失敗了。錯誤如下:

[[email protected] opt]# 2018-11-23T03:40:45.579575Z mysqld_safe Logging to '/var/log/mysqld.log'.
2018-11-23T03:40:45.616041Z mysqld_safe Starting mysqld daemon with databases from /opt/mysql/data
2018-11-23T03:40:48.237984Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

  原因為:socket的路徑mysql使用者沒有write的許可權。報錯如下:

2018-11-23T03:38:19.746257Z 0 [ERROR] [MY-013129] [Server] A message intended for a client cannot be sent there as no client-session is attached. 
            Therefore, we're sending the information to the error-log instead: MY-000001 - Can't create/write to file '/var/run/mysqld/mysqld.pid'
            
(OS errno 13 - Permission denied) 2018-11-23T03:38:19.746285Z 0 [ERROR] [MY-010092] [Server] Can't start server: can't create PID file: Permission denied

  對該路徑修改所屬合寫入許可權:

[[email protected] mysql]# chown -Rf mysql.mysql  /var/run/mysqld
[[email protected] mysql]# chmod 775  /var/run/mysqld

  2.通過初始密碼登入mysql,並修改密碼

    /opt/mysql/bin/mysqladmin -uroot -p password

    -----這裡有問題一直登入不上,後面解決。

  3.設定開機自啟

cp /opt/mysql/support-files/mysql.server  /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld 
chkconfig  --add mysqld
chkconfig  --list mysqld
[[email protected] opt]# cp /opt/mysql/support-files/mysql.server  /etc/rc.d/init.d/mysql
[[email protected] opt]# chmod +x /etc/rc.d/init.d/mysqld 
[[email protected] opt]# chkconfig  --add mysqld
[[email protected] opt]# chkconfig  --list mysqld
            mysqld          0:off   1:off   2:off   3:off   4:off   5:off   6:off

  執行service mysqld stop關閉命令

[[email protected] opt]# service mysqld stop
2018-11-23T04:14:04.201042Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
Stopping mysqld:                                           [  OK  ]
[1]+  Done                    /opt/mysql/bin/mysqld_safe --user=mysql
[[email protected] opt]# 

  然後在執行service mysqld start啟動命令

[[email protected] opt]# service mysqld start
MySQL Daemon failed to start.
Starting mysqld:                                           [FAILED]

  啟動失敗,日誌如下:

181123 12:59:03 mysqld_safe Starting mysqld daemon with databases from /opt/mysql/data
/usr/libexec/mysqld: Table 'mysql.plugin' doesn't exist
181123 12:59:03 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
181123 12:59:03  InnoDB: Initializing buffer pool, size = 8.0M
181123 12:59:03  InnoDB: Completed initialization of buffer pool
InnoDB: Error: log file ./ib_logfile0 is of different size 0 50331648 bytes
InnoDB: than specified in the .cnf file 0 5242880 bytes!
181123 12:59:03 [ERROR] Plugin 'InnoDB' init function returned error.
181123 12:59:03 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
181123 12:59:03 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
181123 12:59:03 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

  一直不明白這個問題。

  執行以下命令解決問題:mysql_install_db --user=mysql --basedir=/usr/ --ldata=/opt/mysql/data/

[[email protected] bin]# mysql_install_db --user=mysql --basedir=/usr/ --ldata=/opt/mysql/data/
Installing MySQL system tables...
OK
Filling help tables...
OK
 
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
 
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
 
/usr//bin/mysqladmin -u root password 'new-password'
/usr//bin/mysqladmin -u root -h centos04 password 'new-password'
 
Alternatively you can run:
/usr//bin/mysql_secure_installation
 
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
 
See the manual for more instructions.
 
You can start the MySQL daemon with:
cd /usr/ ; /usr//bin/mysqld_safe &
 
You can test the MySQL daemon with mysql-test-run.pl
cd /usr//mysql-test ; perl mysql-test-run.pl
 
Please report any problems with the /usr//scripts/mysqlbug script!
 
[[email protected] bin]#

  然後在執行service mysqld start啟動命令

[[email protected] opt]# service mysqld start
Starting mysqld:                                           [  OK  ]
[[email protected] opt]# 

  檢視啟動程序,ps -ef | grep mysql 

[[email protected] opt]# ps -ef|grep mysql
root      8521  5731  0 11:40 pts/1    00:00:00 tail -f mysqld.log
root     15791     1  0 13:02 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/opt/mysql/data 
                            --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql mysql 15908 15791 0 13:02 pts/0 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/opt/mysql/data --user=mysql --log-error=/var/log/mysqld.log
                            --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock root 16020 21837 0 13:06 pts/0 00:00:00 grep mysql [[email protected] opt]#

  4.解決前面登入修改密碼失敗

    以root使用者登入,mysql -u root

[[email protected] opt]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.71 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> 

 修改root密碼

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 Password=PASSWORD('Root#2018') where USER='root' and host='root' or host='localhost';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2  Changed: 0  Warnings: 0
 
mysql> 

  5.防火牆新增埠

    我這裡是關閉了防火牆的。

  6.將user表的 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 Password=PASSWORD('Root#2018') where USER='root' and host='root' or host='localhost';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2  Changed: 0  Warnings: 0
 
mysql> use mysql
Database changed
mysql> update user set host ='%'where user ='root' and host ='localhost';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> 

  

  加上過程問題處理,這次用時3個小時,時間太長了.......