Linux服务器命令行安装二进制mysql
mysql安装的详细步骤,留作个人笔记,方便以后使用
首先查看系统的版本号,下载对应的32位或64位的mysql,查看命令位:uname -a,如果有x86_64则位64位机器,如果有i386则位32位机器。
然后去官网下载对应的mysql压缩包,这里以64位为例,http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz,这里我先有windows下载完毕之后使用winscp上传到linux服务器中。
之后正式进入安装步骤:
1、解压tar.gz
tar –xzvf mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz
2、重命名解压的文件夹
mv mysql-5.6.10-linux-glibc2.5-x86_64 mysql
3、将mysql文件夹移动到/usr/local目录下
sudo mv mysql /usr/local
4、进入mysql目录
cd /usr/local/mysql
5、增加mysql用户组
sudo groupadd mysql
6、增加mysql用户
sudo useradd -r -g mysql mysql
7、将mysql文件夹own及grp变更为mysql
sudo chown -R mysql .
sudo chgrp -R mysql .
8、执行mysql安装脚本 (此处最容易出错误)
sudo scripts/mysql_install_db --user=mysql
若未安装libaio包,会有一个报错提示,安装libaio-dev后,再运行脚本即可
sudo apt-get install libaio-dev
若出现(Can't locate Data/Dumper.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at scripts/mysql_install_db line 42.
BEGIN failed--compilation aborted at scripts/mysql_install_db line 42.)执行下面命令
yum install 'perl(Data::Dumper)'
9、将目录权限变更回来,仅保留data目录为mysql用户
sudo chown -R root .
sudo chown -R mysql data
10、将mysql配置文件拷贝到etc目录(全局配置)
注意:5.6版本的默认配置文件名称由原先的my-medium变更为了my-default。
sudo cp support-files/my-default.cnf /etc/my.cnf
11、启动mysql
sudo bin/mysqld_safe --user=mysql &
12、初始化mysql root用户密码
sudo bin/mysqladmin -u root password '密码文字'
13、复制mysql.server脚本到/etc/init.d(初始化服务,有些人喜欢改成mysqld,在这里改就可以)
sudo cp support-files/mysql.server /etc/init.d/mysql.server
14、查看mysql运行状态
sudo service mysql.server status
如果运行正常,会显示 MySQL running。
如果显示 not running,应该是前面没有启动服务,可直接用service mysql.server start启动
sudo service mysql.server [status|start|stop]
15、让mysql开机启动[defaults],取消开机启动[remove]
sudo update-rc.d -f mysql.server defaults [remove]
如果是在centos系统中,以上命令可能无效,需要执行:chkconfig --add mysql.server
查看是否成功:chkconfig --list
16、将mysql/bin/mysql命令加入到用户命令中,或将mysql/bin目录加入path
加入用户命令:
sudo ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
加入环境变量:
export PATH=$PATH:/usr/local/mysql/bin
17、允许root用户远程登录
1>进入mysql: mysql –u root –p
2>改变数据库: use mysql;
3>从任意主机登录: grant all privileges on *.* to root@"%" identified by "密码文字" with grant option;
4>从指定主机登录: grant all privileges on *.* to root@"192.168.1.101" identified by "passw0rd" with grant option;
5>授权生效: flush privileges;
6>查看host为%授权是否添加: select * from user;
18、找个客户端去试试吧。
同一服务器部署多个mysql
1.将/usr/local/mysql拷贝到一个新的文件夹中
cp -r /usr/local/mysql /usr/local/mysql2
2.拷贝my.cnf配置文件
cp /etc/my.cnf /etc/my2.cnf
3.拷贝mysql.server启动脚本
cp /etc/init.d/mysql.server /etc/init.d/mysql2.server
4.赋予文件夹权限
cd /usr/local/mysql2
chown -R root .
chown -R mysql data
5.修改配置文件my2.cnf
[mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = /usr/local/mysql2 datadir = /usr/local/mysql2/data port = 3307 #server_id = ..... socket = /tmp/mysql2.sock
6.修改启动脚本mysql2.server
basedir=/usr/local/mysql2 datadir=/usr/local/mysql2/data conf=/etc/my2.cnf 把$bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1&替换为: $bindir/mysqld_safe --defaults-file=/etc/my2.cnf --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 & 把下面这句注释掉(前面加个#): parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
7.设置开机自启动
chkconfig --add mysql2.server
8.分别启动两个mysql
/etc/init.d/mysql.server start
/etc/init.d/mysql2.server start
查看进程是否启动 ps -ef|grep mysql
root 2400 1 0 16:48 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/weekend110.pid mysql 2549 2400 0 16:48 pts/0 00:00:05 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/weekend110.err --pid-file=/usr/local/mysql/data/weekend110.pid root 3368 1 0 17:14 pts/0 00:00:00 /bin/sh /usr/local/mysql2/bin/mysqld_safe --defaults-file=/etc/my2.cnf --datadir=/usr/local/mysql2/data --pid-file= mysql 3524 3368 0 17:14 pts/0 00:00:03 /usr/local/mysql2/bin/mysqld --defaults-file=/etc/my2.cnf --basedir=/usr/local/mysql2 --datadir=/usr/local/mysql2/data --plugin-dir=/usr/local/mysql2/lib/plugin --user=mysql --log-error=/usr/local/mysql2/data/weekend110.err --pid-file=/usr/local/mysql2/data/weekend110.pid --socket=/tmp/mysql2.sock --port=3307
配置成功!!!!