1、在阿里云服務(wù)器搭建openv-p-n(有條件的同學(xué)再做)
2、通過(guò)編譯、二進(jìn)制安裝MySQL5.7
編譯安裝
1、源碼包下載,官網(wǎng)下載地址:https://downloads.mysql.com/archives/community/,點(diǎn)擊下載,還有boost_1_59_0.tar.gz這個(gè)源碼包也要提前下載好,然后將源碼包上傳到Linux系統(tǒng)
[root@centos7 ~]#ll
total 131916
-rw-r--r-- 1 root root 51363998 Jan 27 21:58 mysql-boost-5.7.30.tar.gz
-rw-r--r-- 1 root root 83709983 Jan 27 21:58 boost_1_59_0.tar.gz
2、安裝相關(guān)依賴包
[root@centos7 ~]#yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel perl-Data-Dumper
3、創(chuàng)建用戶和數(shù)據(jù)目錄
[root@centos7 ~]#useradd -r -s /sbin/nologin -d /data/mysql mysql
[root@centos7 ~]#id mysql
uid=995(mysql) gid=992(mysql) groups=992(mysql)
4、創(chuàng)建數(shù)據(jù)庫(kù)目錄,修改權(quán)限
[root@centos7 ~]#mkdir /data/mysql -pv
mkdir: created directory ‘/data/mysql’
[root@centos7 ~]#chown mysql.mysql /data/mysql/
[root@centos7 ~]#ll -d /data/mysql/
drwxr-xr-x 2 mysql mysql 6 Jan 27 20:42 /data/mysql/
5、解壓縮源碼包
[root@centos7 ~]#tar xvf mysql-boost-5.7.30.tar.gz -C /usr/local/src
[root@centos7 ~]#tar xvf boost_1_59_0.tar.gz -C /usr/local/src
6、源碼編譯安裝 MySQL
[root@centos7 ~]#cd /usr/local/src/mysql-5.7.30/
[root@centos7 mysql-5.7.30]#pwd
/usr/local/src/mysql-5.7.30
[root@centos7 mysql-5.7.30]#cmake . \
> -DCMAKE_INSTALL_PREFIX=/apps/mysql \
> -DMYSQL_DATADIR=/data/mysql/ \
> -DSYSCONFDIR=/etc/ \
> -DMYSQL_USER=mysql \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
> -DWITH_DEBUG=0 \
> -DWITH_READLINE=1 \
> -DWITH_SSL=system \
> -DWITH_ZLIB=system \
> -DWITH_LIBWRAP=0 \
> -DENABLED_LOCAL_INFILE=1 \
> -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DDOWNLOAD_BOOST=1 \
> -DWITH_BOOST=/usr/local/src/boost_1_59_0
[root@centos7 mysql-5.7.30]#make && make install
提示:如果出錯(cuò),執(zhí)行rm -f CMakeCache.txt
7、準(zhǔn)備環(huán)境變量
[root@centos7 mysql-5.7.30]#echo 'PATH=/apps/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos7 mysql-5.7.30]#. /etc/profile.d/mysql.sh
8、修改配置文件
[root@centos7 mysql-5.7.30]#cd /apps/mysql/
[root@centos7 mysql]#pwd
/apps/mysql
[root@centos7 mysql]#vim /etc/my.cnf
[mysqld]
explicit_defaults_for_timestamp=true
datadir=/data/mysql/
basedir=/apps/mysql/bin/mysql/
port=3306
pid-file=/data/mysql/mysql.pid
socket=/data/mysql/mysql.socket
symbolic-links=0
character_set_server=utf8
user=mysql
[mysqld_safe]
log-error=/data/mysql/mysql.log
[client]
port=3306
socket=/data/mysql/mysql.socket
default-character-set=utf8
9、初始化數(shù)據(jù)庫(kù)
[root@centos7 mysql]#bin/mysqld --initialize-insecure --datadir=/data/mysql/ --user=mysql
10、準(zhǔn)備啟動(dòng)腳本,并啟動(dòng)服務(wù)
[root@centos7 mysql]#cp /apps/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@centos7 mysql]#chkconfig --add mysqld
[root@centos7 mysql]#systemctl start mysqld
[root@centos7 mysql]#ss -ntl
State? ? ? Recv-Q Send-Q? ? ? ? ? ? ? Local Address:Port? ? ? ? ? ? ? ? ? ? ? ? ? ? Peer Address:Port
LISTEN? ? 0? ? ? 128? ? ? ? ? ? ? ? ? ? ? ? ? ? *:22? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *:*
LISTEN? ? 0? ? ? 100? ? ? ? ? ? ? ? ? ? 127.0.0.1:25? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *:*
LISTEN? ? 0? ? ? 128? ? ? ? ? ? ? ? ? ? ? ? ? [::]:22? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [::]:*
LISTEN? ? 0? ? ? 100? ? ? ? ? ? ? ? ? ? ? ? [::1]:25? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [::]:*
LISTEN? ? 0? ? ? 80? ? ? ? ? ? ? ? ? ? ? ? ? [::]:3306? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [::]:*
11、修改密碼,設(shè)置的密碼一定要記好(大寫(xiě)字母、小寫(xiě)字母、數(shù)字加標(biāo)點(diǎn)符號(hào),密碼要定期更換)
[root@centos7 mysql]# mysql
Welcome to the MySQL monitor.? Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 Source distribution
Copyright (c) 2000, 2020, 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> status
--------------
mysql? Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using? EditLine wrapper
Connection id: 2
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.30 Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db? ? characterset: utf8
Client characterset: utf8
Conn.? characterset: utf8
UNIX socket: /data/mysql/mysql.socket
Uptime: 15 min 17 sec
Threads: 1? Questions: 5? Slow queries: 0? Opens: 105? Flush tables: 1? Open tables: 98? Queries per second avg: 0.005
--------------
mysql> alter user root@'localhost' identified by 'MySQL@2022.';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
二進(jìn)制安裝
1、安裝相關(guān)包
yum -y install libaio numactl-libs
2、創(chuàng)建用戶和組
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
3、準(zhǔn)備程序文件
wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linuxglibc2.12-x86_64.tar.gz
tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local/
ln -s mysql-5.7.31-linux-glibc2.12-x86_64/ mysql
chown -R root.root /usr/local/mysql/
4、準(zhǔn)備環(huán)境變量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
5、準(zhǔn)備配置文件
cp /etc/my.cnf{,.bak}
vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
6、初始化數(shù)據(jù)庫(kù)文件并提取root密碼,生成 root 空密碼
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
7、準(zhǔn)備服務(wù)腳本和啟動(dòng)
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
8、修改口令
#修改前面生成的空密碼為指定密碼(密碼要定期修改)
mysqladmin -uroot password MySQL@2022.
9、測(cè)試登錄
mysql -uroot -pMySQL@2022.
3、二進(jìn)制安裝mariadb10.4
一、下載
[root@localhost src]#wget http://mirrors.dotsrc.org/mariadb//mariadb-10.4.13/bintar-linux-x86_64/mariadb-10.4.13-linux-x86_64.tar.gz
[root@localhost src]# tar -zxvf mariadb-10.4.13-linux-x86_64.tar.gz
[root@localhost src]# mv mariadb-10.4.13-linux-x86_64 /usr/local/mariadb
[root@localhost mariadb]# ls
bin? ? ? CREDITS? docs? ? ? ? ? ? ? include? ? ? ? lib? mysql-test? README-wsrep? share? ? ? support-files
COPYING? data? ? EXCEPTIONS-CLIENT? INSTALL-BINARY? man? README.md? scripts? ? ? sql-bench? THIRDPARTY
二、配置
新建組及用戶
[root@localhost mariadb]# groupadd mysql
[root@localhost mariadb]# useradd -s /sbin/nologin -r -g mysql mysql
配置服務(wù)
[root@localhost mariadb]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mariadb]# chmod +x /etc/init.d/mysqld
新建數(shù)據(jù)目錄與權(quán)限
[root@localhost mariadb]# mkdir -p /data/mariadb/
[root@localhost mariadb]# chown -R mysql. /data/mariadb
[root@localhost mariadb]# chown -R mysql:mysql /usr/local/mariadb/
配置my.cnf
[root@localhost mariadb]# vim my.cnf
[mysqld]
basedir=/usr/local/mariadb/
datadir=/data/mariadb/
port=3306
pid-file=/data/mariadb/mysql.pid
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/data/mariadb/mysql.log
[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8
三、數(shù)據(jù)庫(kù)初始化
/usr/local/mariadb/scripts/mysql_install_db --defaults-file=/usr/local/mariadb/my.cnf --user=mysql --datadir=/data/mariadb/ --basedir=/usr/local/mariadb/
[root@localhost ~]# /usr/local/mariadb/scripts/mysql_install_db --defaults-file=/usr/local/mariadb/my.cnf --user=mysql --datadir=/data/mariadb/ --basedir=/usr/local/mariadb/
Installing MariaDB/MySQL system tables in '/data/mariadb/' ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
Two all-privilege accounts were created.
One is root@localhost, it has no password, but you need to
be system 'root' user to connect. Use, for example, sudo mysql
The second is mysql@localhost, it has no password either, but
you need to be the system 'mysql' user to connect.
After connecting you can set the password, if you would need to be
able to connect as any of these users with a password and without sudo
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '/usr/local/mariadb/' ; /usr/local/mariadb//bin/mysqld_safe --datadir='/data/mariadb/'
You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/local/mariadb//mysql-test' ; perl mysql-test-run.pl
Please report any problems at http://mariadb.org/jira
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
[root@localhost ~]# echo $?
0
四、base目錄權(quán)限切回root
[root@localhost mariadb]# chown -R root:root /usr/local/mariadb/
五、配置環(huán)境變量
配置環(huán)境變量
[root@localhost ~]#echo "PATH=/usr/local/mariadb/bin:$PATH" >/etc/profile.d/mariadb.sh
[root@localhost ~]#chmod +x /etc/profile.d/mariadb.sh
[root@localhost ~]#source /etc/profile.d/mariadb.sh
六、啟動(dòng)與登錄
[root@localhost ~]# /etc/init.d/mysqld start
Starting mysqld (via systemctl):? ? ?
[root@localhost ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
報(bào)錯(cuò),
[root@localhost ~]# ps -ef|grep mysql
root? ? ? 4386? 3375? 0 13:54 pts/0? ? 00:00:00 grep --color=auto mysql
解決方法:
1、rm -rf /data/mariadb/* 數(shù)據(jù)目錄
[root@localhost ~]# rm -rf /data/mariadb/*
2、重新初始化 ,這里加上–defaults-file=/usr/local/mariadb/my.cnf
[root@localhost ~]# /usr/local/mariadb/scripts/mysql_install_db --defaults-file=/usr/local/mariadb/my.cnf --user=mysql --datadir=/data/mariadb/ --basedir=/usr/local/mariadb/? ? 內(nèi)容省略
3、換安全模式啟動(dòng),登錄改root密碼
mysqld_safe --defaults-file=/usr/local/mariadb/my.cnf &
[root@localhost ~]# mysqld_safe --defaults-file=/usr/local/mariadb/my.cnf &
[1] 4692
[root@localhost ~]# 200605 14:00:41 mysqld_safe Logging to '/data/mariadb/mysql.log'.
200605 14:00:41 mysqld_safe Starting mysqld daemon with databases from /data/mariadb/
[root@localhost ~]# mysql
Welcome to the MariaDB monitor.? Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.13-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database? ? ? ? ? |
+--------------------+
| information_schema |
| mysql? ? ? ? ? ? ? |
| performance_schema |
| test? ? ? ? ? ? ? |
+--------------------+
4 rows in set (0.002 sec)
MariaDB [(none)]> use mysql
MariaDB [mysql]> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.002 sec)
MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)
4、配置安全模式
[root@localhost ~]# mysql_secure_installation --basedir=/usr/local/mariadb/
print: /usr/local/mariadb//bin/my_print_defaults
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
? ? ? SERVERS IN PRODUCTION USE!? PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
You already have your root account protected, so you can safely answer 'n'.
Switch to unix_socket authentication [Y/n] y
Enabled successfully!
Reloading privilege tables..
... Success!
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.? This is intended only for testing, and to make the installation
go a bit smoother.? You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'.? This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access.? This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done!? If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
5、重啟服務(wù),再mysql登錄
[root@localhost ~]# /etc/init.d/mysqld stop
[root@localhost ~]# /etc/init.d/mysqld start
Starting mysqld (via systemctl):? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]
[root@localhost ~]# ps -ef|grep mysql
root? ? ? 7104? ? 1? 0 14:27 ?? ? ? ? 00:00:00 /bin/sh /usr/local/mariadb//bin/mysqld_safe --datadir=/data/mariadb/ --pid-file=/data/mariadb/mysql.pid
mysql? ? 7219? 7104? 4 14:27 ?? ? ? ? 00:00:00 /usr/local/mariadb/bin/mysqld --basedir=/usr/local/mariadb/ --datadir=/data/mariadb/ --plugin-dir=/usr/local/mariadb//lib/plugin --user=mysql --log-error=/data/mariadb/mysql.log --pid-file=/data/mariadb/mysql.pid --socket=/tmp/mysql.sock --port=3306
root? ? ? 7257? 3375? 0 14:27 pts/0? ? 00:00:00 grep --color=auto mysql
[root@localhost ~]#
[root@localhost ~]# mysql? 這里可以正常登錄了
Welcome to the MariaDB monitor.? Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.4.13-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database? ? ? ? ? |
+--------------------+
| information_schema |
| mysql? ? ? ? ? ? ? |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)