logo头像
Snippet 博客主题

centos7下mysql5.7编译安装

一.卸载mariadb数据库

由于centos7自带mariadb数据库,所以需要先卸载。

1
2
3
4
5
6
[root@localhost /home/lzq]# find / -name mariadb*            #查找mariadb的文件
find: ‘/run/user/1000/gvfs’: 权限不够
/etc/ld.so.conf.d/mariadb-x86_64.conf
/usr/share/doc/mariadb-libs-5.5.60
[root@localhost /home/lzq]# rpm -qa | grep "mariadb" #查找mariadb的服务
mariadb-libs-5.5.60-1.el7_5.x86_64

卸载mariadb的服务

1
2
3
4
[root@localhost /home/lzq]# rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64 --nodeps
/sbin/ldconfig: /lib64/libstdc++.so.6 不是符号连接
[root@localhost /home/lzq]# find / -name mariadb* #再次查找mariadb的文件文件就没了
find: ‘/run/user/1000/gvfs’: 权限不够

二.安装mysql依赖的库

1
[root@localhost /home/lzq]# yum -y install make gcc-c++ cmake bison-devel ncurses-devel

三.下载源代码包

mysql5.7.5以后的版本都要依赖boost,我没有安装boost,就直接下载了自带boost的源代码包

mysql代码包下载地址

如果上面的地址没了csdn的下载地址

csdn下载地址

四.解压代码编译安装

1
[lzq@localhost ~/tool]$  tar zxvf mysql-boost-5.7.22.tar.gz

然后进入mysql代码文件夹

1
[root@localhost ~/tool]$ cd mysql-5.7.22/

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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]

无注释可直接复制

1
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_EMBEDDED_SERVER=OFF -DWITH_SYSTEMD=1

配置完后安装

1
2
[root@localhost /home/lzq/tool/mysql-5.7.22]# make -j 2
[root@localhost /home/lzq/tool/mysql-5.7.22]# make install

五.mysql安装后相关配置

1.创建mysql用户,并更改权限

1
2
3
4
5
6
7
8
[root@localhost /home/lzq]# groupadd -g 1100 mysql
[root@localhost /home/lzq]# useradd -g 1100 -M -s /sbin/nologin -u 1100 mysql
[root@localhost /home/lzq]# chown -R mysql: /usr/local/mysql/
[root@localhost /home/lzq]# chown -R mysql: /data/mysql
chown: 无法访问"/data/mysql": 没有那个文件或目录
[root@localhost /home/lzq]# mkdir /data #创建 /data
[root@localhost /home/lzq]# mkdir /data/mysql #创建 /data/mysql
[root@localhost /home/lzq]# chown -R mysql: /data/mysql

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

1
[root@localhost /home/lzq]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service   /usr/lib/systemd/system/

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

1
2
3
4
[root@localhost /home/lzq]# mkdir -p /log/mysql
[root@localhost /home/lzq]# chown -R mysql: /log/mysql
[root@localhost /home/lzq]# mkdir -p /var/run/mysqld
[root@localhost /home/lzq]# chown -R mysql: /var/run/mysqld #可以通过grep pid /usr/lib/systemd/system/mysqld.service查看pid文件位置

4.添加环境变量

打开/etc/profile添加mysql bin路径

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

1
[root@localhost /home/lzq]# source /etc/profile

5.初始化数据库并启动

初始化数据库

1
[root@localhost /home/lzq]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

启动数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@localhost /home/lzq]# systemctl start mysqld
[root@localhost /home/lzq]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since 四 2019-01-17 08:30:41 CST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 19998 ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 19917 ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 20001 (mysqld)
Tasks: 27
CGroup: /system.slice/mysqld.service
└─20001 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/ru...

1月 17 08:30:41 localhost.localdomain mysqld[19998]: 2019-01-17T00:30:41.365...
1月 17 08:30:41 localhost.localdomain mysqld[19998]: 2019-01-17T00:30:41.365...
1月 17 08:30:41 localhost.localdomain mysqld[19998]: 2019-01-17T00:30:41.368...
1月 17 08:30:41 localhost.localdomain mysqld[19998]: 2019-01-17T00:30:41.369...
1月 17 08:30:41 localhost.localdomain mysqld[19998]: 2019-01-17T00:30:41.369...
1月 17 08:30:41 localhost.localdomain mysqld[19998]: 2019-01-17T00:30:41.369...
1月 17 08:30:41 localhost.localdomain mysqld[19998]: 2019-01-17T00:30:41.369...
1月 17 08:30:41 localhost.localdomain mysqld[19998]: 2019-01-17T00:30:41.382...
1月 17 08:30:41 localhost.localdomain mysqld[19998]: 2019-01-17T00:30:41.445...
1月 17 08:30:41 localhost.localdomain systemd[1]: Started MySQL Server.
Hint: Some lines were ellipsized, use -l to show in full.

6.设置密码

6.1 密码设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
[root@localhost /home/lzq]# mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 50
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!
6.2 遇到的错误

Failed! Error: Your password does not satisfy the current policy requirements

解决办法:无密码登陆mysql执行

set global validate_password_policy=0;

set global validate_password_length=4;

然后在执行mysql_secure_installation设置密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@localhost /home/lzq]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
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> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)
```


#### 7.开放Root远程连接权限

``` bash
[root@localhost /home/lzq]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
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> 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> grant all privileges on *.* to 'root'@'%' identified by 'root';
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

mysql> select * from user;

六.mysql配置文件

打开/etc/my.cnf

1
[root@localhost /home/lzq]# gedit /etc/my.cnf

添加如下内容

1
2
3
4
5
6
7
[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
微信打赏