这里介绍Linux下两种安装mysql的方式:yum安装和源码编译安装。
1. yum安装
(1)首先查看centos自带的mysql是否被安装:
# yum list installed |grep mysql //若有自带安装的mysql,将其卸载# yum -y remove mysql-libs.x86_64
(2)下载MySQL官网的yum仓库:https://dev.mysql.com/downloads/repo/yum/,
# yum localinstall mysql57-community-release-el6-11.noarch.rpm//查看yum仓库是否成功添加# yum repolist enabled |grep "mysql.*-community.*"
(3)使用mysql yum仓库时,默认情况下选择的最新版本进行安装,也可以通过手动编辑文件来选择一个版本安装。例如,要安装mysql5.6版本,则再mysql56-community设置enabled=1,mysql57-community设置enabled=0。
# vim /etc/yum.repos.d/mysql-community.repo[mysql57-community]name=MySQL 5.7 Community Serverbaseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/enabled=0gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql# Enable to use MySQL 5.6[mysql56-community]name=MySQL 5.6 Community Serverbaseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/enabled=1gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
(4)安装mysql
# yum install mysql-community-server
(5)启动mysql服务
# service mysqld start
若出现下面输出则表示mysql安装成功:
Starting mysqld:[ OK ]
2. 源码编译安装mysql
(1)首先安装源码编译所需要的包
# yum -y install make gcc-c++ cmake bison-devel ncurses-devel
(2)下载并解压安装包
# wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.14.tar.gz
# tar xvf mysql-5.6.14.tar.gz
(3)编译安装(编译参数按实际情况制定)
# cd mysql-5.6.14# cmake .-DCMAKE_INSTALL_PREFIX=/usr/local/mysql /-DMYSQL_DATADIR=/usr/local/mysql/data /-DSYSCONFDIR=/etc /-DWITH_MYISAM_STORAGE_ENGINE=1 /-DWITH_INNOBASE_STORAGE_ENGINE=1 /-DWITH_MEMORY_STORAGE_ENGINE=1 /-DWITH_READLINE=1 /-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock /-DMYSQL_TCP_PORT=3306 /-DENABLED_LOCAL_INFILE=1 /-DWITH_PARTITION_STORAGE_ENGINE=1 /-DEXTRA_CHARSETS=all /-DDEFAULT_CHARSET=utf8 /-DDEFAULT_COLLATION=utf8_general_ci# make && make install
(4)配置mysql
设置权限:
# useradd mysql# passwd mysql # chown -R mysql:mysql /usr/local/mysql
初始化mysql:
# cd /usr/local/mysql# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
注意:在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。
(5)注册为服务
# cd /usr/local/mysql/support-files//注册服务# cp mysql.server /etc/rc.d/init.d/mysql//使用默认配置文件# cp my-default.cnf /etc/my.cnf//设置开机启动 # chkconfig mysql on
(6)启动服务
# service mysql start
3. mysql客户端
首次进入mysql客户端时,通常会出现这种错误:
解决方案如下:
(1)在/etc/my.cnf文件中添加如下命令:
(2)重启mysql服务之后,进入mysql客户端修改root用户的密码:
update mysql.user set authentication_string=password("PASSWORD") where user="root";flush privileges;
(3)将刚刚添加的那条命令注释掉,在mysql客户端重设密码:
//设置密码强度和长度> set global validate_password_policy=0;> set global validate_password_length=1;// 更改密码> alter user 'root'@'localhost' identified by 'PASSWORD';
(4)若设置root用户可以远程访问,还需执行:
> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;> flush privileges;
随后就可以通过mysql客户端创建数据库、表这些了。
总结
新闻热点
疑难解答