Install MySQL by tar package
The connection address configuration of the project database can be configured first: Configure in /etc/hosts:
1. Uninstall
Since CentOS generally has MySQL by default, check the current installation first:
[root@localhost ~]$ rpm -qa|grep -i mysql
# 或者:
[root@localhost ~]$ yum list installed mysql*
After that, to uninstall the database that comes with CentOS, it is recommended to use the yum command, because the yum command can automatically delete dependencies related to mysql; if you use the rpm command, you need to manually delete the files related to mysql, which is more troublesome.
[root@localhost ~]$ yum remove mysql*
# 或者
[root@localhost ~]$ yum remove MySQL-python.x86_64
# 或者
[root@localhost ~]$ rpm -ev package
# 删除遗留文件(yum方式卸载的话应该已经没有遗留文件了):
[root@localhost ~]$ rm -rf /var/lib/mysql
[root@localhost ~]$ rm /etc/my.cnf
After uninstalling, you can use whereis
the command to find mysql
related files. Because it was uninstalled with yum
, mysql
it will be cleaned very clean. If you use it, rpm
it will be very troublesome. There are still many files that need to be cleaned manually.
[root@localhost ~]$ whereis mysql
mysql:
2. Installation
Force installation of all rpm packages in the current folder, ignore dependencies and install:
rpm -Uvh *.rpm --nodeps --force
After uninstalling it, upload the downloaded MySQL installation package to CentOS and decompress it:
Link: MySQL-Download
Create a new folder as follows (create it according to your own wishes, for storing compressed packages):
[root@localhost ~]$ mkdir /data/tools
[root@localhost ~]$ cd /data/tools
[root@localhost ~]$ rz
[root@localhost ~]$ tar -xvf MySQL-5.5.54-1.linux2.6.x86_64.rpm-bundle.tar
# (如果是MySQL8的安装,解压后会有三个xz的压缩包,继续用 tar -xvf 解压第一个xz)
# 安装rpm(按照以下顺序安装):
[root@localhost ~]$ yum localinstall MySQL-server-5.5.54-1.linux2.6.x86_64.rpm
[root@localhost ~]$ yum localinstall MySQL-devel-5.5.54-1.linux2.6.x86_64.rpm
[root@localhost ~]$ yum localinstall MySQL-client-5.5.54-1.linux2.6.x86_64.rpm
[root@localhost ~]$ yum localinstall MySQL-shared-compat-5.5.54-1.linux2.6.x86_64.rpm
3. Start and connect to test and configure
1. start
# 启动MySQL:
[root@localhost ~]$ /etc/init.d/mysql start
# 设置每次开机启动:
[root@localhost ~]$ chkconfig --levels 345 mysqld on
Note:
Mysqld may not be useful here. You can set it as follows:
Copymysql.server
the file to/etc/init.d
and name itmysqld
:
[root@localhost ~]$ cp /usr/share/mysql/mysql.server /etc/init.d/mysqld
[root@localhost ~]$ chkconfig --add mysqld
In this way, you should be able to set up the startup, and then execute the set startup command.
If you have set all the above, but find that mysql cannot be started at startup, it may be caused by selinux. Generally, CentOS will enable selinux by default. Solution: close
it, open it /etc/selinux/config
, and SELINUX=enforcing
Change to , SELINUX=disabled
save, exit and try restarting the machine.
2. Connect
Try connecting to MySQL:
[root@localhost ~]$ mysql
# 进入成功之后可以修改密码:
mysql> set password = password('xxxx')
退出用:
mysql> \q
# 或者
mysql> exit
3. Open the port
Open port 3306 and save:
[root@localhost ~]$ /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
[root@localhost ~]$ /etc/rc.d/init.d/iptables save
4. Coding modification
Check the encoding after entering mysql:
[root@localhost ~]$ mysql
mysql> show variables like 'character%';
# 退出数据库:
mysql> \q
bye
# 关闭数据库:
[root@localhost ~]$ service mysqld stop
Stopping mysqld
The following is to modify the database encoding:
modify the my.cnf file:
[root@localhost ~]$ vi /etc/my.cnf # 更详细的看下面另一种方式装MySQL中的my.cnf配置
# 分别在[client]和[mysqld]下面加入如下代码:
[client]
default_character_set=utf8
[mysqld]
collation_server=utf8_general_ci
character_set_server=utf8
max_allowed_packet = 16M
group_concat_max_len = 4294967295
The content of the my.cnf file is as follows:
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
[mysqld]
explicit_defaults_for_timestamp=true
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 512M
lower_case_table_names = 1
# 0:大小写敏感;
# 1:大小写不敏感
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = "/usr/local/mysql"
datadir = "/usr/local/mysql/data"
port = 3306
# server_id = .....
max_allowed_packet=500M
# The default character set that will be used when a new schema or table is
# created and no character set is defined
character-set-server=utf8
# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER
Restart the mysql service:
[root@localhost ~]$ service mysqld restart
5. Create a new user and authorize it
Add a user to the database and authorize it ( use mysql;
operate after ):
insert into user (host,user) values ('%','test_user');
# 或者是
create user 'test_user'@'%' identified by 'xxx密码';
# 授权
grant all privileges on *.* to 'test_user'@'%' with grant option;
# 或者
grant all privileges on *.* to 'root'@'我电脑的ip地址' identified by '密码';
# 之后刷新:
FLUSH PRIVILEGES;
At this point, the tar package installation method is completed!
Install MySQL using tar.gz method
- Upload the Mysql installation package "mysql-6.0.11-alpha-linux-x86_64-glibc23.tar.gz" to the deployment machine in any location;
- Unzip the Mysql installation package to the directory where it is located. The command is as follows:
[root@localhost ~]$ tar -zxvf tar -zxvf mysql-6.0.11-alpha-linux-x86_64-glibc23.tar.gz
- Copy the decompressed directory to the system's local software directory "/usr/local/". The command is as follows:
[root@localhost ~]$ cp -rf mysql-6.0.11-alpha-linux-x86_64-glibc23 /usr/local/mysql
- Add the mysql user group and mysql user, the command is as follows:
[root@localhost ~]$ groupadd mysql
[root@localhost ~]$ useradd -r -g mysql mysql
- Enter the directory where the Mysql software is installed, and the command is as follows:
[root@localhost ~]$ cd /usr/local/mysql
- Change the owner of the current directory to the newly created mysql user. The command is as follows:
[mysql@localhost mysql]$ chown -R mysql:mysql ./
- Copy the default global startup parameter configuration file to the /etc directory. The command is as follows:
[mysql@localhost mysql]$ cp ./support-files/my-medium.cnf /etc/my.cnf
- Execute the script included in the installation package to install the database. The command is as follows:
[mysql@localhost mysql]$ ./scripts/mysql_install_db --user=mysql
- Change the owner of the current directory to the root user. The command is as follows:
[mysql@localhost mysql]$ chown -R root:root ./
- Modify the owner of the data directory to the mysql user. The command is as follows:
[root@localhost ~]$ chown -R mysql:mysql data
At this point, the database installation is complete, let’s verify it.
11. Modify /etc/my.cnf
the file. Modify the character set of the database and make it "case insensitive".
[client] # 下添加:
default-character-set=utf8
[mysqld] # 下添加:
character-set-server=utf8
collation_server=utf8_general_ci
lower_case_table_names=1
max_allowed_packet = 16M(如果已经有了修改一下)
group_concat_max_len = 4294967295
[mysql] # 下添加:
default-character-set=utf8
- Start the Mysql database. Note: You need to execute it with root privileges. The command is as follows:
[root@localhost ~]$ sudo /usr/local/mysql/support-files/mysql.server start
Check whether the Mysql process has been started. The command is as follows:
[root@localhost ~]$ ps -ef|grep mysql
The results shown in the figure below indicate that the Mysql database was started successfully:
- Modify the password of the root user of the Mysql database. The initial password of root is empty by default. The command is as follows:
[root@localhost mysql]$ ./bin/mysqladmin -u root password '此处填写密码'
Install mysql-client client
If you want to use the mysql command in the server, you can just install the mysql-client client:
rpm package installation:
[root@localhost ~]$ rpm -ivh mysql-community-client-5.7.34-1.el7.x86_64.rpm --force --nodeps
After installation, if you run the mysql command, an error will be reported:
Executing mysql -u root -p will report an error:
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
Reason: mysql found that this dependency was missing when starting libncurses.so.5
, and the dependency could not be found in /usr/lib
and/lib
Solution:
first check the dependencies of mysql:
[root@localhost ~]$ cd /usr/bin/
[root@localhost ~]$ ldd mysql
You will see the following dependencies:
Look for a dependency file greater than or equal to the dependency version in /usr/lib, /lib64, /usr/lib64. Mine found a libncurses.so.6.1 in /lib64, and then created A soft link (equivalent to a shortcut):
sudo ln -s file path shortcut path
[root@localhost ~]$ sudo ln -s /lib64/libncurses.so.6.1 /lib64/libncurses.so.5
This creates a shortcut /lib64
under . libncurses.so.5
When mysql starts, it will look for libncurses.so.5
the shortcut of the dependency, and finally it will actually link to libncurses.so.6.1
the dependency. When executed again at this time, mysql -u root -p
it will pass
mysql settings startup
# 将服务文件拷贝到init.d下,并重命名为mysql
[root@localhost ~]$ cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# 赋予可执行权限
[root@localhost ~]$ chmod +x /etc/init.d/mysql
# 添加服务
[root@localhost ~]$ chkconfig --add mysql
# 显示服务列表
[root@localhost ~]$ chkconfig --list
# 如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则键入
[root@localhost ~]$ chkconfig --level 345 mysql on
# 重启电脑
[root@localhost ~]$ reboot
# 如果看到有监听说明服务启动了
[root@localhost ~]$ netstat -na | grep 3306
If you have set all the above, but find that mysql cannot be started at boot, it is most likely to be the selinux
fault. Generally, CentOS will enable selinux by default.
Solution: close it, open it, /etc/selinux/config
change it to , SELINUX=enforcing
save SELINUX=disabled
and exit, and try restarting the machine.