在ubuntu下使用源码包安装mysql

安装前的准备,需要安装编译器
apt-get install g++
apt-cache search ncurses
apt-get install libncurses5-dev
 
备注:一台服务器安装多个mysql要注意几点:
1.配置文件安装路径不能相同
2.数据库目录不能相同
3.启动脚本不能同名
4.端口不能相同
5.socket文件的生成路径不能相同
 
mysql源码包安装过程:
1下载mysql
wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.1/mysql-5.1.57.tar.gz
 
2.创建mysql组和用户
shell>su root
shell>groupadd mysql
shell>useradd -g mysql mysql
shell>id mysql
 
3.创建安装目录
mkdir -p /usr/local/mysql1/data
mkdir -p /usr/local/mysql1/tmp
 
4.编译安装mysql(经典的四步)
tar -zxvf mysql-5.1.57.tar.gz
cd mysql-5.1.57
./configure --prefix=/usr/local/mysql1 --localstatedir=/usr/local/mysql1/data --with-mysqld-user=mysql --with-charset=utf8 --with-unix-socket-path=/usr/local/mysql1/tmp/mysql.sock
make
make install
 
5.修改权限和创建配置文件
cp support-files/my-medium.cnf /usr/local/mysql1/my.cnf  
备注
根据机器配置的不同选择不同的文件:
/user/local/mysql/share/mysql/my-small.cnf   最小配置安装,内存<=64M,数据数量最少
/user/local/mysql/share/mysql/my-large.cnf 内存=512M
/user/local/mysql/share/mysql/my-medium.cnf  32M<内存<64M,或者内存有128M,但是数据库与web服务器公用内存
/user/local/mysql/share/mysql/my-huge.cnf  1G<内存<2G,服务器主要运行mysql
/user/local/mysql/share/mysql/my-innodb-heavy-4G.cnf  最大配置安装,内存至少4G
 
[mysqld]
basedir = /usr/local/mysql1              定义mysql程序目录
datadir = /usr/local/mysql1/data         定义数据目录
并修改端口,各个mysql使用不同的端口
 
#设置权限
chown mysql:mysql /usr/local/mysql1/my.cnf
chown -R mysql:mysql /usr/local/mysql1
 
6.初始化mysql(用户表和权限表)和启动mysql
#进入mysql的安装目录
cd /usr/local/mysql1
#初始化数据库
bin/mysql_install_db --basedir=/usr/local/mysql1 --datadir=/usr/local/mysql1/data --user=mysql
 
#启动mysql
bin/mysqld_safe --defaults-file=/usr/local/mysql1/my.cnf &
 
备注:&代表后台运行
停止mysql可以使用:bin/mysqladmin shutdown -u root -p
 
#设置root用户的密码
bin/mysqladmin -u root password '123456'
 
备注:如果出现error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'解决办法:
ln -s /usr/local/mysql1/tmp/mysql.sock /tmp/mysql.sock
chmod 755 /tmp/mysql.sock
 
#登录mysql
bin/mysql -u root -p  
 
7.设置mysql开机自动启动
#进入解压后的源码目录
cd mysql-5.1.57
#将mysql.server这个文件copy到/etc/init.d/目录下,并更名为mysql1
cp support-files/mysql.server /etc/init.d/mysql1  
#给mysql1这个文件赋予可执行的权限
chmod 755 /etc/init.d/mysql1
#加入到开机自动运行
chkconfig --add mysql1
chkconfig --level 345 mysql1 on
#重新启动mysql
service mysql1 restart
 
#检查是否正常,程序是否已经运行,端口是否打开
ps -ef | grep mysql

netstat -an | grep'3306'

至此安装就完成了,我装的非常成功,啦啦啦~~~

 
备注
1、更改root用户的密码有两种方法(把root用户的密码从123456改成123456)
(1)bin/mysqladmin -u root -p123456 password 123456
(2)bin/mysql -u root -p
  mysql> use mysql;
  mysql> update user set password=password('654321') where user='root'
  mysql>flush privileges;
 
2、新增用户并授权
 格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码"
 例如:grant all privileges on *.* to nova@'%' identified by '123456';
      grant select,insert,update,delete on mydb.* to test2@localhost   identified by "abc";
     grant all privileges on *.* to root@'%' with grant option

你可能感兴趣的:(常见问题解决)