源代码安装mysql

1、获得以下所需的源代码包,并存放在/usr/local/src
与mysql相关:
boost_1_59_0.tar.gz cmake-3.6.2.tar.gz mysql-5.7.16.tar.gz
注意:boost_1_59_0.tar.gz可以不手工安装,在安装mysql的时候指定该源代码在那个目录即可,会自动安装。
2、安装cmake前的依赖包的安装
检查gcc-c++ 、ncurses-devel是否安装,如果没有安装,先用yum进行安装,也可以用rpm安装
如下:

编译安装mysql

编译安装mysql

3、编译安装cmake工具
cd /usr/local/src
tar xf cmake-3.6.2.tar.gz
cd cmake-3.6.2
看readme或者install文件
./bootstrap --prefix=/usr/local/cmake
make
make install #如果前面没有指定安装目录,则默认安装到/usr/local/bin/cmake
如下:
编译安装mysql
编译安装mysql
编译安装mysql
编译安装mysql
编译安装mysql
编译安装mysql
编译安装mysql

4、建立mysql组和用户,并将mysql用户添加到mysql组
groupadd mysql
useradd -g mysql mysql
如下:
编译安装mysql
5、创建mysql数据文件存放的目录
mkdir /mydata
chown mysql:mysql /mydata
chmod o= /mydata #设置其他人没有任何权限
如下:
编译安装mysql
6、编译安装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
注意:上面这一段是一行,其中-DCMAKE_INSTALL_PREFIX指定mysql安装目录,DMYSQL_DATADIR指定mysql数据目录,DWITH_BOOST指定boost_1_59_0.tar.gz软件包路径,后面的其他的用固定的就可以

make && make install
如下:
编译安装mysql
编译安装mysql

上面标识的那一行告诉我们去指定的网页查看安装步骤
编译安装mysql
编译安装mysql
编译安装mysql
编译安装mysql
编译安装mysql

到上面这一步完成后,mysql软件已经安装好了,但是还没有配置,可能还不能用

7、更改mysql安装目录的属主属组并添加mysql环境变量
chown -R mysql:mysql /usr/local/mysql
vim /etc/profile.d/mysql.sh
文件内容是:
export PATH=$PATH:/usr/local/mysql/bin
执行命令:
bash #让新的PATH变量生效
如下:
编译安装mysql编译安装mysql
8、加入服务列表并设置为开机自启
cd /usr/local/mysql/support-files
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig mysqld on
如下:
编译安装mysql
9、修改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

10、初始化mysql注意目录要一一对应,初始化不能有error,可以有warning
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/mydata
说明:
##“-–initialize”会生成一个随机密码(~/.mysql_secret),而”–initialize-insecure”不会生成密码 ##user表示指定用户 ##basedir表示mysql的安装路径,datadir表示数据库文件存放路径
如下:
编译安装mysql
11、启动mys ql服务
service mysqld start
查看MySQL服务的进程和端口
ps -ef | grep mysqld如果有/usr/local/mysql/bin/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也可以通过这种方法查看是否启动成功,有3306LISTEN说明启动成功
tcp 0 0 :::3306 ::???? LISTEN
如下:
编译安装mysql
12、初始化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! 完成了

13、将MySQL数据库的动态链接库共享至系统链接库
vim /etc/ld.so.conf.d/mysql.conf在 /etc/ld.so.conf.d/目录下创建mysql.conf文件
文件内容是:
/usr/local/mysql/lib

ldconfig -v 让系统重新读取库文件,查看是否有刚才的目录,有的话才可以,如下:
编译安装mysql

14、测试登陆MySQL数据库
mysql -uroot -p #这里的root用户是mysql数据库的root,不是操作系统的root用户
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 © 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
-------------------------------------------mysql安装完成了-----------------------------------------------------------

如果有什么不明白的可以私信我哦,不嫌弃的话也可以点赞或者关注我哦,谢谢

相关文章: