一、系统环境
OS系统环境:RHEL release 6.1
Mysql版本:mysql-5.5.17.tar.gz
二、数据库安装
2.1新增mysql用户
[root@localhost ~]# groupadd mysql –g 502
[root@localhost ~]# useradd -g mysql mysql
系统环境配置:
[root@localhost ~]# yum install -y gcc gcc-c++ make ncurses-devel bison cmake libaio libaio-devel
2.2编译安装
[root@localhost ~]# mkdir /data/mysql
[root@localhost ~]# mkdir /log/mysql -p
[root@localhost ~]#chown -R mysql:mysql /data/mysql
[root@localhost ~]# tar zxvf mysql-5.5.17.tar.gz
[root@localhost ~]# cd mysql-5.5.17
[root@localhost ~]# cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ ===> 指定MySQL安装路径
-DMYSQL_DATADIR=/data/mysql \ ===> mysql数据保存的路径
-DWITH_DEBUG=0 \ ===> 关闭debug模式
-DDEFAULT_CHARSET=utf8 \ ===> 默认服务器字符集
-DWITH_EXTRA_CHARSETS=all \ ===> 额外的字符集
-DDEFAULT_COLLATION=utf8_general_ci \ ===> 服务器的排序规则。默认情况下,MySQL使用latin1_swedish_ci。使用SHOW COLLATION语句来确定每个字符集的排序规则。
-DENABLED_LOCAL_INFILE=1 \ ===> 允许从本地导入数据,load命令需要用到
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ ===> 打开innodb引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 \ ===> 打开partition引擎
或者:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_I*ILE=1 -DMYSQL_DATADIR=/data/mysql -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
[root@localhost ~]# make && make install
2.3创建MySQL授权表
[root@localhost ~]# cd /usr/local/mysql
[root@localhost ~]# chown -R root:mysql .
[root@localhost ~]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql
[root@localhost ~]# cp support-files/my-medium.cnf /etc/my.cnf
2.4配置mysql
[root@localhost ~]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chmod +x /etc/init.d/mysqld
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig --level 2345 mysqld on
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
[root@localhost ~]# source /etc/profile
2.5修改配置文件(my.cnf )
[mysqld]
pid-file=/var/run/mysqld/mysqld.pid (可以不要)
max_connections=1000
log-error=/log/mysql/mysql
skip-name-resolve
2.6启动mysql服务器
[root@localhost ~]# /etc/init.d/mysqld start
修改mysql中root用户的密码
[root@localhost ~]# mysqladmin –u root password ‘123456’
登录mysql
#Mysql –uroot –p
#输入密码 (如果提示1045错误,说明密码输入错误)
如果忘记密码按照以下步骤重新设置密码
1、修改MySQL的登录设置:
# vi /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
例如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-name-resolve
skip-grant-tables (加了这句语句不用密码都可以任意登录)
保存并且退出vi。
2.重新启动mysqld
# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
3.登录并修改MySQL的root密码
#mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3 to server version: 3.23.56
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> USE mysql ;
Database changed
mysql> UPDATE user SET Password = password ( ‘new-password’ ) WHERE User = ‘root’ ;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0
mysql> flush privileges ;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
4.将MySQL的登录设置修改回来
# vi /etc/my.cnf
将刚才在[mysqld]的段中加上的skip-grant-tables删除
保存并且退出vi。
5.重新启动mysqld
# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
2.7查看mysql是否启用libaio
mysql> show variables like ‘%aio‘;
+---------------------------+----------+
| Variable_name | Value |
+----------------------------+----------+
| innodb_use_native_aio | ON |
+----------------------------+----------+
2.8删除不必要的账号和设置允许远程登录
#mysql
mysql> show databases;
mysql> use mysql;
mysql> select user,host,password from user;
mysql> delete from user where user=‘ ‘ and host=‘host~sina4‘;
mysql> delete from user where user=‘ ‘ and host=‘localhost‘;
mysql> delete from user where user=‘root‘ and host=‘::1‘;
mysql> delete from user where user=‘root‘ and host=‘127.0.0.1‘;
mysql> select user,host,password from user;
mysql> flush privileges;
mysql> show databases;
mysql> use mysql;
mysql> select user,host,password from user;
mysql> grant all privileges on *.* to root@‘%‘ identified by ‘mima‘; ##给所有用户登录
mysql> flush privileges;
mysql> show databases;
mysql> use mysql;
mysql> select host,user,password from user;
最后测试:mysql -h IP地址 -uroot –p
如果还登录不了,检查服务器iptables设置,需要开放3306端口