MySQL Series (i) Installation
Take the example of installing MySQL 5.6 under CENTOS7.
First, the Environment preparation
(1) Download
: https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
(2) Installation dependency
yum -y install perl perl-devel autoconf libaio
(3) Uninstall the installed MySQL
yum list installed | grep mysql # 查询已安装的 MySQLyum -y remove mysql-libs.x86_64
(4) Create a MySQL user
userdel mysql # 删除 mysql 用户groupdel mysql # 删除 mysql 用户组groupadd mysql # 创建 mysql 用户组useradd -g mysql mysql # 创建 mysql 用户并加入 mysql 用户组passwd mysql # mysq 添加密码
(5) sudo password-free operation
visudomysql ALL=(ALL) ALL%wheel ALL=(ALL) NOPASSWD: ALL# 用户加入 wheel 组gpasswd -a mysql wheel
Second, MySQL installation
(1) switch to MySQL account
# 切换到 mysql 帐号su mysql# 切换到 mysql 目录cd
(2) Unzip the downloaded binary installation package and place it in the/usr/local/mysql directory
tar -zxvf mysql-5.6.38-linux-glibc2.12-x86_64.tar.gzsudo mv mysql-5.6.38-linux-glibc2.12-x86_64 /usr/local/mysql
(3) Environment variables
sudo vim /etc/profile.d/start.shexport PATH=$PATH:/usr/local/mysql/binsource /etc/profile.d/start.sh # 立即生效
(4) Create a new configuration file under etc my.cnf
[mysql]# 设置mysql客户端默认字符集default-character-set=utf8 socket=/var/lib/mysql/mysql.sock[mysqld]user=mysqlskip-name-resolve#设置3306端口port = 3306 socket=/var/lib/mysql/mysql.sock# 设置mysql的安装目录basedir=/usr/local/mysql# 设置mysql数据库的数据的存放目录datadir=/usr/local/mysql/data# 允许最大连接数max_connections=200# 服务端使用的字符集默认为8比特编码的latin1字符集character-set-server=utf8# 创建新表时将使用的默认存储引擎default-storage-engine=INNODB lower_case_table_names=1max_allowed_packet=16M
(5) Create a/var/lib/mysql and change the user to MySQL
# my.cnf 文件中配制的 socket 目录sudo mkdir /var/lib/mysqlsudo chown -R mysql:mysql /var/lib/mysql
(6) Initializing MySQL
./scripts/mysql_install_db --user=mysql
To this MySQL installation is complete! The following describes MySQL as a service startup mode.
Third, MySQL startup
1. Add MySQL service boot from boot
sudo cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld # 复制启动脚本到资源目录sudo chmod +x /etc/rc.d/init.d/mysqld # 增加 mysqld 服务控制脚本执行权限sudo chkconfig --add mysqld # 将 mysqld 服务加入到系统服务sudo chkconfig --list mysqld # 检查 mysqld 服务是否已经生效
The command output resembles the following result:
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Indicates that the MYSQLD service is in effect and starts automatically with the system boot at the 2, 3, 4, 5 runlevel, and can be used to control the start and stop of MySQL later using the service command.
2. Start mSQL
service mysqld start # 启动 msqlservice mysqld stop # 停止msql
Iv. Control of permissions
(1) Setting up MySQL account and password
Log in as root account MySQL, default is no password
update user set password=password(‘you password‘) where user=‘root‘ and host=‘localhost‘;GRANT ALL PRIVILEGES ON *.* TO ‘your username‘@‘%‘ IDENTIFIED BY ‘your password‘ WITH GRANT OPTION;GRANT USAGE ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘root‘ WITH GRANT OPTION;GRANT ALL PRIVILEGES ON *.* TO [email protected]‘%‘ IDENTIFIED BY ‘root‘;FLUSH PRIVILEGES;
(2) Removing anonymous users
delete from mysql.user where User=‘‘; # 删除匿名用户,使用root用户登录数据库flush privileges;
V. Error handling
MySQL Log storage directory: MY.CNF in the DataDir directory, such as Datadir=/usr/local/mysql/data, a new "hostname. Err" file is created in the DataDir directory
(1) Problem 1:can ' t connect to local MySQL server through socket '/tmp/mysql.sock '
ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘ (2)
Workaround:
Open/ETC/MY.CNF to see what directory the socket is configured in, such as Socket=/var/lib/mysql/mysql.sock
MySQL server start times wrong, it is likely that the MySQL user does not have permission to operate/var/lib/mysql. It is generally possible to fix the problem by changing the/var/lib/mysql user to a MySQL user.
chown -R mysql:mysql /var/lib/mysql
MySQL Client connection times are wrong, it is likely that the socket path and "/tmp/mysql.sock" are inconsistent. To establish a soft connection:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
Record a little bit every day. Content may not be important, but habits are important!
MySQL Series (i) Installation