centos7 源码安装mysql5.7.22

删除系统中的mariadb

[root@MySql ~]# find / -name mariadb*
/etc/ld.so.conf.d/mariadb-x86_64.conf
/usr/share/doc/mariadb-libs-5.5.56
[root@MySql ~]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
[root@MySql ~]# find / -name mariadb*

安装依赖

yum -y install make gcc-c++ cmake bison-devel ncurses-devel

下载源码包并解压,这里下载的是带boost的包,MySQL5.7.5之后需要boost库支持,所以这里直接下载带boost的源码包

[root@MySql ~]# wget -c https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.22.tar.gz
[root@MySql ~]# tar zxvf mysql-boost-5.7.22.tar.gz
[root@MySql ~]# cd mysql-5.7.22/

编译,生成makefile

cmake  \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \           #[MySQL安装的根目录]
-DMYSQL_DATADIR=/data/mysql \                       #[MySQL数据库文件存放目录]
-DSYSCONFDIR=/etc \                                 #[MySQL配置文件所在目录]
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \     #[MySQL的sock文件目录]
-DEXTRA_CHARSETS=all \                              #[使MySQL支持所有的扩展字符]
-DDEFAULT_CHARSET=utf8  \                           #[设置默认字符集为utf8]
-DDEFAULT_COLLATION=utf8_general_ci  \              #[设置默认字符校对]
-DWITH_BOOST=boost \                                #[指定boost安装路径]
-DWITH_MYISAM_STORAGE_ENGINE=1 \                    
-DWITH_INNOBASE_STORAGE_ENGINE=1 \                  
-DMYSQL_TCP_PORT=3306 \                             #[MySQL的监听端口]
-DENABLED_LOCAL_INFILE=1 \                          #[启用加载本地数据]
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_EMBEDDED_SERVER=OFF \
-DWITH_SYSTEMD=1                                    #[用systemd管理mysql]

然后

make -j 2                                           #此过程时间过长
make install

创建mysql用户,并更改权限

[root@MySql ~]# groupadd -g 1000 mysql
[root@MySql ~]# useradd -g 1000 -M -s /sbin/nologin -u 1000 mysql
[root@MySql ~]# chown -R mysql: /usr/local/mysql/
[root@MySql ~]# chown -R mysql: /data/mysql

将mysqld.servie复制到/usr/lib/systemd/system/

[root@MySql ~]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service   /usr/lib/systemd/system/

创建配置文件

[root@MySql ~]# vim /etc/my.cnf 
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/usr/local/mysql/mysql.sock
log_error=/log/mysql/mysql.log
server-id=1
explicit_defaults_for_timestamp=true 

创建log目录与pid目录,并修改权限

mkdir -p /log/mysql
chown -R mysql: /log/mysql
mkdir -p /var/run/mysqld
chown -R mysql: /var/run/mysqld #可以通过grep pid /usr/lib/systemd/system/mysqld.service查看pid文件位置

添加环境变量

[root@MySql ~]# echo "export PATH=/usr/local/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh
[root@MySql ~]# source /etc/profile

初始化 mysql 数据库

[root@MySql ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
--initialize 会生成一个随机密码,而 -–initialize-insecure 不会生成密码,在MySQL安全配置向导mysql_secure_installation设置密码时,可自由选择 mysql 密码等级。
--datadir目标目录下不能有数据文件。

启动数据库

[root@MySql ~]# systemctl start mysqld
[root@MySql ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2018-07-20 14:41:43 CST; 1h 28min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 37883 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─37883 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

720 14:41:42 MySql systemd[1]: Starting MySQL Server...
720 14:41:43 MySql systemd[1]: Started MySQL Server.

配置安全向导

[root@MySql ~]# mysql_secure_installation
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

测试链接

[root@MySql ~]# mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

开放 Root 远程连接权限

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> grant all privileges on *.* to 'root'@'%' identified by 'password';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
4 rows in set (0.00 sec)

开放3306端口

[root@MySql ~]# firewall-cmd --add-port=3306/tcp --permanent
success
[root@MySql ~]# firewall-cmd --reload
success

OK! 安装到此结束


遇到的问题:
1.编译时报错
collect2: 错误:ld 返回 1
make[2]: *** [libmysqld/examples/mysql_client_test_embedded] 错误 1
make[1]: *** [libmysqld/examples/CMakeFiles/mysql_client_test_embedded.dir/all] 错误 2
make: *** [all] 错误 2
解决方法:编译时添加参数 -DWITH_EMBEDDED_SERVER=OFF

2.启动时由于默认目录/var/run下没有mysqld目录,无法创建pid文件,所以需要手动创建/var/run/mysqld目录。并修改权限
如果my.cnf中定义了log文件的目录,也需要手动创建,并需要修改权限

3.初始化数据库有个警告:
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
解决方法:在my.cnf中添加explicit_defaults_for_timestamp=true

4.服务器重启后/var/run/mysqld目录丢失,需要重新创建,我的解决方法时是让他启动之后自己创建
echo “mkdir -p /var/run/mysqld” >> /etc/rc.d/rc.local
echo “chown -R mysql: /var/run/mysqld ” >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

猜你喜欢

转载自blog.csdn.net/harryxxxxx/article/details/81135222