MySQL的安装方式有rpm、源码编译安装,这边文章将演示免编译安装
一、安装配置
1.创建mysql用户并解压mysql安装包
[root@mysql local]# groupadd -r mysql [root@mysql local]# useradd -g mysql -r -M -s /sbin/nologin mysql [root@mysql local]# tar -zxvf mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ [root@mysql local]# cd /usr/local/ [root@mysql local]# ln -sv mysql-5.6.14-linux-glibc2.5-x86_64 mysql [root@mysql local]# chown -R mysql:mysql mysql-5.6.14-linux-glibc2.5-x86_64/
2.新建文件夹存放mysql的数据文件,最好放到一个独立的分区上,我这里就直接放在/下面了
[root@mysql local]# mkdir -p /mysql/data [root@mysql local]# chown -R mysql:mysql !$
3.开始初始化数据库
[root@mysql media]# cd /usr/local/mysql/scripts/ [root@mysql scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql -- datadir=/mysql/data/ WARNING: The host 'mysql' could not be looked up with /usr/local/mysql/bin/resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables.../usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
#因为我的linux是最小化安装的,这里报错缺少共享库文件,没关系,我们安装一下就可以了。
[root@mysql scripts]# yum install -y libaio [root@mysql scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mysql/data/
#这次执行就成功了
4. 修改PATH变量
[root@mysql scripts]#vim /etc/profile export PATH=$PATH:/usr/local/mysql/bin #最后增加这一行 [root@mysql scripts]# . !$
5. 修改MANPATH让man命令可以找到mysql的帮助文档
[root@mysql scripts]#vim /etc/man.config MANPATH /usr/local/mysql/man/ #增加这一行
6. 添加库文件路径
[root@mysql scripts]#vim /etc/ld.so.conf.d/mysql-x86_64.conf /usr/local/mysql/lib #增加这一行 [root@mysql scripts]#ldconfig -v
7. 添加头文件路径
[root@mysql scripts]#ln -sv /usr/local/mysql/include /usr/include/mysql `/usr/include/mysql' -> `/usr/local/mysql/include'
8. 修改mysql的属主为root
[root@mysql local]# chown -R root mysql-5.6.14-linux-glibc2.5-x86_64/
9. 修改配置文件
[root@mysql mysql]# vi /etc/my.cnf [mysqld] datadir=/mysql/data socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [root@mysql mysql]# cp support-files/mysql.server /etc/init.d/mysqld
10.设置开机自动启动
[root@mysql mysql]# chkconfig --add mysqld [root@mysql mysql]# chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
二、用户管理
mysql的用户是以‘user’@‘host’组成的,当host变成%时,代表主机可以是任意主机。
1.修改root密码
第一种方法:
# mysqladmin -u root password ’new-password’ # mysqladmin -u root -h this_host_name password ’new-password’
第二种方法
mysql> set password for 'root'@'localhost' = PASSWORD('new-password'); mysql> select host,user,password from mysql.user; mysql> flush privileges;
第三种方法:
mysql> update mysql.user set password = PASSWORD('new-password') where host='localhost'; mysql> flush privileges;
2.删除匿名用户
mysql> drop user ''@localhost; mysql> drop user ''@mysql;
3.创建用户
mysql> grant all privileges on *.* to 'user01'@'localhost' identified by 'mysql';