當(dāng)前位置:首頁 > IT技術(shù) > 數(shù)據(jù)庫 > 正文

mysql的三種安裝方法
2021-09-17 16:20:37

MySQL安裝

?

一、yum安裝mariadb

1、配置yum源

[root@localhost ~]# cd /etc/yum.repos.d/

[root@localhost yum.repos.d]# vim mariadb.repo

[mariadb]

name = MariaDB

baseurl = https://mirrors.nju.edu.cn/mariadb/yum/10.2/centos7-amd64

gpgkey=https://mirrors.nju.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB

gpgcheck=1

?

安裝mariadb

[root@localhost ~]# yum -y install mariadb mariadb-server mariadb-devel

?

查看安裝情況

[root@localhost ~]# rpm -qa|grep mariadb

mariadb-libs-5.5.60-1.el7_5.x86_64

mariadb-server-5.5.60-1.el7_5.x86_64

mariadb-5.5.60-1.el7_5.x86_64

mariadb-devel-5.5.60-1.el7_5.x86_64

?

二、源碼部署mysql5.7

?

安裝boost庫

[root@localhost ~]# tar xf boost_1_59_0.tar.gz

[root@localhost ~]# mv boost_1_59_0 /usr/local/boost

安裝mysql5.7

[root@localhost ~]# tar xf mysql-5.7.21.tar.gz

[root@localhost ~]# cd mysql-5.7.21

安裝依賴包

[root@localhost mysql-5.7.21]# yum -y install gcc gcc-c++ ncurses-devel libaio bison git cmake openssl-devel

預(yù)編譯

[root@localhost mysql-5.7.21]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql57 -DMYSQL_UNIX_ADDR=/data/mysql57/mysql.sock -DMYSQL_DATADIR=/data/mysql57 -DSYSCONFDIR=/usr/local/mysql57 -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3307 -DWITH_XTRADB_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_EXTRA_CHARSETS=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BIG_TABLES=1 -DWITH_DEBUG=0 -DENABLE_DTRACE=0 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost

編譯安裝mysql5.7

[root@localhost mysql-5.7.21]# make && make install

[root@localhost mysql-5.7.21]# mkdir -p /data/mysql57

[root@localhost mysql-5.7.21]# useradd -s /sbin/nologin mysql

[root@localhost mysql-5.7.21]# chown -R mysql. /data/mysql57

[root@localhost mysql-5.7.21]# cp support-files/mysql.server /etc/init.d/mysqld57

[root@localhost mysql-5.7.21]# chmod +x /etc/init.d/mysqld57

[root@localhost mysql-5.7.21]# vim /usr/local/mysql57/my.cnf

[mysqld]

basedir=/usr/local/mysql57/

datadir=/data/mysql57/

port=3307

pid-file=/data/mysql57/mysql.pid

socket=/tmp/mysql.sock

[mysqld_safe]

log-error=/data/mysql57/mysql.log

初始化數(shù)據(jù)庫

[root@localhost mysql-5.7.21]# /usr/local/mysql57/bin/mysqld --initialize --user=mysql --datadir=/data/mysql57 --basedir=/usr/local/mysql57

?

[root@localhost mysql-5.7.21]# ls /data/mysql57/

auto.cnf? ib_buffer_pool? ibdata1? ib_logfile0? ib_logfile1? mysql? performance_schema? sys

?

?

啟動mysql服務(wù)

[root@localhost mysql-5.7.21]# /etc/init.d/mysqld57 start

[root@localhost mysql-5.7.21]# vim /usr/local/mysql57/my.cnf

skip-grant-tables

[root@localhost mysql-5.7.21]# /etc/init.d/mysqld57 restart

[root@localhost mysql-5.7.21]# ln -s /tmp/mysql.sock /data/mysql57/mysql.sock

[root@localhost mysql-5.7.21]# /usr/local/mysql57/bin/mysql

mysql> update mysql.user set authentication_string=password('') where user="root";

[root@localhost mysql-5.7.21]# /etc/init.d/mysqld57 restart

免密碼登錄

[root@localhost mysql-5.7.21]# /usr/local/mysql57/bin/mysql

Welcome to the MySQL monitor.? Commands end with ; or g.

Your MySQL connection id is 2

Server version: 5.7.21 Source distribution

?

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>

?

?

三、二進(jìn)制部署部署mysql8.0

1、安裝依賴包

[root@localhost yum.repos.d]# yum -y install gcc gcc-c++ ncurses-devel libaio bison git cmake openssl-devel

2、解壓縮

[root@localhost ~]# tar -xf mysql-8.0.24-el7-x86_64.tar.gz

[root@localhost ~]# cd /usr/local/mysql/

3、移動安裝文件到/usr/local/mysql 下

[root@localhost ~]# mv mysql-8.0.24-el7-x86_64 /usr/local/mysql

[root@localhost ~]# cd /usr/local/mysql/

4、創(chuàng)建mysql用戶和組

[root@localhost mysql]# useradd -s /sbin/nologin mysql

5、創(chuàng)建數(shù)據(jù)目錄

[root@localhost mysql]# mkdir -p /data/mysql

6、變更數(shù)據(jù)目錄為mysql用戶所屬主和所屬組

[root@localhost mysql]# chown -R mysql. /data/mysql

7、將啟動文件移動到啟動目錄下,并給予執(zhí)行權(quán)限

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[root@localhost mysql]# chmod +x /etc/init.d/mysqld

8、編輯mysql配置文件

[root@localhost mysql]# vim /usr/local/mysql/my.cnf

[mysqld]

basedir=/usr/local/mysql/

datadir=/data/mysql/

port=3306

pid-file=/data/mysql/mysql.pid

socket=/data/mysql/mysql.sock

[mysqld_safe]

log-error=/data/mysql/mysql.log

9、初始化

[root@localhost mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql

?

[root@localhost mysql]# ls /data/mysql/

auto.cnf??? client-cert.pem??? #ib_16384_1.dblwr? ib_logfile0?? mysql?????????????? private_key.pem? server-key.pem? undo_002

ca-key.pem? client-key.pem???? ib_buffer_pool???? ib_logfile1?? mysql.ibd?????????? public_key.pem?? sys

ca.pem????? #ib_16384_0.dblwr? ibdata1??????????? #innodb_temp? performance_schema? server-cert.pem? undo_001

10、設(shè)置啟動文件鏈接

[root@localhost mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin

?

?

11、啟動mysql服務(wù),查看服務(wù)的運(yùn)行情況

[root@localhost mysql]# /etc/init.d/mysqld start

Starting MySQL.Logging to '/data/mysql/mysql.log'.

.... SUCCESS!

[root@localhost mysql]# ps -ef |grep mysql

root?????? 4184????? 1? 0 05:02 pts/0??? 00:00:00 /bin/sh /usr/local/mysql//bin/mysqld_safe --datadir=/data/mysql/ --pid-file=/data/mysql/mysql.pid

mysql????? 4349?? 4184 10 05:02 pts/0??? 00:00:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql/ --datadir=/data/mysql --plugin-dir=/usr/local/mysql//lib/plugin --user=mysql --log-error=/data/mysql/mysql.log --pid-file=/data/mysql/mysql.pid --socket=/data/mysql/mysql.sock --port=3306

root?????? 4402?? 1478? 0 05:03 pts/0??? 00:00:00 grep --color=auto mysql

[root@localhost mysql]# netstat -anptul|grep :3306

tcp6?????? 0????? 0 :::33060??????????????? :::*??????????????????? LISTEN????? 4349/mysqld????????

tcp6?????? 0????? 0 :::3306???????????????? :::*??????????????????? LISTEN????? 4349/mysqld

?

連接出現(xiàn)錯誤

[root@localhost mysql]# mysql -uroot -p

Enter password:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

?

[root@localhost ~]# ln -s /data/mysql/mysql.sock /tmp/

[root@localhost ~]# /usr/local/mysql/bin/mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot –p

Enter password:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

修改登錄密碼

[root@localhost ~]# vim /usr/local/mysql/my.cnf

skip-grant-tables

[root@localhost ~]# /etc/init.d/mysqld restart

[root@localhost ~]# /usr/local/mysql/bin/mysql

mysql> select host, user, authentication_string, plugin from mysql.user;

+-----------+------------------+------------------------------------------------------------------------+-----------------------+

| host????? | user???????????? | authentication_string????????????????????????????????????????????????? | plugin??????????????? |

+-----------+------------------+------------------------------------------------------------------------+-----------------------+

| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |

| localhost | mysql.session??? | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |

| localhost | mysql.sys??????? | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |

| localhost | root???????????? | $A$005$

;

?j;D?z47X*YB!jeDFujaez6PRCqAwu2bGrt2rlTKxyhbRiJRR59cbVCGt8 | caching_sha2_password |

+-----------+------------------+------------------------------------------------------------------------+-----------------------+

4 rows in set (0.00 sec)

?

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

?

mysql> alter user 'root'@'localhost'IDENTIFIED BY '123';

Query OK, 0 rows affected (0.01 sec)

?

?

修改配置,重啟服務(wù),重新登錄

[root@localhost ~]# vim /usr/local/mysql/my.cnf

刪除掉skip-grant-tables

[root@localhost ~]# /etc/init.d/mysqld restart

Shutting down MySQL. SUCCESS!

Starting MySQL. SUCCESS!

[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot -p123

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.? Commands end with ; or g.

Your MySQL connection id is 8

Server version: 8.0.24 MySQL Community Server - GPL

?

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

?

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>

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >