Linux下编译安装MySQL

获得以下所需的源代码包,并存放在/usr/local/src

与mysql相关: boost_1_59_0.tar.gz cmake-3.6.2.tar.gz mysql-5.7.16.tar.gz
 

安装cmake前的依赖包的安装:

检查gcc-c++ 、ncurses-devel是否安装,如果没有安装,先用yum进行安装
 

编译安装cmake工具

cd /usr/local/src
tar xf cmake-3.6.2.tar.gz
cd cmake-3.6.2
./bootstrap --prefix=/usr/local/cmake
make
make install             //如果前面没有指定安装目录,则默认安装到/usr/local/bin/cmake
 

建立mysql组和 用户,并将mysql用户添加到mysql组

groupadd mysql
useradd -g mysql mysql

创建mysql数据文件存放的目录

mkdir /mydata
chown mysql:mysql /mydata
chmod o= /mydata           //设置其他人没有任何权限
 

编译安装mysql

cd /usr/local/src
tar xf mysql-5.7.16.tar.gz
cd mysql-5.7.16
/usr/local/cmake/bin/cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata -DWITH_BOOST=/usr/local/src -DSYSCONFDIR=/etc -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled
make && make install
 

更改mysql安装目录的属主属组并添加mysql环境变量

chown -R mysql:mysql /usr/local/mysql
vim /etc/profile.d/mysql.sh
文件内容是: export PATH=$PATH:/usr/local/mysql/bin
执行命令: bash          //让新的PATH变量生效
 

安装时/usr/local/mysql目录找不到怎么办?

查找资料说需要单独安装Development Libraries开发包
[root@centos6-y ~]# 
sudo rpm -ivh mysql-community-devel-5.7.13-1.el7.x86_64.rpm
[sudo] password for home: 
warning: mysql-community-devel-5.7.13-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
        libmysqlclient.so.20()(64bit) is needed by mysql-community-devel-5.7.13-1.el7.x86_64
        mysql-community-libs(x86-64) >= 5.7.9 is needed by mysql-community-devel-5.7.13-1.el7.x86_64
[ocsr90@localhost temp]$ sudo rpm -ivh mysql-community-devel-5.7.13-1.el7.x86_64.rpm --nodeps --noforce
rpm: --noforce: unknown option
[ocsr90@localhost temp]$ sudo rpm -ivh mysql-community-devel-5.7.13-1.el7.x86_64.rpm --nodeps --force
warning: mysql-community-devel-5.7.13-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-devel-5.7.13-1.el################################# [100%]
 
安装完,目录/usr/include/mysql存在了
 
 

加入服务列表并设置为开机自启

cd /usr/local/mysql/support-files
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig mysqld on
 

修改mysql的配置文件

cat /etc/my.cnf
[mysql]
socket=/tmp/mysql.sock
 
[mysqld]
datadir=/mydata
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
 
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/mydata/mysqld.pid
 
 

初始化mysql

mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/mydata
说明:
##“-–initialize”会生成一个随机密码(~/.mysql_secret),而”–initialize-insecure”不会生成密码。
##user表示指定用户 ##basedir表示mysql的安装路径,datadir表示数据库文件存放路径。
 

启动mysql服务

# service mysqld start
查看MySQL服务的进程和端口
# ps -ef | grep mysqld
root 22306 1 0 12:51 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/mydata --pid-file=/mydata/web1.deng.com.pid
mysql 22480 22306 12 12:51 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mydata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/mydata/web1.deng.com.pid --socket=/tmp/mysql.sock
 
# netstat -an | grep :3306
tcp 0 0 :::3306 :::* LISTEN
 
初始化MySQL数据库的root用户密码
# 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 #需要修改密码,所以输入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: 2 #设置密码复杂度为强
Please set the password for root here.
New password: 
Re-enter new password: #输入2次新密码
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) : y #禁止root远程登录
... 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!
 

将MySQL数据库的动态链接库共享至系统链接库

vim /etc/ld.so.conf.d/mysql.conf
文件内容是:
/usr/local/mysql/lib
ldconfig -v       //让系统重新读取库文件
 

测试登陆MySQL数据库

# mysql -uroot -p
Enter password: #输入刚才设置的新密码
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, 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> exit
Bye
原文地址:https://www.cnblogs.com/5945yang/p/10267441.html