1、建立MySQL账号
首先以root登录到Linux,然后执行如下命令创建MySQL组及用户
[root@loveyu home]# groupadd mysql [root@loveyu home]# useradd -s /sbin/nologin -g mysql -M mysql [root@loveyu home]# tail -1 /etc/passwd feitian:x:811:811::/home/mysql:/sbin/nologin(/sbin/nologin表示不能登录) -s/sbin/nologin表示禁止登录 -gmysql指定属于mysql组 -M表示不创建家目录。 [root@loveyu tools]# cd /home/loveyu/tools/
2、编译安装MySQL
2.1获得MySQL软件
1》通过rz上传
2》wgte 在搜狐的镜像站点中下载 http://mirrors.sohu.com/mysql/MySQL-5.7/
2.2编译[root@loveyu tools]# tar zxf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
./configure \ --prefix=/usr/local/mysql \ #安装路径--with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock \ #指定socket文件存放--localstatedir=/usr/local/mysql/data \ #MySQL数据文件存放位置--enable-assembler \ #允许使用汇编模式--enable-thread-safe-client \--with-mysqld-user=mysql \ #指定MySQL运行的系统用户--with-big-tables\--without-debug \ #使用非debug--with-pthread \ #强制使用pthread线程序库编译--enable-assembler \--with-extra-charsets=complex \--with-readline \--with-ssl \--with-embedded-server \--enable-local-infile \--with-plugins=innobase,partition \--enable-thread-safe-client \ #使用线程的方式编译客户端--with-client-ldflags=-all-static \ #服务端使用静态库--with-mysqld-ldflags=-all-static #客户端使用静态库#等待完成之后make && make install
3、获取MySQL配置文件
[root@loveyu mysql-5.1.72]# ll support-files/*.cnf-rw-r--r-- 1 root root 4714 May 11 08:53 support-files/my-huge.cnf-rw-r--r-- 1 root root 19763 May 11 08:53 support-files/my-innodb-heavy-4G.cnf-rw-r--r-- 1 root root 4688 May 11 08:53 support-files/my-large.cnf-rw-r--r-- 1 root root 4699 May 11 08:53 support-files/my-medium.cnf-rw-r--r-- 1 root root 2467 May 11 08:53 support-files/my-small.cnf#这里有大中小和超大三个配置文件,这里是试验环境我们最好选最小的配置文件[root@loveyu mysql-5.1.72]# /bin/cp support-files/my-innodb-heavy-4G.cnf /etc/my.cnf[root@loveyu mysql-5.1.72]# ls -l /etc/my.cnf -rw-r--r-- 1 root root 19763 May 11 09:02 /etc/my.cnf
4、初始化数据库,创建MySQL数据库文件
[root@loveyu data]# mkdir /usr/lacal/mysql/data -p[root@loveyu data]# chown -R mysql /usr/local/mysql[root@loveyu tools]# /application/mysql/scripts/mysql_install_db basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
5、启动数据库
[root@feitian mysql-5.1.72]# cp support-files/mysql.server /usr/local/mysql/bin/[root@feitian mysql-5.1.72]# chmod 700 /usr/local/mysql/bin/mysql.server [root@feitian tools]# /usr/local/mysql/bin/mysql.server startStarting MySQL. SUCCESS! [root@feitian tools]# ps -ef|grep 3306root 64252 63576 0 09:48 pts/0 00:00:00 grep 3306[root@feitian mysql-test]# /usr/local/mysql/bin/mysqld_safe &[1] 64731[root@feitian mysql]# 170511 11:50:09 mysqld_safe Logging to '/var/log/mysqld.log'.170511 11:50:09 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
6、配置MySQL命令全局使用路径(/etc/profile)
PATH=$PATH:/usr/bin/mysql
7、配置/etc/init.d/mysqld start 的方式启动数据库
[root@feitian mysql-5.1.72]# cp support-files/mysql.server /etc/init.d/mysqld[root@feitian mysql-5.1.72]# chmod 700 /etc/init.d/mysqld[root@feitian mysql-5.1.72]# /etc/init.d/mysqld restartShutting down MySQL..... SUCCESS! Starting MySQL. SUCCESS!
8、设置开机自启
[root@feitian mysql-5.1.72]# chkconfig --add mysqld[root@feitian mysql-5.1.72]# chkconfig mysqld on或者放入rc.local中
9、登陆MySQL数据库 测试
9.1为root增加密码
[root@feitian mysql-5.1.72]# mysqladmin -u root password 'fenghui'[root@feitian mysql-5.1.72]# mysqlERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
9.2删除多余用户
mysql> select user,host from mysql.user;+------+-----------+|user | host |+------+-----------+|root | 127.0.0.1|| | feitian ||root | feitian || | localhost||root | localhost|+------+-----------+5 rows in set (0.00 sec)mysql> drop user ""@localhost;\Query OK, 0 rows affected (0.00 sec)mysql> drop user ""@feitian;\Query OK, 0 rows affected (0.00 sec)mysql> drop user root@feitian;\Query OK, 0 rows affected (0.00 sec)mysql> select user,host from mysql.user;+------+-----------+|user | host |+------+-----------+|root |127.0.0.1 ||root | localhost|+------+-----------+2 rows in set (0.00 sec)如果用户名中还有大写字母,则用以下命令删除delete from mysql.user while user="" and host="大写的用户名"
错误总结:如果刚装完数据库启动时出现如下出现错误,一般来说可以重新初始化数据文件即可,要将数据文件全部删除。
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)