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 --nodeps
[root@MySql ~]# find / -name mariadb*

安装依赖

yum -y install make gcc-c++ cmake bison-devel libaio libaio-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  \
-DBUILD_CONFIG=mysql_release \						#使用该选项,可以促使编译器按照oracle生成官方版本的选项来编译程序
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \			#[MySQL安装的根目录]
-DMYSQL_DATADIR=/data/mysql \						#[MySQL数据库文件存放目录]
-DSYSCONFDIR=/etc \									#[MySQL配置文件所在目录]
-DMYSQL_UNIX_ADDR=/tmp/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 \

然后

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 root:mysql /usr/local/mysql/
[root@MySql ~]# chown -R mysql: /data/mysql
[root@MySql ~]# mkdir -p /log/mysql
[root@MySql ~]# chown -R mysql: /log/mysql

创建配置文件

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

创建log目录,并修改权限

mkdir -p /log/mysql
chown -R mysql: /log/mysql

添加环境变量

[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@131 ~]# ll /data/mysql/
total 122920
-rw-r-----. 1 mysql mysql        5 Sep 28 20:42 131.pid
-rw-r-----. 1 mysql mysql       56 Sep 28 20:21 auto.cnf
-rw-r-----. 1 mysql mysql      366 Sep 28 20:39 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Sep 28 20:42 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Sep 28 20:42 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Sep 28 20:21 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Sep 28 20:42 ibtmp1
drwxr-x---. 2 mysql mysql     4096 Sep 28 20:21 mysql
drwxr-x---. 2 mysql mysql     8192 Sep 28 20:21 performance_schema
drwxr-x---. 2 mysql mysql     8192 Sep 28 20:21 sys

[root@131 ~]# ll /log/mysql/
total 20
-rw-r-----. 1 mysql mysql 18183 Sep 28 20:42 error.log

复制启动脚本

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

启动数据库:

方法一

[root@131 mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!

方法二

[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

7月 20 14:41:42 MySql systemd[1]: Starting MySQL Server...
7月 20 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

开机启动

/sbin/chkconfig mysqld on

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.初始化数据库有个警告:
[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

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
CentOS 7 安装 MySQL 的步骤如下: 1.安装必要的依赖项 首先需要安装一些必要的依赖项,包括 gcc、cmake、make、bison、ncurses-devel 和 zlib-devel。可以使用以下命令安装它们: ``` sudo yum install gcc cmake make bison ncurses-devel zlib-devel -y ``` 2.下载 MySQL 代码 在安装 MySQL 之前,需要先下载 MySQL代码。可以从 MySQL 官网下载最新版本的代码。下载后,可以将其解压到 /usr/local/src 目录下: ``` cd /usr/local/src sudo wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.25.tar.gz sudo tar -xzf mysql-8.0.25.tar.gz ``` 3.编译和安装 MySQL 进入解压后的代码目录,运行以下命令进行编译和安装: ``` cd mysql-8.0.25 sudo cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_BOOST=/usr/local/src/mysql-8.0.25/boost sudo make && sudo make install ``` 4.创建 MySQL 用户和组 为 MySQL 创建一个专用的用户和组,使其能够运行 MySQL 服务: ``` sudo groupadd mysql sudo useradd -r -g mysql -s /bin/false mysql ``` 5.初始化 MySQL 数据库 运行以下命令来初始化 MySQL 数据库,并设置 root 用户密码: ``` sudo mkdir /usr/local/mysql/data sudo chown mysql:mysql /usr/local/mysql/data sudo /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data sudo /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data sudo systemctl start mysqld sudo systemctl enable mysqld sudo systemctl status mysqld ``` 6.登录 MySQL 使用以下命令登录 MySQL: ``` sudo /usr/local/mysql/bin/mysql -u root -p ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值