mysql安裝教程-兩種安裝方式,rpm安裝和tar安裝

目錄

 

1、常用命令總結

2、rpm下載安裝(最快捷方式)

2.1、卸載mariadb

2.2、安裝依賴

2.3、下載rpm文件

2.3.1下載地址

 2.3.2下載整合版Bundle

2.3.3下載四個文件

2.4、執行命令安裝

2.5、安裝目錄如下

2.6、查看mysql運行狀態,並重啓

2.7、重置密碼

3、tar安裝

卸載:mariadb

3.1、下載安裝包

3.2、上傳到服務器

3.3、解壓到當前文件夾/usr/local/

3.4、創建data路徑

3.5、創建mysql用戶組

3.6、授權用戶組

3.7、初始化腳本,5.7中 這個過時了(這裏不採用)

3.8、更改mysql安裝文件夾mysql/的權限

3.9、初始化mysql

     3.9.1執行如果出現錯誤,可能缺少這個依賴:yum install libaio

3.10、修改Mysql配置文件,並將配置文件放到/etc/init.d/目錄下面

    3.10.1將當前配置文件加入到啓動/etc/init.d/mysqld

3.11、加入開啓自啓動,啓動mysql。(這個用不用都行)

3.12、添加my.cnf

3.13、mysql添加到環境變量中

3.14、啓動mysql服務

    14.1 如果不只是在 /usr/local/mysql路徑下面可能會報錯,那麼執行下面操作,軟連接

3.15、登錄

3.16、修改密碼命令:


 

1、常用命令總結

1.rpm卸載: rpm -e --nodeps mysql-community-common-8.0.19-1.el7.x86_64
  rpm查找: rpm -qa | grep mariadb

2.yum卸載:yum remove xx
 

3.服務重啓操作命令:systemctl restart mysqld
         查看狀態: systemctl status mysqld
         centos6.5mysql的服務操作命令:service mysql start/stop

4.關閉防火牆       :systemctl stop firewalld.service
  開機禁止啓動防火牆:systemctl disable firewalld.service

 

2、rpm下載安裝(最快捷方式)

2.1、卸載mariadb

        1、查找命令: rpm -qa | grep mariadb
        2、查找幾個就卸載幾個,下載命令如下: rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
        3、卸載完成之後,vim /etc/my.cnf  應該就沒了。這個時候就代表卸載完成
 

2.2、安裝依賴

具體根據實際情況,缺啥安裝啥,這個也沒細總結過

yum install perl -y
 
 

2.3、下載rpm文件

2.3.1下載地址

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

 

 

 2.3.2下載整合版Bundle

 

2.3.3下載四個文件

下載地址是:

https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.30-1.el7.x86_64.rpm

https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.30-1.el7.x86_64.rpm

https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.30-1.el7.x86_64.rpm

https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.30-1.el7.x86_64.rpm

(選擇對應的版本)按照順序來
              
 
           (mysql-community-common-5.7.30-1.el7.x86_64.rpm)
            (mysql-community-libs-5.7.30-1.el7.x86_64.rpm)
            (mysql-community-server-5.7.30-1.el7.x86_64.rpm)
            (mysql-community-client-5.7.30-1.el7.x86_64.rpm)
 
 
 
 

2.4、執行命令安裝

如果存在衝突或者缺少依賴:
如卸載命令: rpm -e --nodeps mysql-community-common-8.0.19-1.el7.x86_64
如安裝命令:yum  install gcc -y
      
依次安裝即可,要注意順序
 
   sudo rpm -ivh mysql-community-common-5.7.30-1.el7.x86_64.rpm

    sudo rpm -ivh mysql-community-libs-5.7.30-1.el7.x86_64.rpm

    sudo rpm -ivh mysql-community-client-5.7.30-1.el7.x86_64.rpm

    sudo rpm -ivh mysql-community-server-5.7.30-1.el7.x86_64.rpm

 

    到這裏默認就安裝結束,同時已經啓動mysql的服務
 

2.5、安裝目錄如下

數據庫目錄:/var/lib/mysql/
命令配置:/usr/share/mysql  (mysql.server命令及配置文件)
相關命令:/usr/bin   (mysqladmin mysqldump等命令)
啓動腳本:/etc/rc.d/init.d/   (啓動腳本文件mysql的目錄)
系統配置:/etc/my.conf
 
 

2.6、查看mysql運行狀態,並重啓

     systemctl status mysqld
     systemctl restart mysqld
 

2.7、重置密碼

    1:vim /etc/my.cnf
    2:在最後加上: skip-grant-tables
    3:重啓mysql服務: systemctl restart mysqld
    4: mysql -u root-p  不需要輸入密碼直接enter即可
    5:設置root賬號的密碼: 
            設置密碼:update mysql.user set authentication_string=password('123456') where user='root' ;
            刷新:FLUSH PRIVILEGES;
    6:退出mysql:將之前加入的跳過密碼驗證刪掉
                 命令:vim  /etc/my.cnf  
                 把skip-grant-tables去掉
    7: 重新登錄數據庫
            執行命令出現不能操作情況:show databases; 
            修改密碼即可:alter user 'root'@'localhost' identified by 'Root123456xxx';
    8:外部不能連接問題
            use mysql  ;
            GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Root123456xxx' WITH GRANT OPTION;
            flush privileges;刷新
 9 : 安裝完成了
 
 
 
 
 

3、tar安裝

 

卸載:mariadb

        1、 rpm -qa | grep mariadb
        2、rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
        3、卸載完成之後,vim /etc/my.cnf  應該就沒了
 

3.1、下載安裝包

地址:  https://dev.mysql.com/downloads/mysql/
 
    選擇:(mysql-5.7.29-el7-x86_64.tar.gz)

3.2、上傳到服務器

位置是:/usr/local/  目錄下
 

3.3、解壓到當前文件夾/usr/local/

        如果是tar文件:tar -xvf mysql-5.7.29-linux-glibc2.12-x86_64.tar
        如果是tar.gz解壓 :tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
        改名字:mv /usr/local/mysql-5.7.29-linux-glibc2.12-x86_64 mysql

3.4、創建data路徑

            mkdir /usr/local/mysql/data

3.5、創建mysql用戶組

        groupadd mysql
        useradd -r -g mysql mysql
 

3.6、授權用戶組

     
     chown -R mysql:mysql /usr/local/mysql
     chown -R mysql:mysql /usr/local/mysql/data
 

3.7、初始化腳本,5.7中 這個過時了(這裏不採用)

/usr/local/mysql/bin/mysql_install_db --user=mysql
[root@ip-100 bin]# /usr/local/mysql/bin/mysql_install_db --user=mysql
2020-01-16 01:23:59 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2020-01-16 01:23:59 [ERROR]   The data directory needs to be specified.

 

 

3.8、更改mysql安裝文件夾mysql/的權限

   chmod -R 755 /usr/local/mysql/
 

3.9、初始化mysql

     3.9.1執行如果出現錯誤,可能缺少這個依賴:yum install libaio

/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
 
下面是輸出內容,注意最後一行的初始化密碼,
2020-01-15T17:31:36.283878Z 1 [Note] A temporary password is generated for root@localhost: spPwujtd1z%X
[root@ip-100 mysql]# /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
2020-01-15T17:31:33.818233Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-01-15T17:31:34.417755Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-01-15T17:31:34.510250Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-01-15T17:31:34.759518Z 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: e0788195-37bc-11ea-a2a6-000c29cf0c82.
2020-01-15T17:31:34.765729Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-01-15T17:31:35.831655Z 0 [Warning] CA certificate ca.pem is self signed.

2020-01-15T17:31:36.283878Z 1 [Note] A temporary password is generated for root@localhost: spPwujtd1z%X
 
 

3.10、修改Mysql配置文件,並將配置文件放到/etc/init.d/目錄下面

vim /usr/local/mysql/support-files/mysql.server
 
# Set some defaults
mysqld_pid_file_path=
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
 

    3.10.1將當前配置文件加入到啓動/etc/init.d/mysqld

(ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 也可以)
         cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
         chmod 755 /etc/init.d/mysqld
 

3.11、加入開啓自啓動,啓動mysql。(這個用不用都行)

systemctl enable mysqld
 
 
 

3.12、添加my.cnf

        vi /etc/my.cnf
原先內容:(可能不存在原先的內容)
 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

 

 
覆蓋的內容是:(如果沒有卸載mariadb,加上這句 : !includedir /etc/my.cnf.d
[root@ip-100 mysql]# cat  /etc/my.cnf
 
[client]
no-beep
socket =/usr/local/mysql/mysql.sock
# pipe
# socket=0.0
port=3306
[mysql]
default-character-set=utf8
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
pid-file=/usr/local/mysql/mysqld.pid
#skip-grant-tables
skip-name-resolve
socket = /usr/local/mysql/mysql.sock
character-set-server=utf8
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true
# Server Id.
server-id=1
max_connections=2000
query_cache_size=0
table_open_cache=2000
tmp_table_size=246M
thread_cache_size=300
#限定用於每個數據庫線程的棧大小。默認設置足以滿足大多數應用
thread_stack = 192k
key_buffer_size=512M
read_buffer_size=4M
read_rnd_buffer_size=32M
innodb_data_home_dir = /usr/local/mysql/data
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=16M
innodb_buffer_pool_size=256M
innodb_log_file_size=128M
innodb_thread_concurrency=128
innodb_autoextend_increment=1000
innodb_buffer_pool_instances=8
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_open_files=300
innodb_stats_on_metadata=0
innodb_file_per_table=1
innodb_checksum_algorithm=0
back_log=80
flush_time=0
join_buffer_size=128M
max_allowed_packet=1024M
max_connect_errors=2000
open_files_limit=4161
query_cache_type=0
sort_buffer_size=32M
table_definition_cache=1400
binlog_row_event_max_size=8K
sync_master_info=10000
sync_relay_log=10000
sync_relay_log_info=10000
#批量插入數據緩存大小,可以有效提高插入效率,默認爲8M
bulk_insert_buffer_size = 64M
interactive_timeout = 120
wait_timeout = 120
log-bin-trust-function-creators=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
 
# include all files from the config directory
!includedir /etc/my.cnf.d
 

3.13、mysql添加到環境變量中

vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin/
source ~/.bash_profile
 

3.14、啓動mysql服務

            /etc/init.d/mysqld start
            #或者:     systemctl restart mysqld
 

    14.1 如果不只是在 /usr/local/mysql路徑下面可能會報錯,那麼執行下面操作,軟連接

            執行:ln -s /sofware/mysql/bin/mysqld /usr/local/mysql/mysqld
 
下面是網上的報錯內容:
Starting MySQL.Logging to '/data/mysql/SZY.err'.
2018-07-02T10:09:03.779928Z mysqld_safe The file /usr/local/mysql/bin/mysqld
does not exist or is not executable. Please cd to the mysql installation
directory and restart this script from there as follows:
./bin/mysqld_safe&
See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information
ERROR! The server quit without updating PID file (/software/mysql/mysqld.pid).
 
  因爲新版本的mysql安全啓動安裝包只認/usr/local/mysql這個路徑。解決辦法:
  方法1、建立軟連接
        cd /usr/local/mysql
        ln -s /sofware/mysql/bin/myslqd /usr/local/mysql/mysqld
  方法2、修改mysqld_safe文件(有強迫症的同學建議這種,我用的這種)
        vim /software/mysql/bin/mysqld_safe
        #將所有的/usr/local/mysql改爲/software/mysql
      保存退出。(可以將這個文件拷出來再修改然後替換)
 

3.15、登錄

         15.1: /usr/local/mysql/bin/mysql -u root –p
        輸入的密碼是剛纔初始化的時候生成的密碼
 
        15.2:修改mysql的登錄密碼,在mysql的控制檯直接執行即可
        mysql> show databases;
        ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
 
        set password=password('root');
        grant all privileges on *.* to root@'%' identified by 'root';
        flush privileges;
 

3.16、修改密碼命令:

        alter user 'root'@'localhost' identified by 'root.?';
        flush privileges;
 
到此安裝完成。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章