在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
4 准备mysql用户和组以及目录
groupadd mysql useradd -g mysql mysql cd /usr/localmkdir mysqlcd mysqlmkdir datamkdir logcd ..chown -R 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 -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
配置成功后就可以编译安装了,中间要等一段时间
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
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 -uroot -p(接下来会要求输入你的密码)
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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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 strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

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

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)
