目录
软件下载
[root@localhost ~]# cd /usr/src/ //下载二进制格式的mysql软件包 [root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz --2022-07-31 20:41:52-- (try: 2) https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz Connecting to cdn.mysql.com (cdn.mysql.com)|104.85.245.54|:443... connected. HTTP request sent, awaiting response... 206 Partial Content Length: 674830866 (644M), 656840759 (626M) remaining [application/x-tar-gz] Saving to: 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz' mysql-5.7.38-linux-gl 100%[======================>] 643.57M 1.56MB/s in 8m 4s 2022-07-31 20:49:59 (1.29 MB/s) - 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz' saved [674830866/674830866]
配置用户并解压
配置用户和组并解压二进制程序至/usr/local下
[root@localhost src]# useradd -r -M -s /sbin/nologin mysql //创建用户和组 [root@localhost src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ //解压软件包 [root@localhost src]# ls /usr/local/ bin games lib libexec sbin src etc include lib64 mysql-5.7.38-linux-glibc2.12-x86_64 share [root@localhost src]# cd /usr/local/ [root@localhost local]# ln -s mysql-5.7.38-linux-glibc2.12-x86_64 mysql [root@localhost local]# ll total 0 ...... lrwxrwxrwx. 1 root root 35 Jul 31 20:55 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64 drwxr-xr-x. 9 root root 129 Jul 31 20:54 mysql-5.7.38-linux-glibc2.12-x86_64 ...... [root@localhost local]# chown -R mysql.mysql mysql* //做软链接并且修改属主属组 [root@localhost local]# ll total 0 ..... lrwxrwxrwx. 1 mysql mysql 35 Jul 31 20:55 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64 drwxr-xr-x. 9 mysql mysql 129 Jul 31 20:54 mysql-5.7.38-linux-glibc2.12-x86_64 ...... [root@localhost local]# cd mysql //配置环境变量以及头文件 [root@localhost mysql]# ls LICENSE README bin docs include lib man share support-files [root@localhost mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh [root@localhost mysql]# source /etc/profile.d/mysql.sh [root@localhost mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql [root@localhost mysql]# chown -R mysql.mysql /usr/include/mysql [root@localhost mysql]# ll -d /usr/include/mysql lrwxrwxrwx. 1 mysql mysql 25 Jul 31 20:56 /usr/include/mysql -> /usr/local/mysql/include/ [root@localhost mysql]# vi /etc/ld.so.conf.d/mysql.conf [root@localhost mysql]# cat /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib [root@localhost mysql]# ldconfig [root@localhost mysql]# vi /etc/man_db.conf MANDATORY_MANPATH /usr/local/mysql/man
创建目录
创建各实例数据存放的目录
[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308} [root@localhost ~]# chown -R mysql.mysql /opt/data/ [root@localhost ~]# ll /opt/data/ total 0 drwxr-xr-x. 2 mysql mysql 6 Jul 31 21:04 3306 drwxr-xr-x. 2 mysql mysql 6 Jul 31 21:04 3307 drwxr-xr-x. 2 mysql mysql 6 Jul 31 21:04 3308
初始化
初始化各实例
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3306 2022-07-31T13:07:35.963333Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2022-07-31T13:07:36.196092Z 0 [Warning] InnoDB: New log files created, LSN=45790 2022-07-31T13:07:36.230768Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2022-07-31T13:07:36.287763Z 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: bf598445-10d1-11ed-8264-000c2912fbe9. 2022-07-31T13:07:36.288571Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2022-07-31T13:07:36.407379Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher. 2022-07-31T13:07:36.407402Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher. 2022-07-31T13:07:36.410676Z 0 [Warning] CA certificate ca.pem is self signed. 2022-07-31T13:07:36.568765Z 1 [Note] A temporary password is generated for root@localhost: jgal&uhcu9;F [root@localhost ~]# echo 'jgal&uhcu9;F' > 3306_pass [root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3307 2022-07-31T13:09:14.792281Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2022-07-31T13:09:14.998212Z 0 [Warning] InnoDB: New log files created, LSN=45790 2022-07-31T13:09:15.033719Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2022-07-31T13:09:15.090643Z 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: fa3da3f3-10d1-11ed-b5c7-000c2912fbe9. 2022-07-31T13:09:15.091487Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2022-07-31T13:09:15.232440Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher. 2022-07-31T13:09:15.232454Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher. 2022-07-31T13:09:15.232865Z 0 [Warning] CA certificate ca.pem is self signed. 2022-07-31T13:09:15.262071Z 1 [Note] A temporary password is generated for root@localhost: yQUp;/E=3+<F [root@localhost ~]# echo 'yQUp;/E=3+<F' > 3307_pass [root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3308 2022-07-31T13:10:04.614522Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2022-07-31T13:10:04.833493Z 0 [Warning] InnoDB: New log files created, LSN=45790 2022-07-31T13:10:04.875727Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2022-07-31T13:10:04.930204Z 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: 17f28de8-10d2-11ed-ae4c-000c2912fbe9. 2022-07-31T13:10:04.931005Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2022-07-31T13:10:05.148793Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher. 2022-07-31T13:10:05.148808Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher. 2022-07-31T13:10:05.149125Z 0 [Warning] CA certificate ca.pem is self signed. 2022-07-31T13:10:05.193757Z 1 [Note] A temporary password is generated for root@localhost: Ag*EfRkFB9o0 [root@localhost ~]# echo ' Ag*EfRkFB9o0' > 3308_pass [root@localhost ~]# ls 3306_pass 3307_pass 3308_pass anaconda-ks.cfg
编写配置文件
配置配置文件/etc/my.cnf
[root@localhost ~]# vim /etc/my.cnf [root@localhost ~]# cat /etc/my.cnf [mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld_safe //定义mysqld程序命令存放路径 mysqladmin = /usr/local/mysql/bin/mysqladmin //定义mysqladmin程序命令存放路径 [mysqld3306] datadir = /opt/data/3306 //定义数据目录存放路径 port = 3306 //定义端口 socket = /tmp/mysql3306.sock //定义套接字 pid-file = /opt/data/3306/mysql_3306.pid //定义pid进程文件存放路径 log-error=/var/log/3306.log //定义错误日志存放路径 [mysqld3307] datadir = /opt/data/3307 port = 3307 socket = /tmp/mysql3307.sock pid-file = /opt/data/3307/mysql_3307.pid log-error=/var/log/3307.log [mysqld3308] datadir = /opt/data/3308 port = 3308 socket = /tmp/mysql3308.sock pid-file = /opt/data/3308/mysql_3308.pid log-error=/var/log/3308.log
启动实例
启动会弹出报错信息,需要安装perl
[root@localhost ~]# yum -y install perl
启动各实例
[root@localhost ~]# mysqld_multi start 3306 [root@localhost ~]# mysqld_multi start 3307 [root@localhost ~]# mysqld_multi start 3308 [root@localhost ~]# ss -anlt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 80 *:3306 *:* LISTEN 0 80 *:3307 *:* LISTEN 0 80 *:3308 *:* LISTEN 0 128 [::]:22 [::]:*
这种启动方式启动可以正常查看到端口,但是在进行stop停止的时候,可以看到端口依然是存在的,所以并不推荐这种启动方式,可以写一个systemctl配置文件,以这种方式来实现启动,停止等一系列操作。
systemctl启动
[root@localhost ~]# cp /usr/lib/systemd/system//sshd.service /usr/lib/systemd/system/3306.service [root@localhost ~]# vim /usr/lib/systemd/system/3306.service [root@localhost ~]# cat /usr/lib/systemd/system/3306.service [Unit] Description=3306 server daemon After=network.target sshd-keygen.target [Service] Type=forking ExecStart=/usr/local/mysql/bin/mysqld_multi start 3306 ExecStop=ps -ef | grep 3306 | grep -v grep | awk '{print$2}' | xargs kill -9 ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target [root@localhost ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3307.service [root@localhost ~]# cp /usr/lib/systemd/system/3306.service /usr/lib/systemd/system/3308.service [root@localhost ~]# vim /usr/lib/systemd/system/3307.service [root@localhost ~]# cat /usr/lib/systemd/system/3307.service [Unit] Description=3307 server daemon After=network.target sshd-keygen.target [Service] Type=forking ExecStart=/usr/local/mysql/bin/mysqld_multi start 3307 ExecStop=ps -ef | grep 3307 | grep -v grep | awk '{print$2}' | xargs kill -9 ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target [root@localhost ~]# vim /usr/lib/systemd/system/3308.service [root@localhost ~]# cat /usr/lib/systemd/system/3308.service [Unit] Description=3308 server daemon After=network.target sshd-keygen.target [Service] Type=forking ExecStart=/usr/local/mysql/bin/mysqld_multi start 3308 ExecStop=ps -ef | grep 3308 | grep -v grep | awk '{print$2}' | xargs kill -9 ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target [root@localhost ~]# systemctl daemon-reload [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@localhost ~]# vim /etc/selinux/config SELINUX=disabled [root@localhost ~]# setenforce 0 [root@localhost ~]# ln -s /usr/local/mysql/bin/my_print_defaults /usr/bin//my_print_defaults [root@localhost ~]# systemctl start 3306 [root@localhost ~]# systemctl start 3307 [root@localhost ~]# systemctl start 3308 [root@localhost ~]# ss -anlt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 80 *:3306 *:* LISTEN 0 80 *:3307 *:* LISTEN 0 80 *:3308 *:* LISTEN 0 128 [::]:22 [::]:* [root@localhost ~]# systemctl stop 3306 [root@localhost ~]# systemctl stop 3307 [root@localhost ~]# systemctl stop 3308 [root@localhost ~]# ss -anlt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
初始化密码
[root@localhost ~]# ls 3306_pass 3307_pass 3308_pass anaconda-ks.cfg [root@localhost ~]# cat 3306_pass jgal&uhcu9;F [root@localhost ~]# mysql -uroot -p'jgal&uhcu9;F' -S /tmp/mysql3306.sock 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 2 Server version: 5.7.38 Copyright (c) 2000, 2022, 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> set password = password('123!com'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> quit Bye [root@localhost ~]# [root@localhost ~]# cat 3307_pass yQUp;/E=3+<F [root@localhost ~]# mysql -uroot -p'yQUp;/E=3+<F' -S /tmp/mysql3307.sock -e 'set password = password("wangqing123!");' --connect-expired-password mysql: [Warning] Using a password on the command line interface can be insecure. [root@localhost ~]# cat 3308_pass Ag*EfRkFB9o0 [root@localhost ~]# mysql -uroot -p'Ag*EfRkFB9o0' -S /tmp/mysql3308.sock -e 'set password = password("wangqing123!");' --connect-expired-password mysql: [Warning] Using a password on the command line interface can be insecure.
报错解决
启动实例的时候安装perl会有可能如下报错 [root@localhost ~]# mysqld_multi start 3306 perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LANG = "zh_CN.UTF-8" are supported and installed on your system. perl: warning: Falling back to the standard locale ("C"). 解决办法 echo "export LC_ALL=C" >> ~/bashrc source ~/bashrc