search
HomeDatabaseMysql Tutorial在linux上使用源代码安装MySQL_MySQL

    由于工作和学习方面的原因,经常使用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



这样就可以直接使用mysql和mysqladmin命令了,其他的根据需要再添加。

设置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



调整着两条记录的顺序,这样来的实在点,调整过后重启防火墙就行了




Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

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 Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment