MySQL的安裝方式有rpm、源碼編譯安裝,這邊文章將演示免編譯安裝
1、安裝配置mysql
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/linux
2.新建文件夾存放mysql的數據文件,最好放到一個獨立的分區上,我這裏就直接放在/下面了
[root@mysql local]# mkdir -p /mysql/data
[root@mysql local]# chown -R mysql:mysql !$sql
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]# . !$vim
5. 修改MANPATH讓man命令能夠找到mysql的幫助文檔
[root@mysql scripts]#vim /etc/man.config
MANPATH /usr/local/mysql/man/
#增長這一行socket
6. 添加庫文件路徑
[root@mysql scripts]#vim /etc/ld.so.conf.d/mysql-x86_64.conf
/usr/local/mysql/lib #增長這一行
[root@mysql scripts]#ldconfig -vide
7. 添加頭文件路徑
[root@mysql scripts]#ln -sv /usr/local/mysql/include /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include'this
8. 修改mysql的屬主爲root
[root@mysql local]# chown -R root mysql-5.6.14-linux-glibc2.5-x86_64/spa
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/mysqldorm
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
2、用戶管理
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';
連接數據庫時出現錯誤
[root@localhost mysql]# /usr/local/mysql/bin/mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
由於系統啓動時生成的本機通訊套接字在/var/lib/mysql/mysql.sock,而默認的是在/tmp/mysql.sock
客戶端在/tmp/mysql.sock中找不到套接字文件故會出現此錯誤
能夠把/etc/my.cnf修改成下面的形式
注:主要是針對socket的修改
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
[mysqld]
port = 3306
datadir=/data/mydata
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
skip_name_resolve
# 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