由于工作和学习方面的原因,经常使用linux,前段时间折腾了几次linux上源代码方式安装mysql,期间也参考了网友的各种介绍,现在总结如下,方便自己以后查看,也方便其他人学习使用。
1 环境
linux:CentOS 6.5/6.3 x86/x86_64均实验过
2 准备
这里进行介绍所需要的软件或工具版本有:
MySQL:mysql-5.5.37.tar.gz,cmake:cmake-2.8.12.2.tar.gz
这些都可以到相应的官网下载,版本无关紧要,重要的是过程和方法。
还要为linux安装gcc和gcc-c++以及ncurses-devel和由此产生的依赖,直接使用yum进行安装
yum install gcc gcc-c++ ncurses-devel安装完后就可以进行正式的安装了。
3 安装cmake
将下载好的cmake-2.8.12.2.tar.gz上传到linux上,然后解包
tar -zxvf cmake-2.8.12.2.tar.gz进入解压出来的目录,然后按照要求运行安装命令(中间可能要等一会儿)
cd cmake-2.8.12.2./boostrapmakemake install这样就把cmake装好了,之后就可以安装MySQL了。
4 准备mysql用户和组以及目录
groupadd mysql useradd -g mysql mysql cd /usr/localmkdir mysqlcd mysqlmkdir datamkdir logcd ..chown -R mysql:mysql mysql这样就创建了mysql的用户和用户组,也创建了安装的相关目录。这里的目录可以修改,但是要和接下来的安装配置相对应。
5 安装mysql
将下载的mysql-5.5.37.tar.gz上传到linux上,解压缩包
tar -zxvf mysql-5.5.37.tar.gz进入源代码目录
cd mysql-5.5.37使用cmake配置编译文件
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=mysql这里面的目录要和之前准备的目录关联,如果提示出错的话还要删除cmake的缓存文件,修正错误,然后重新配置。
配置成功后就可以编译安装了,中间要等一段时间
makemake install
6 mysql的安装后配置
修改文件权限
chmod +w /usr/local/mysql/chown -R mysql:mysql /usr/local/mysql/查看链接库的情况
ls /usr/local/mysql/lib输出可能是这个样子的
libmysqlclient.a libmysqlclient_r.so.18.0.0 libmysqlservices.alibmysqlclient_r.a libmysqlclient.so pluginlibmysqlclient_r.so libmysqlclient.so.18libmysqlclient_r.so.18 libmysqlclient.so.18.0.0要创建一个链接文件,如果具体的输出不一样,对应更改
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18进入源代码目录下面的配置文件目录
cd support-files/复制相关配置文件
cp my-small.cnf /etc/my.cnf cp mysql.server /etc/rc.d/init.d/mysql初始化安装,
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql为文件添加可执行权限
chmod +x /etc/init.d/mysql修改配置文件,
vi /etc/init.d/mysql 找到修改为 basedir=/usr/local/mysql datadir=/usr/local/mysql/data mysqld_pid_file_path=/usr/local/mysql/log/mysql.pid为mysql服务添加开机自动启动
chkconfig --add mysqlchkconfig --level 345 mysql on具体的启动“等级”根据需要修改
前面都正确的话现在就可以启动服务了,
service mysql start接下来是权限方面的配置
7 mysql的权限和用户配置
为可执行文件添加链接
ln -s /usr/local/mysql/bin/mysqladmin /usr/binln -s /usr/local/mysql/bin/mysql /usr/bin
设置root用户(这个root是mysql的root用户)密码
mysqladmin -uroot password '你的密码'接下来使用刚才的密码登录mysql
mysql -uroot -p(接下来会要求输入你的密码)登陆成功后就可以配置具体的权限了,接下来的操作都是在“mysql>”提示符下进行的,表示已登录mysql。
use mysqlmysql> select user,password from user;查询当前已有的账户,可以看到有很多没有密码的用户,这是需要删除的,
delete from user where password='';mysql> select user,password from user;这样就会删除了空密码用户,然后在查看用户列表,就会发现只有一个用户了。
当前的用户还不能在除服务器外的机器上登录,这显然是不应该的,添加root用户远程拥有所有权限
grant all privileges on *.* to root@'%' identified by '你的密码' with grant option;具体的场景下,如果要对权限进行限制,则需要修改具体的语句。
为每一个数据库创建单独的用户并分配全显示很有必要的,下面就是创建一个名为recruitdba的用户,并分配权限为所有主机操作recruit数据库的所有权限
CREATE USER 'recruitdba'@'%' IDENTIFIED BY 'recruitdba';grant all privileges on recruit.* to recruitdba@'%' identified by 'recruitdba' with grant option;flush privileges;
至此,mysql的安装和配制就完成了,具体的环境下有一些不一样的就需要灵活变通了
8 额外配置
有些时候会发现linux的防火墙会阻挡mysql的数据包,所以要添加防火墙规则
iptables -A INPUT -p tcp --dport 3306 -j ACCEPTiptables -A OUTPUT -p tcp --sport 3306 -j ACCEPT service iptables save 进行保存service iptables restart重启
这样设置之后按道理说就应该正确了,但事实上从其他主机访问还是有问题,测试发现是防火墙的问题,
使用iptables -L -n查看到的这两条的顺序应该是这样
Chain INPUT (policy ACCEPT)target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT)target prot opt source destination REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT)target prot opt source destination ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:3306但是结果却是这样
Chain INPUT (policy ACCEPT)target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 Chain FORWARD (policy ACCEPT)target prot opt source destination REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT)target prot opt source destination ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:3306就是这两条的顺序有问题
Chain INPUT (policy ACCEPT)…… REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306先拒绝,再允许,就允许不了了,
使用
vi /etc/sysconfig/iptables

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
