I will choose to install MySQL from the source code. I must have a certain understanding of MySQL and other installation methods. I won’t go into too much surrounding information here, just start.
Compiling MySQL consumes more memory. If the machine's memory is small, an out-of-memory exception may occur during compilation. If the swap partition is not set, you can set the swap partition to solve it, otherwise you can only expand the memory:
[ root@txy-server ~]# dd if=/dev/zero of=/swapfile bs=1k count=2048000[root@txy-server ~]# mkswap /swapfile
[ root@txy-server ~]# swapon /swapfile
**1.1、**Use the yum command to install the packages and tools that the compilation depends on:
[ root@txy-server ~]# yum install -y cmake3 git gcc-c++ ncurses-devel perl-Data-Dumper boost boost-doc boost-devel bzip2 openssl-devel libtirpc-devel.x86_64
Since the gcc version requirement for MySQL is 5.3 or higher, we need to upgrade the gcc version first, because the latest version of yum is only 4.8.5.
There are two main ways to upgrade the gcc version, one is to download the source package to compile and install, the other is to use yum to install the devtoolset package, the current gcc version in the devtoolset package is 5.3.1.
The source code installation method is more flexible and you can choose any version, but it is very time-consuming. Both methods will be introduced here, you can choose according to the situation.
**1.1.1、**Use yum to install the devtoolset package, the command is as follows:
[ root@txy-server ~]# yum install -y centos-release-scl scl-utils-build
[ root@txy-server ~]# yum install -y devtoolset-4-gcc.x86_64 devtoolset-4-gcc-c++.x86_64 devtoolset-4-gcc-gdb-plugin.x86_64
**1.1.2、**Create a software link and overwrite the gcc-related commands under /usr/bin
, because when compiling MySQL, it will find gcc-related commands in the /usr/bin
directory by default:
[ root@txy-server ~]# ln -sf /opt/rh/devtoolset-4/root/usr/bin/* /usr/bin/
**1.1.3、**Finally, verify that the version of commands such as gcc, cc, c++, etc. is 5.3.1:
[ root@txy-server ~]# gcc -v
...
gcc version 5.3.120160406(Red Hat 5.3.1-6)(GCC)[root@txy-server ~]# cc -v
...
gcc version 5.3.120160406(Red Hat 5.3.1-6)(GCC)[root@txy-server ~]# c++-v
...
gcc version 5.3.120160406(Red Hat 5.3.1-6)(GCC)
**1.2.1、**The following is the source installation method, first download the GCC source installation package and unzip it:
[ root@txy-server ~]# cd /usr/local/src
[ root@txy-server /usr/local/src]# wget http://ftp.gnu.org/gnu/gcc/gcc-9.1.0/gcc-9.1.0.tar.gz
[ root@txy-server /usr/local/src]# tar -xzvf gcc-9.1.0.tar.gz
**1.2.2、**Enter the decompressed directory and run the download_prerequisites
script, which will automatically download the dependent files and libraries needed for compilation:
[ root@txy-server /usr/local/src]# cd gcc-9.1.0[root@txy-server /usr/local/src/gcc-9.1.0]# ./contrib/download_prerequisites
**1.2.3、**Create an output directory and place all intermediate files in this directory:
[ root@txy-server /usr/local/src/gcc-9.1.0]# mkdir gcc-build-9.1.0
**1.2.4、**Enter the newly created directory and complete the compilation configuration:
[ root@txy-server /usr/local/src/gcc-9.1.0]# cd gcc-build-9.1.0[root@txy-server /usr/local/src/gcc-9.1.0/gcc-build-9.1.0]# ../configure -enable-checking=release -enable-languages=c,c++-disable-multilib
Parameter Description:
--Enable-languages
: which languages are supported by gcc --Disable-multilib
: Do not generate a cross-compiler compiled into executable code for other platforms --Disable-checking
: The generated compiler does not do additional checks during the compilation process, and you can also use –enable-checking=xxx
to add some checks**1.2.5、**Then you can compile and install:
[ root@txy-server /usr/local/src/gcc-9.1.0/gcc-build-9.1.0]# make && make install
**1.2.6、**Create a software link and overwrite the gcc-related commands under /usr/bin
, because when compiling MySQL, it will find gcc-related commands in the /usr/bin
directory by default:
[ root@txy-server ~]# ln -sf /usr/local/bin/* /usr/bin/
**1.2.7、**Finally, verify whether the gcc version is 9.1.0, the following means the installation is successful:
[ root@txy-server ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with:../configure -enable-checking=release -enable-languages=c,c++-disable-multilib
Thread model: posix
gcc version 9.1.0(GCC)[root@txy-server ~]#
**2.1、**Enter the download address of MySQL official website:
**2.2、**Copy the download link of the source package:
**2.3、**Download and unzip the source package on Linux:
[ root@txy-server ~]# cd /usr/local/src
[ root@txy-server /usr/local/src]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-boost-8.0.18.tar.gz[root@txy-server /usr/local/src]# tar -zxvf mysql-boost-8.0.18.tar.gz
**2.4、**Enter the decompressed directory and follow the steps below to complete the compilation and installation:
# Create a data file storage directory
[ root@txy-server /usr/local/src]# mkdir -p /data/mysql
[ root@txy-server /usr/local/src]# cd mysql-8.0.18/
# Create a new directory to store the intermediate files generated by the compilation. Because it is not allowed to compile in the source directory
[ root@txy-server /usr/local/src/mysql-8.0.18]# mkdir builder
[ root@txy-server /usr/local/src/mysql-8.0.18]# cd builder/[root@txy-server /usr/local/src/mysql-8.0.18/builder]# cmake3 ../-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1-DWITH_INNOBASE_STORAGE_ENGINE=1-DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_TCP_PORT=3306-DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_DEBUG=0-DMYSQL_MAINTAINER_MODE=0-DWITH_SYSTEMD=1-DDOWNLOAD_BOOST=1-DWITH_BOOST=../boost
[ root@txy-server /usr/local/src/mysql-8.0.18/builder]# make && make install
Description of the parameters used by cmake3
command:
- DCMAKE_INSTALL_PREFIX=/usr/local/mysql
: the root directory of MySQL installation- DMYSQL_DATADIR=/data/mysql
: the directory where the data files are stored- DSYSCONFDIR=/etc
: the directory where the MySQL configuration file is located- DMYSQL_USER=mysql
: the user name of the MySQL service- DWITH_MYISAM_STORAGE_ENGINE=1
: Install MyISAM engine- DWITH_INNOBASE_STORAGE_ENGINE=1
: install InnoDB engine- DWITH_ARCHIVE_STORAGE_ENGINE=1
: Install Archive engine- DWITH_MEMORY_STORAGE_ENGINE=1
: Install Memory Engine- DWITH_FEDERATED_STORAGE_ENGINE=1
: Install Federated engine- DWITH_PARTITION_STORAGE_ENGINE=1
: Install Partition Engine- DWITH_READLINE=1
: MySQL readline library- DMYSQL_UNIX_ADDR=/tmp/mysql.sock
: the path of the sock file- DMYSQL_TCP_PORT=3306
: MySQL listening port- DENABLED_LOCAL_INFILE=1
: enable loading local data- DENABLE_DOWNLOADS=1
: Allow independent download of related files during compilation- DEXTRA_CHARSETS=all
: Make MySQL support all extended characters- DDEFAULT_CHARSET=utf8mb4
: Set the default character set to utf8mb4- DDEFAULT_COLLATION=utf8mb4_general_ci
: set default character collation- DWITH_DEBUG=0
: disable debug mode- DMYSQL_MAINTAINER_MODE=0
: Whether to enable the specific development environment of mysql maintainer- DDOWNLOAD_BOOST=1
: allow online update of boost library- DWITH_BOOST=../boost
: Specify the boost installation pathFor the parameters supported by cmake3
, you can check the official website document:
https://dev.mysql.com/doc/refman/8.0/en/source-configuration-options.html
**2.5、**After compiling and installing, create a mysql user and change the owner of the corresponding directory:
[ root@txy-server ~]# groupadd mysql
[ root@txy-server ~]# useradd -M -g mysql -s /sbin/nologin mysql
[ root@txy-server ~]# chown -R mysql:mysql /usr/local/mysql/[root@txy-server ~]# chown -R mysql:mysql /data/mysql/
**2.6、**Edit the configuration file:
[ root@txy-server ~]# vim /etc/my.cnf
[ mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
[ mysqld_safe]
log-error=/var/log/mysqld/mysqld.log
pid-file=/var/run/mysqld/mysql.pid
**2.7、**Create the directory where the log file is stored and the directory where the pid file is stored, and grant the permissions to the mysql user:
[ root@txy-server ~]# mkdir -p /var/log/mysqld /var/run/mysqld
[ root@txy-server ~]# chown -R mysql:mysql /var/log/mysqld
[ root@txy-server ~]# chown -R mysql:mysql /var/run/mysqld
**2.8、**Configure environment variables to facilitate the use of MySQL commands:
[ root@txy-server ~]# vim /etc/profile
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
[ root@txy-server ~]# source /etc/profile
[ root@txy-server ~]# mysql --version #Verify that the configuration is successful
mysql Ver 8.0.18for Linux on x86_64(Source distribution)[root@txy-server ~]#
**2.9、**Execute the following command to initialize the database:
[ root@txy-server ~]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql/--datadir=/data/mysql
After the initialization is successful, the default password of the root account will be generated, as shown in the following figure:
Copy and save the password, because you will need to log in to MySQL to modify the password later
**2.10、**Copy the startup file generated by MySQL to the /usr/lib/systemd/system/
directory:
[ root@txy-server ~]# cp /usr/local/src/mysql-8.0.18/builder/scripts/mysqld.service /usr/lib/systemd/system/[root@txy-server ~]# chown 775/usr/lib/systemd/system/mysqld.service
**2.11、**Use the systemctl
command to start the MySQL service:
[ root@txy-server ~]# systemctl start mysqld
**2.12、**Check whether port 3306 has been monitored normally:
[ root@txy-server ~]# netstat -lntp |grep 3306
tcp6 00:::33060:::* LISTEN 27363/mysqld
tcp6 00:::3306:::* LISTEN 27363/mysqld
[ root@txy-server ~]#
**2.13、**Use the default password to log in to MySQL, reset the password and open remote login:
[ root@txy-server ~]# mysql -uroot -pmXyfy/g8\)aus
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'; #reset Password
mysql> use mysql;
mysql> update user set host ='%' where user='root'; #Open remote login
mysql> FLUSH PRIVILEGES; #Refresh modification
**2.14、**Use a visualization tool to connect remotely and test whether you can access MySQL normally:
end
Recommended Posts