Linux上安裝MySQL的幾種常見方式的過程和步驟

weixin_44077638發表於2020-09-25

不同的發行版安裝方式是不同的

原始碼安裝和二進位制安裝是通用的

1.APT安裝方式(如果系統是Ubuntu或者dibian作業系統)

apt install mysql-server mysql-client -y

/etc/mysql/debian.cnf:(隨機密碼的預設賬戶)

mysql_secure_installation(安裝驗證密碼外掛)

2.centos下,注意(可能在安裝系統時已經安裝了MariaDB)

阿里雲映象-OS映象-centos-7(x86_64-Minimal-1908)

yum install MariaDB-server MariaDB-client -y(官方源安裝MariaDB)

systemctl start mariadb.service

systemctl enable mariadb.service(啟動並新增開機自啟)

mysql_secure_installation(修改密碼)ynyy

mysql -uroot -proot

3.rpm安裝方式

(1)線上安裝

在CentOS6之前,yum源預設存在mysql

apt install mysql-server mysql-client -y

在CentOS6之後,yum沒有mysql源,需要自己下載源

清華映象 - mysql - yum/ - mysql-5.6-community-e17-x86_64/ - mysql-community-client-5.7.11-1.e17.i686.rpm

MySQL官網

mkdir download

cd download

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

yum install -y mysql57-community-release-el7-10.noarch.rpm

yum mysql-server  mysql-client

(2)離線包安裝

需要下載離線包:

ls -l mysql-5.7.14-1.el7.x86_64.rpm-bundle.tar(上傳至伺服器)

tar xf mysql-5.7.14-1.el7.x86_64.rpm-bundle.tar(解壓檢視)

yum remove mariadb-libs

systemctl start mysqld (限制mariadb相關軟體包)

rpm -ivh mysql-community-client-5.7.14-1.el7.x86_64.rpm mysql-community-common-5.7.14-1.el7.x86_64.rpm mysql-community-libs-5.7.14- 1.el7.x86_64.rpm mysql-community-server-5.7.14-1.el7.x86_64.rpm(安裝)

systemctl enable mysqld(啟動)

4.通用二進位制方式安裝

mysql官網

tar xf mysql-5.7.14-linux-glibc2.5-x86_64.tar.gz(解壓)

ln -sv mysql-5.7.14-linux-glibc2.5-x86_64 mysql

chown -R mysql.mysql mysql/*(修改許可權)(新增mysql組)

mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql -- datadir=/usr/local/mysql/data(初始化)(記住臨時密碼)

cd /usr/local/mysql

# \cp support-files/my-default.cnf /etc/my.cnf vim /etc/my.cnf(為mysql提供主配置檔案)

datadir = /usr/local/mysql/data

cp support-files/mysql.server /etc/rc.d/init.d/mysqld

chmod +x /etc/rc.d/init.d/mysqld(為mysql提供sysv服務指令碼)

chkconfig --add mysqld --新增為系統服務

chkconfig mysqld on --開機自啟動

systemctl start mysqld(啟動mysql)

export PATH=$PATH:/usr/local/mysql/bin(將mysql的bin目錄加入PATH環境變數,編輯/etc/profile檔案)

5.原始碼安裝

mysql官網

yum install make gcc gcc-c++ cmake bison-devel ncurses-devel -y(編譯C/C++的)

yum install libaio libaio-devel openssl-devel -y

yum install perl-Data-Dumper -y

yum install net-tools -y(安裝開發工具和開發包)

tar xf mysql-boost-5.7.14.tar.gz -C /usr/local/src/(解壓)

groupadd -r -g 306 mysql

useradd -g 306 -r -u 306 mysql(新增使用者和組)

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ (路徑)

-DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc \

-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 \

-DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 \

-DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \(埠)

-DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all \

-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \

--DWITH_BOOST=/usr/local/src/mysql-5.7.14/boost/

make && make install(編譯安裝)

/usr/local/mysql/bin/mysqld --initialize --user=mysql --

datadir=/usr/local/mysql/data --basedir=/usr/local/mysql/(初始化)

datadir = /usr/local/mysql/data(為mysql提供主配置檔案)

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld on(為mysql提供sysv服務指令碼)

vim /etc/profile.d/mysql.sh

export PATH=$PATH:/usr/local/mysql/bin

source mysql.sh(修改PATH環境變數,讓系統可以直接使用mysql的相關命令)

systemctl start mysqld(啟動MySQL)

6.docker安裝

安裝docker(不同版本安裝方式不同)

相關文章